Skip to content

Commit

Permalink
Add object primitive support to createAnyOneOf (#895)
Browse files Browse the repository at this point in the history
* add support for object primitive in anyoneof

* add test for object primitive

* switch to createDetailsNode and add margin bottom

* comment legacy createAnyOneOfProperty function

* update test snapshot
  • Loading branch information
sserrata authored Jul 23, 2024
1 parent a67d2b8 commit 00b3f78
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 133 deletions.
11 changes: 11 additions & 0 deletions demo/examples/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ paths:
description: Add new pet to the store inventory.
operationId: addPet
responses:
"200":
description: All good
content:
application/json:
schema:
type: object
properties:
data:
oneOf:
- type: string
- type: object
"405":
description: Invalid input
security:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Array [
</summary>
<div style={{ marginLeft: \\"1rem\\" }}>
<div>
<span className={\\"badge badge--info\\"}>oneOf</span>
<span className={\\"badge badge--info\\"} style={{ marginBottom: \\"1rem\\" }}>
oneOf
</span>
<SchemaTabs>
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
<SchemaItem
Expand Down Expand Up @@ -226,67 +228,72 @@ Array [
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>
<details style={{}} className={\\"openapi-markdown__details\\"}>
<summary style={{}}>
<strong>oneOfProperty</strong>
<span style={{ opacity: \\"0.6\\" }}>object</span>
<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>
<div>
<span className={\\"badge badge--info\\"}>oneOf</span>
<SchemaTabs>
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
<SchemaItem
collapsible={false}
name={\\"noseLength\\"}
required={true}
schemaName={\\"number\\"}
qualifierMessage={undefined}
schema={{ type: \\"number\\" }}
></SchemaItem>
</TabItem>
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
<li>
<div
style={{
fontSize: \\"var(--ifm-code-font-size)\\",
opacity: \\"0.6\\",
marginLeft: \\"-.5rem\\",
paddingBottom: \\".5rem\\",
}}
>
Array [
<div style={{ marginLeft: \\"1rem\\" }}>
<div>
<span className={\\"badge badge--info\\"} style={{ marginBottom: \\"1rem\\" }}>
oneOf
</span>
<SchemaTabs>
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
<SchemaItem
collapsible={false}
name={\\"noseLength\\"}
required={true}
schemaName={\\"number\\"}
qualifierMessage={undefined}
schema={{ type: \\"number\\" }}
></SchemaItem>
</TabItem>
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
<li>
<div
style={{
fontSize: \\"var(--ifm-code-font-size)\\",
opacity: \\"0.6\\",
marginLeft: \\"-.5rem\\",
paddingBottom: \\".5rem\\",
}}
>
Array [
</div>
</li>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
string
</div>
<li>
<div
style={{
fontSize: \\"var(--ifm-code-font-size)\\",
opacity: \\"0.6\\",
marginLeft: \\"-.5rem\\",
}}
>
]
</div>
</li>
</TabItem>
<TabItem label={\\"MOD3\\"} value={\\"2-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
boolean
</div>
</li>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
string
</div>
<li>
<div
style={{
fontSize: \\"var(--ifm-code-font-size)\\",
opacity: \\"0.6\\",
marginLeft: \\"-.5rem\\",
}}
>
]
</TabItem>
<TabItem label={\\"MOD4\\"} value={\\"3-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
number
</div>
</li>
</TabItem>
<TabItem label={\\"MOD3\\"} value={\\"2-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
boolean
</div>
</TabItem>
<TabItem label={\\"MOD4\\"} value={\\"3-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
number
</div>
</TabItem>
<TabItem label={\\"MOD5\\"} value={\\"4-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
string
</div>
</TabItem>
</SchemaTabs>
</TabItem>
<TabItem label={\\"MOD5\\"} value={\\"4-item-properties\\"}>
<div style={{ marginTop: \\".5rem\\", marginBottom: \\".5rem\\" }}>
string
</div>
</TabItem>
</SchemaTabs>
</div>
</div>
</details>
</SchemaItem>;
Expand Down
158 changes: 84 additions & 74 deletions packages/docusaurus-plugin-openapi-docs/src/markdown/createSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function createAnyOneOf(schema: SchemaObject): any {
create("span", {
className: "badge badge--info",
children: type,
style: { marginBottom: "1rem" },
}),
create("SchemaTabs", {
children: schema[type]!.map((anyOneSchema, index) => {
Expand All @@ -74,6 +75,15 @@ function createAnyOneOf(schema: SchemaObject): any {
: `MOD${index + 1}`;
const anyOneChildren = [];

if (
anyOneSchema.type === "object" &&
!anyOneSchema.properties &&
!anyOneSchema.allOf &&
!anyOneSchema.items
) {
anyOneChildren.push(createNodes(anyOneSchema, SCHEMA_TYPE));
}

if (anyOneSchema.properties !== undefined) {
anyOneChildren.push(createProperties(anyOneSchema));
delete anyOneSchema.properties;
Expand Down Expand Up @@ -415,79 +425,79 @@ function createDetailsNode(
/**
* For handling anyOf/oneOf properties.
*/
function createAnyOneOfProperty(
name: string,
schemaName: string,
schema: SchemaObject,
required: string[] | boolean,
nullable: boolean | unknown
): any {
return create("SchemaItem", {
collapsible: true,
className: "schemaItem",
children: [
createDetails({
className: "openapi-markdown__details",
children: [
createDetailsSummary({
children: [
create("strong", { children: name }),
create("span", {
style: { opacity: "0.6" },
children: ` ${schemaName}`,
}),
guard(
(schema.nullable && schema.nullable === true) ||
(nullable && nullable === true),
() => [
create("strong", {
style: {
fontSize: "var(--ifm-code-font-size)",
color: "var(--openapi-nullable)",
},
children: " nullable",
}),
]
),
guard(
Array.isArray(required)
? required.includes(name)
: required === true,
() => [
create("strong", {
style: {
fontSize: "var(--ifm-code-font-size)",
color: "var(--openapi-required)",
},
children: " required",
}),
]
),
],
}),
create("div", {
style: { marginLeft: "1rem" },
children: [
guard(getQualifierMessage(schema), (message) =>
create("div", {
style: { marginTop: ".5rem", marginBottom: ".5rem" },
children: createDescription(message),
})
),
guard(schema.description, (description) =>
create("div", {
style: { marginTop: ".5rem", marginBottom: ".5rem" },
children: createDescription(description),
})
),
],
}),
createAnyOneOf(schema),
],
}),
],
});
}
// function createAnyOneOfProperty(
// name: string,
// schemaName: string,
// schema: SchemaObject,
// required: string[] | boolean,
// nullable: boolean | unknown
// ): any {
// return create("SchemaItem", {
// collapsible: true,
// className: "schemaItem",
// children: [
// createDetails({
// className: "openapi-markdown__details",
// children: [
// createDetailsSummary({
// children: [
// create("strong", { children: name }),
// create("span", {
// style: { opacity: "0.6" },
// children: ` ${schemaName}`,
// }),
// guard(
// (schema.nullable && schema.nullable === true) ||
// (nullable && nullable === true),
// () => [
// create("strong", {
// style: {
// fontSize: "var(--ifm-code-font-size)",
// color: "var(--openapi-nullable)",
// },
// children: " nullable",
// }),
// ]
// ),
// guard(
// Array.isArray(required)
// ? required.includes(name)
// : required === true,
// () => [
// create("strong", {
// style: {
// fontSize: "var(--ifm-code-font-size)",
// color: "var(--openapi-required)",
// },
// children: " required",
// }),
// ]
// ),
// ],
// }),
// create("div", {
// style: { marginLeft: "1rem" },
// children: [
// guard(getQualifierMessage(schema), (message) =>
// create("div", {
// style: { marginTop: ".5rem", marginBottom: ".5rem" },
// children: createDescription(message),
// })
// ),
// guard(schema.description, (description) =>
// create("div", {
// style: { marginTop: ".5rem", marginBottom: ".5rem" },
// children: createDescription(description),
// })
// ),
// ],
// }),
// createAnyOneOf(schema),
// ],
// }),
// ],
// });
// }

/**
* For handling discriminators that map to a same-level property (like 'petType').
Expand Down Expand Up @@ -606,7 +616,7 @@ function createEdges({
}

if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
return createAnyOneOfProperty(
return createDetailsNode(
name,
schemaName,
schema,
Expand Down

0 comments on commit 00b3f78

Please sign in to comment.