diff --git a/demo/examples/tests/anyOf.yaml b/demo/examples/tests/anyOf.yaml new file mode 100644 index 000000000..a19581e8f --- /dev/null +++ b/demo/examples/tests/anyOf.yaml @@ -0,0 +1,63 @@ +openapi: 3.0.1 +info: + title: AnyOf Variations API + description: Demonstrates various anyOf combinations. + version: 1.0.0 +tags: + - name: anyOf + description: anyOf tests +paths: + /anyof-primitives: + get: + tags: + - anyOf + summary: anyOf with primitives + description: | + Schema: + ```yaml + anyOf: + - type: string + - type: integer + - type: boolean + ``` + responses: + "200": + description: Successful response + content: + application/json: + schema: + anyOf: + - type: string + - type: integer + - type: boolean + + /anyof-oneof: + get: + tags: + - anyOf + summary: anyOf with oneOf + description: | + Schema: + ```yaml + anyOf: + - oneOf: + - type: string + - type: integer + - type: boolean + ``` + responses: + "200": + description: Successful response + content: + application/json: + schema: + anyOf: + - oneOf: + - type: string + title: A string + - type: integer + title: An integer + title: A string or integer + - type: boolean + title: A boolean + title: A string or integer, or a boolean diff --git a/packages/docusaurus-plugin-openapi-docs/src/markdown/__snapshots__/createSchema.test.ts.snap b/packages/docusaurus-plugin-openapi-docs/src/markdown/__snapshots__/createSchema.test.ts.snap index febded87a..7b2a8b125 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/markdown/__snapshots__/createSchema.test.ts.snap +++ b/packages/docusaurus-plugin-openapi-docs/src/markdown/__snapshots__/createSchema.test.ts.snap @@ -368,6 +368,93 @@ Array [ ] `; +exports[`createNodes anyOf should render oneOf within anyOf 1`] = ` +Array [ + " +
+ + + oneOfProperty + object + + +
+
+ + anyOf + + + +
+ + oneOf + + + +
+ integer +
+
+ +
+ boolean +
+
+
+
+
+ +
+ string +
+
+
+
+
+
+
; +", +] +`; + +exports[`createNodes anyOf should render primitives within anyOf 1`] = ` +Array [ + " +
+ + + oneOfProperty + object + + +
+
+ + anyOf + + + +
+ integer +
+
+ +
+ boolean +
+
+
+
+
+
+
; +", +] +`; + exports[`createNodes discriminator should handle basic discriminator with mapping 1`] = ` Array [ "
diff --git a/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.test.ts b/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.test.ts index 1b50900e9..8124da1ba 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.test.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.test.ts @@ -246,6 +246,70 @@ describe("createNodes", () => { }); }); + describe("anyOf", () => { + it("should render primitives within anyOf", async () => { + const schema: SchemaObject = { + type: "object", + properties: { + oneOfProperty: { + anyOf: [ + { + type: "integer", + }, + { + type: "boolean", + }, + ], + title: "One of int or bool", + }, + }, + }; + + expect( + await Promise.all( + createNodes(schema, "response").map( + async (md: any) => await prettier.format(md, { parser: "babel" }) + ) + ) + ).toMatchSnapshot(); + }); + + it("should render oneOf within anyOf", async () => { + const schema: SchemaObject = { + type: "object", + properties: { + oneOfProperty: { + anyOf: [ + { + oneOf: [ + { + type: "integer", + }, + { + type: "boolean", + }, + ], + title: "An int or a bool", + }, + { + type: "string", + }, + ], + title: "One of int or bool, or a string", + }, + }, + }; + + expect( + await Promise.all( + createNodes(schema, "response").map( + async (md: any) => await prettier.format(md, { parser: "babel" }) + ) + ) + ).toMatchSnapshot(); + }); + }); + describe("allOf", () => { it("should render same-level properties with allOf", async () => { const schema: SchemaObject = { diff --git a/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts b/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts index b78a14b08..ff13b3f79 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts @@ -104,6 +104,11 @@ function createAnyOneOf(schema: SchemaObject): any { delete anyOneSchema.allOf; } + if (anyOneSchema.oneOf !== undefined) { + anyOneChildren.push(createNodes(anyOneSchema, SCHEMA_TYPE)); + delete anyOneSchema.oneOf; + } + if (anyOneSchema.items !== undefined) { anyOneChildren.push(createItems(anyOneSchema)); delete anyOneSchema.items;