diff --git a/demo/examples/tests/allOf.yaml b/demo/examples/tests/allOf.yaml index e53756e99..1f229f3b7 100644 --- a/demo/examples/tests/allOf.yaml +++ b/demo/examples/tests/allOf.yaml @@ -475,6 +475,41 @@ paths: priority: $ref: "#/components/schemas/Priority" + /allof-incompatible-types: + get: + tags: + - allOf + summary: allOf with Incompatible Types + description: | + Schema with a property whose allOf contains incompatible primitive types + (string and integer). + + Schema: + ```yaml + type: object + properties: + numero: + allOf: + - type: string + - type: integer + name: + type: string + ``` + responses: + "200": + description: Successful response + content: + application/json: + schema: + type: object + properties: + numero: + allOf: + - type: string + - type: integer + name: + type: string + /allof-multiple-oneof: post: tags: diff --git a/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts b/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts index 7315baa6b..3447507fc 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts @@ -32,7 +32,7 @@ export function mergeAllOf(allOf: SchemaObject) { }; const mergedSchemas = merge(allOf, { onMergeError }) as SchemaObject; - return mergedSchemas; + return mergedSchemas ?? ({} as SchemaObject); } /** diff --git a/packages/docusaurus-plugin-openapi-docs/src/openapi/createSchemaExample.test.ts b/packages/docusaurus-plugin-openapi-docs/src/openapi/createSchemaExample.test.ts index 697a2e2c1..0d987dabe 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/openapi/createSchemaExample.test.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/openapi/createSchemaExample.test.ts @@ -54,4 +54,36 @@ describe("sampleFromSchema", () => { expect(result).toBe("dog"); }); }); + + describe("allOf with incompatible types", () => { + it("should return undefined when allOf contains incompatible types", () => { + const schema: SchemaObject = { + allOf: [{ type: "string" }, { type: "integer" }], + }; + const context = { type: "request" as const }; + + const result = sampleFromSchema(schema, context); + + expect(result).toBeUndefined(); + }); + + it("should handle incompatible allOf types in a property", () => { + const schema: SchemaObject = { + type: "object", + properties: { + numero: { + allOf: [{ type: "string" }, { type: "integer" }], + }, + name: { + type: "string", + }, + }, + }; + const context = { type: "request" as const }; + + const result = sampleFromSchema(schema, context); + + expect(result.name).toBe("string"); + }); + }); }); diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx index 631c46439..62dd06546 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx @@ -41,7 +41,7 @@ const mergeAllOf = (allOf: any) => { const mergedSchemas = merge(allOf, { onMergeError }); - return mergedSchemas; + return mergedSchemas ?? {}; }; interface MarkdownProps {