Handle non-array required field in OpenAPI schema (#4030)

This commit is contained in:
Nolann B.
2026-02-21 13:34:12 +01:00
committed by GitHub
parent 5f3f4da2d2
commit 1a7ef78fe2
3 changed files with 71 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@gitbook/react-openapi": patch
---
Handle non-array required field in OpenAPI schema to fix TypeError with specs using boolean required values
@@ -185,6 +185,60 @@ describe('getSchemaAlternatives', () => {
});
});
it('should handle non-standard boolean required values without throwing', () => {
// Some specs (e.g. Trustly) use `"required": true` on properties
// instead of the standard `string[]` format. This should not throw.
const schema = {
allOf: [
{
type: 'object',
properties: {
name: { type: 'string' },
},
required: true as any,
},
{
type: 'object',
properties: {
email: { type: 'string' },
},
required: ['email'],
},
],
} as any;
const result = getSchemaAlternatives(schema);
expect(result).toBeDefined();
// The boolean `required: true` should be ignored, only the valid array is kept
expect(result?.schemas[0]?.required).toEqual(['email']);
});
it('should handle boolean required on both schemas without throwing', () => {
const schema = {
allOf: [
{
type: 'object',
properties: {
name: { type: 'string' },
},
required: true as any,
},
{
type: 'object',
properties: {
email: { type: 'string' },
},
required: false as any,
},
],
} as any;
const result = getSchemaAlternatives(schema);
expect(result).toBeDefined();
// Boolean required values should not cause a crash, result is an empty array
expect(result?.schemas[0]?.required).toEqual([]);
});
describe('safe merging with allOf', () => {
it('should merge objects with safe extensions', () => {
expect(
+12 -8
View File
@@ -1105,15 +1105,19 @@ function mergeRequiredFields(
schemaOrRef: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
latestAncestor: OpenAPIV3.SchemaObject | undefined
) {
if (!schemaOrRef.required && !latestAncestor?.required) {
const ancestorRequired = Array.isArray(latestAncestor?.required)
? latestAncestor.required
: undefined;
if (checkIsReference(schemaOrRef)) {
return ancestorRequired;
}
const schemaRequired = Array.isArray(schemaOrRef.required) ? schemaOrRef.required : undefined;
if (!ancestorRequired && !schemaRequired) {
return undefined;
}
if (checkIsReference(schemaOrRef)) {
return latestAncestor?.required;
}
return Array.from(
new Set([...(latestAncestor?.required || []), ...(schemaOrRef.required || [])])
);
return Array.from(new Set([...(ancestorRequired || []), ...(schemaRequired || [])]));
}