Skip to content

Commit 6198c9b

Browse files
fix: handle allOf with incompatible types (#1308)
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
1 parent 7beaa7d commit 6198c9b

4 files changed

Lines changed: 69 additions & 2 deletions

File tree

demo/examples/tests/allOf.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,41 @@ paths:
475475
priority:
476476
$ref: "#/components/schemas/Priority"
477477

478+
/allof-incompatible-types:
479+
get:
480+
tags:
481+
- allOf
482+
summary: allOf with Incompatible Types
483+
description: |
484+
Schema with a property whose allOf contains incompatible primitive types
485+
(string and integer).
486+
487+
Schema:
488+
```yaml
489+
type: object
490+
properties:
491+
numero:
492+
allOf:
493+
- type: string
494+
- type: integer
495+
name:
496+
type: string
497+
```
498+
responses:
499+
"200":
500+
description: Successful response
501+
content:
502+
application/json:
503+
schema:
504+
type: object
505+
properties:
506+
numero:
507+
allOf:
508+
- type: string
509+
- type: integer
510+
name:
511+
type: string
512+
478513
/allof-multiple-oneof:
479514
post:
480515
tags:

packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function mergeAllOf(allOf: SchemaObject) {
3232
};
3333

3434
const mergedSchemas = merge(allOf, { onMergeError }) as SchemaObject;
35-
return mergedSchemas;
35+
return mergedSchemas ?? ({} as SchemaObject);
3636
}
3737

3838
/**

packages/docusaurus-plugin-openapi-docs/src/openapi/createSchemaExample.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,36 @@ describe("sampleFromSchema", () => {
5454
expect(result).toBe("dog");
5555
});
5656
});
57+
58+
describe("allOf with incompatible types", () => {
59+
it("should return undefined when allOf contains incompatible types", () => {
60+
const schema: SchemaObject = {
61+
allOf: [{ type: "string" }, { type: "integer" }],
62+
};
63+
const context = { type: "request" as const };
64+
65+
const result = sampleFromSchema(schema, context);
66+
67+
expect(result).toBeUndefined();
68+
});
69+
70+
it("should handle incompatible allOf types in a property", () => {
71+
const schema: SchemaObject = {
72+
type: "object",
73+
properties: {
74+
numero: {
75+
allOf: [{ type: "string" }, { type: "integer" }],
76+
},
77+
name: {
78+
type: "string",
79+
},
80+
},
81+
};
82+
const context = { type: "request" as const };
83+
84+
const result = sampleFromSchema(schema, context);
85+
86+
expect(result.name).toBe("string");
87+
});
88+
});
5789
});

packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const mergeAllOf = (allOf: any) => {
4141

4242
const mergedSchemas = merge(allOf, { onMergeError });
4343

44-
return mergedSchemas;
44+
return mergedSchemas ?? {};
4545
};
4646

4747
interface MarkdownProps {

0 commit comments

Comments
 (0)