Skip to content

Commit

Permalink
Merge pull request #944 from robbieaverill/fix-oneof-within-anyof
Browse files Browse the repository at this point in the history
Fix bug where oneOf within anyOf would not be rendered in request schemas
  • Loading branch information
sserrata authored Sep 5, 2024
2 parents 480727d + cd2ef3b commit bdc4028
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
63 changes: 63 additions & 0 deletions demo/examples/tests/anyOf.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,93 @@ Array [
]
`;

exports[`createNodes anyOf should render oneOf within anyOf 1`] = `
Array [
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>
<details style={{}} className={\\"openapi-markdown__details\\"}>
<summary style={{}}>
<span className={\\"openapi-schema__container\\"}>
<strong className={\\"openapi-schema__property\\"}>oneOfProperty</strong>
<span className={\\"openapi-schema__name\\"}>object</span>
</span>
</summary>
<div style={{ marginLeft: \\"1rem\\" }}>
<div>
<span className={\\"badge badge--info\\"} style={{ marginBottom: \\"1rem\\" }}>
anyOf
</span>
<SchemaTabs>
<TabItem label={\\"An int or a bool\\"} value={\\"0-item-properties\\"}>
<div>
<span
className={\\"badge badge--info\\"}
style={{ marginBottom: \\"1rem\\" }}
>
oneOf
</span>
<SchemaTabs>
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
integer
</div>
</TabItem>
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
boolean
</div>
</TabItem>
</SchemaTabs>
</div>
</TabItem>
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
string
</div>
</TabItem>
</SchemaTabs>
</div>
</div>
</details>
</SchemaItem>;
",
]
`;

exports[`createNodes anyOf should render primitives within anyOf 1`] = `
Array [
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>
<details style={{}} className={\\"openapi-markdown__details\\"}>
<summary style={{}}>
<span className={\\"openapi-schema__container\\"}>
<strong className={\\"openapi-schema__property\\"}>oneOfProperty</strong>
<span className={\\"openapi-schema__name\\"}>object</span>
</span>
</summary>
<div style={{ marginLeft: \\"1rem\\" }}>
<div>
<span className={\\"badge badge--info\\"} style={{ marginBottom: \\"1rem\\" }}>
anyOf
</span>
<SchemaTabs>
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
integer
</div>
</TabItem>
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
boolean
</div>
</TabItem>
</SchemaTabs>
</div>
</div>
</details>
</SchemaItem>;
",
]
`;

exports[`createNodes discriminator should handle basic discriminator with mapping 1`] = `
Array [
"<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bdc4028

Please sign in to comment.