Skip to content

Commit bdc4028

Browse files
authored
Merge pull request #944 from robbieaverill/fix-oneof-within-anyof
Fix bug where oneOf within anyOf would not be rendered in request schemas
2 parents 480727d + cd2ef3b commit bdc4028

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

demo/examples/tests/anyOf.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
openapi: 3.0.1
2+
info:
3+
title: AnyOf Variations API
4+
description: Demonstrates various anyOf combinations.
5+
version: 1.0.0
6+
tags:
7+
- name: anyOf
8+
description: anyOf tests
9+
paths:
10+
/anyof-primitives:
11+
get:
12+
tags:
13+
- anyOf
14+
summary: anyOf with primitives
15+
description: |
16+
Schema:
17+
```yaml
18+
anyOf:
19+
- type: string
20+
- type: integer
21+
- type: boolean
22+
```
23+
responses:
24+
"200":
25+
description: Successful response
26+
content:
27+
application/json:
28+
schema:
29+
anyOf:
30+
- type: string
31+
- type: integer
32+
- type: boolean
33+
34+
/anyof-oneof:
35+
get:
36+
tags:
37+
- anyOf
38+
summary: anyOf with oneOf
39+
description: |
40+
Schema:
41+
```yaml
42+
anyOf:
43+
- oneOf:
44+
- type: string
45+
- type: integer
46+
- type: boolean
47+
```
48+
responses:
49+
"200":
50+
description: Successful response
51+
content:
52+
application/json:
53+
schema:
54+
anyOf:
55+
- oneOf:
56+
- type: string
57+
title: A string
58+
- type: integer
59+
title: An integer
60+
title: A string or integer
61+
- type: boolean
62+
title: A boolean
63+
title: A string or integer, or a boolean

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,93 @@ Array [
368368
]
369369
`;
370370

371+
exports[`createNodes anyOf should render oneOf within anyOf 1`] = `
372+
Array [
373+
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>
374+
<details style={{}} className={\\"openapi-markdown__details\\"}>
375+
<summary style={{}}>
376+
<span className={\\"openapi-schema__container\\"}>
377+
<strong className={\\"openapi-schema__property\\"}>oneOfProperty</strong>
378+
<span className={\\"openapi-schema__name\\"}>object</span>
379+
</span>
380+
</summary>
381+
<div style={{ marginLeft: \\"1rem\\" }}>
382+
<div>
383+
<span className={\\"badge badge--info\\"} style={{ marginBottom: \\"1rem\\" }}>
384+
anyOf
385+
</span>
386+
<SchemaTabs>
387+
<TabItem label={\\"An int or a bool\\"} value={\\"0-item-properties\\"}>
388+
<div>
389+
<span
390+
className={\\"badge badge--info\\"}
391+
style={{ marginBottom: \\"1rem\\" }}
392+
>
393+
oneOf
394+
</span>
395+
<SchemaTabs>
396+
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
397+
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
398+
integer
399+
</div>
400+
</TabItem>
401+
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
402+
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
403+
boolean
404+
</div>
405+
</TabItem>
406+
</SchemaTabs>
407+
</div>
408+
</TabItem>
409+
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
410+
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
411+
string
412+
</div>
413+
</TabItem>
414+
</SchemaTabs>
415+
</div>
416+
</div>
417+
</details>
418+
</SchemaItem>;
419+
",
420+
]
421+
`;
422+
423+
exports[`createNodes anyOf should render primitives within anyOf 1`] = `
424+
Array [
425+
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>
426+
<details style={{}} className={\\"openapi-markdown__details\\"}>
427+
<summary style={{}}>
428+
<span className={\\"openapi-schema__container\\"}>
429+
<strong className={\\"openapi-schema__property\\"}>oneOfProperty</strong>
430+
<span className={\\"openapi-schema__name\\"}>object</span>
431+
</span>
432+
</summary>
433+
<div style={{ marginLeft: \\"1rem\\" }}>
434+
<div>
435+
<span className={\\"badge badge--info\\"} style={{ marginBottom: \\"1rem\\" }}>
436+
anyOf
437+
</span>
438+
<SchemaTabs>
439+
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
440+
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
441+
integer
442+
</div>
443+
</TabItem>
444+
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
445+
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
446+
boolean
447+
</div>
448+
</TabItem>
449+
</SchemaTabs>
450+
</div>
451+
</div>
452+
</details>
453+
</SchemaItem>;
454+
",
455+
]
456+
`;
457+
371458
exports[`createNodes discriminator should handle basic discriminator with mapping 1`] = `
372459
Array [
373460
"<div>

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,70 @@ describe("createNodes", () => {
246246
});
247247
});
248248

249+
describe("anyOf", () => {
250+
it("should render primitives within anyOf", async () => {
251+
const schema: SchemaObject = {
252+
type: "object",
253+
properties: {
254+
oneOfProperty: {
255+
anyOf: [
256+
{
257+
type: "integer",
258+
},
259+
{
260+
type: "boolean",
261+
},
262+
],
263+
title: "One of int or bool",
264+
},
265+
},
266+
};
267+
268+
expect(
269+
await Promise.all(
270+
createNodes(schema, "response").map(
271+
async (md: any) => await prettier.format(md, { parser: "babel" })
272+
)
273+
)
274+
).toMatchSnapshot();
275+
});
276+
277+
it("should render oneOf within anyOf", async () => {
278+
const schema: SchemaObject = {
279+
type: "object",
280+
properties: {
281+
oneOfProperty: {
282+
anyOf: [
283+
{
284+
oneOf: [
285+
{
286+
type: "integer",
287+
},
288+
{
289+
type: "boolean",
290+
},
291+
],
292+
title: "An int or a bool",
293+
},
294+
{
295+
type: "string",
296+
},
297+
],
298+
title: "One of int or bool, or a string",
299+
},
300+
},
301+
};
302+
303+
expect(
304+
await Promise.all(
305+
createNodes(schema, "response").map(
306+
async (md: any) => await prettier.format(md, { parser: "babel" })
307+
)
308+
)
309+
).toMatchSnapshot();
310+
});
311+
});
312+
249313
describe("allOf", () => {
250314
it("should render same-level properties with allOf", async () => {
251315
const schema: SchemaObject = {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ function createAnyOneOf(schema: SchemaObject): any {
104104
delete anyOneSchema.allOf;
105105
}
106106

107+
if (anyOneSchema.oneOf !== undefined) {
108+
anyOneChildren.push(createNodes(anyOneSchema, SCHEMA_TYPE));
109+
delete anyOneSchema.oneOf;
110+
}
111+
107112
if (anyOneSchema.items !== undefined) {
108113
anyOneChildren.push(createItems(anyOneSchema));
109114
delete anyOneSchema.items;

0 commit comments

Comments
 (0)