Skip to content

Commit

Permalink
Fix bug where oneOf within anyOf would not be rendered in request sch…
Browse files Browse the repository at this point in the history
…emas
  • Loading branch information
robbieaverill committed Sep 5, 2024
1 parent de1099e commit 0ab914a
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
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 0ab914a

Please sign in to comment.