Skip to content

Commit 4e960fd

Browse files
authored
handle various additionalProperties cases (#803)
1 parent b1f73af commit 4e960fd

File tree

4 files changed

+339
-47
lines changed

4 files changed

+339
-47
lines changed

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

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,226 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`createNodes additionalProperties should handle additionalProperties: {"allOf": [Array]} 1`] = `
4+
Array [
5+
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>
6+
<details style={{}} className={\\"openapi-markdown__details\\"}>
7+
<summary style={{}}>
8+
<span className={\\"openapi-schema__container\\"}>
9+
<strong className={\\"openapi-schema__property\\"}>property name*</strong>
10+
<span className={\\"openapi-schema__name\\"}>object</span>
11+
</span>
12+
</summary>
13+
<div style={{ marginLeft: \\"1rem\\" }}>
14+
<div>
15+
<span className={\\"badge badge--info\\"}>oneOf</span>
16+
<SchemaTabs>
17+
<TabItem label={\\"MOD1\\"} value={\\"0-item-properties\\"}>
18+
<SchemaItem
19+
collapsible={false}
20+
name={\\"type\\"}
21+
required={true}
22+
schemaName={\\"string\\"}
23+
qualifierMessage={\\"**Possible values:** [\`nose\`]\\"}
24+
schema={{ type: \\"string\\", enum: [\\"nose\\"] }}
25+
></SchemaItem>
26+
</TabItem>
27+
<TabItem label={\\"MOD2\\"} value={\\"1-item-properties\\"}>
28+
<SchemaItem
29+
collapsible={false}
30+
name={\\"type\\"}
31+
required={true}
32+
schemaName={\\"string\\"}
33+
qualifierMessage={\\"**Possible values:** [\`mouth\`]\\"}
34+
schema={{ type: \\"string\\", enum: [\\"mouth\\"] }}
35+
></SchemaItem>
36+
</TabItem>
37+
<TabItem label={\\"MOD3\\"} value={\\"2-item-properties\\"}>
38+
<SchemaItem
39+
collapsible={false}
40+
name={\\"type\\"}
41+
required={true}
42+
schemaName={\\"string\\"}
43+
qualifierMessage={\\"**Possible values:** [\`eyes\`]\\"}
44+
schema={{ type: \\"string\\", enum: [\\"eyes\\"] }}
45+
></SchemaItem>
46+
<SchemaItem
47+
collapsible={false}
48+
name={\\"default\\"}
49+
required={false}
50+
schemaName={\\"string\\"}
51+
qualifierMessage={undefined}
52+
schema={{ type: \\"string\\" }}
53+
></SchemaItem>
54+
</TabItem>
55+
</SchemaTabs>
56+
</div>
57+
<SchemaItem
58+
collapsible={false}
59+
name={\\"description\\"}
60+
required={true}
61+
schemaName={\\"string\\"}
62+
qualifierMessage={undefined}
63+
schema={{
64+
type: \\"string\\",
65+
description: \\"Description of the body part.\\",
66+
}}
67+
></SchemaItem>
68+
</div>
69+
</details>
70+
</SchemaItem>;
71+
",
72+
]
73+
`;
74+
75+
exports[`createNodes additionalProperties should handle additionalProperties: {"items": [Object], "type": "array"} 1`] = `
76+
Array [
77+
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>
78+
<details style={{}} className={\\"openapi-markdown__details\\"}>
79+
<summary style={{}}>
80+
<span className={\\"openapi-schema__container\\"}>
81+
<strong className={\\"openapi-schema__property\\"}>property name*</strong>
82+
<span className={\\"openapi-schema__name\\"}>object[]</span>
83+
</span>
84+
</summary>
85+
<div style={{ marginLeft: \\"1rem\\" }}>
86+
<li>
87+
<div
88+
style={{
89+
fontSize: \\"var(--ifm-code-font-size)\\",
90+
opacity: \\"0.6\\",
91+
marginLeft: \\"-.5rem\\",
92+
paddingBottom: \\".5rem\\",
93+
}}
94+
>
95+
Array [
96+
</div>
97+
</li>
98+
<SchemaItem
99+
collapsible={false}
100+
name={\\"a\\"}
101+
required={false}
102+
schemaName={\\"\\"}
103+
qualifierMessage={undefined}
104+
schema={\\"string\\"}
105+
></SchemaItem>
106+
<SchemaItem
107+
collapsible={false}
108+
name={\\"b\\"}
109+
required={false}
110+
schemaName={\\"\\"}
111+
qualifierMessage={undefined}
112+
schema={\\"number\\"}
113+
></SchemaItem>
114+
<li>
115+
<div
116+
style={{
117+
fontSize: \\"var(--ifm-code-font-size)\\",
118+
opacity: \\"0.6\\",
119+
marginLeft: \\"-.5rem\\",
120+
}}
121+
>
122+
]
123+
</div>
124+
</li>
125+
</div>
126+
</details>
127+
</SchemaItem>;
128+
",
129+
]
130+
`;
131+
132+
exports[`createNodes additionalProperties should handle additionalProperties: {"type": "boolean"} 1`] = `
133+
Array [
134+
"<SchemaItem
135+
name={\\"property name*\\"}
136+
required={false}
137+
schemaName={\\"boolean\\"}
138+
qualifierMessage={undefined}
139+
schema={{ type: \\"boolean\\" }}
140+
collapsible={false}
141+
discriminator={false}
142+
></SchemaItem>;
143+
",
144+
]
145+
`;
146+
147+
exports[`createNodes additionalProperties should handle additionalProperties: {"type": "integer"} 1`] = `
148+
Array [
149+
"<SchemaItem
150+
name={\\"property name*\\"}
151+
required={false}
152+
schemaName={\\"integer\\"}
153+
qualifierMessage={undefined}
154+
schema={{ type: \\"integer\\" }}
155+
collapsible={false}
156+
discriminator={false}
157+
></SchemaItem>;
158+
",
159+
]
160+
`;
161+
162+
exports[`createNodes additionalProperties should handle additionalProperties: {"type": "number"} 1`] = `
163+
Array [
164+
"<SchemaItem
165+
name={\\"property name*\\"}
166+
required={false}
167+
schemaName={\\"number\\"}
168+
qualifierMessage={undefined}
169+
schema={{ type: \\"number\\" }}
170+
collapsible={false}
171+
discriminator={false}
172+
></SchemaItem>;
173+
",
174+
]
175+
`;
176+
177+
exports[`createNodes additionalProperties should handle additionalProperties: {"type": "string"} 1`] = `
178+
Array [
179+
"<SchemaItem
180+
name={\\"property name*\\"}
181+
required={false}
182+
schemaName={\\"string\\"}
183+
qualifierMessage={undefined}
184+
schema={{ type: \\"string\\" }}
185+
collapsible={false}
186+
discriminator={false}
187+
></SchemaItem>;
188+
",
189+
]
190+
`;
191+
192+
exports[`createNodes additionalProperties should handle additionalProperties: {} 1`] = `
193+
Array [
194+
"<SchemaItem
195+
name={\\"property name*\\"}
196+
required={false}
197+
schemaName={\\"any\\"}
198+
qualifierMessage={undefined}
199+
schema={{ type: \\"object\\", additionalProperties: {} }}
200+
collapsible={false}
201+
discriminator={false}
202+
></SchemaItem>;
203+
",
204+
]
205+
`;
206+
207+
exports[`createNodes additionalProperties should handle additionalProperties: false 1`] = `Array []`;
208+
209+
exports[`createNodes additionalProperties should handle additionalProperties: true 1`] = `
210+
Array [
211+
"<SchemaItem
212+
name={\\"property name*\\"}
213+
required={false}
214+
schemaName={\\"any\\"}
215+
qualifierMessage={undefined}
216+
schema={{ type: \\"object\\", additionalProperties: true }}
217+
collapsible={false}
218+
discriminator={false}
219+
></SchemaItem>;
220+
",
221+
]
222+
`;
223+
3224
exports[`createNodes should create readable MODs for oneOf primitive properties 1`] = `
4225
Array [
5226
"<SchemaItem collapsible={true} className={\\"schemaItem\\"}>

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,91 @@ describe("createNodes", () => {
5656
)
5757
).toMatchSnapshot();
5858
});
59+
60+
describe("additionalProperties", () => {
61+
it.each([
62+
[
63+
{
64+
allOf: [
65+
{
66+
oneOf: [
67+
{
68+
type: "object",
69+
properties: {
70+
type: {
71+
type: "string",
72+
enum: ["nose"],
73+
},
74+
},
75+
required: ["type"],
76+
},
77+
{
78+
type: "object",
79+
properties: {
80+
type: {
81+
type: "string",
82+
enum: ["mouth"],
83+
},
84+
},
85+
required: ["type"],
86+
},
87+
{
88+
type: "object",
89+
properties: {
90+
type: {
91+
type: "string",
92+
enum: ["eyes"],
93+
},
94+
default: {
95+
type: "string",
96+
},
97+
},
98+
required: ["type"],
99+
},
100+
],
101+
},
102+
{
103+
type: "object",
104+
properties: {
105+
description: {
106+
type: "string",
107+
description: "Description of the body part.",
108+
},
109+
},
110+
required: ["description"],
111+
},
112+
],
113+
},
114+
],
115+
[
116+
{
117+
type: "array",
118+
items: { type: "object", properties: { a: "string", b: "number" } },
119+
},
120+
],
121+
[{ type: "string" }],
122+
[{ type: "number" }],
123+
[{ type: "integer" }],
124+
[{ type: "boolean" }],
125+
[false],
126+
[true],
127+
[{}],
128+
] as [SchemaObject["additionalProperties"]][])(
129+
"should handle additionalProperties: %p",
130+
async (additionalProperties) => {
131+
const schema: SchemaObject = {
132+
type: "object",
133+
additionalProperties,
134+
};
135+
136+
expect(
137+
await Promise.all(
138+
createNodes(schema, "request").map(
139+
async (md: any) => await prettier.format(md, { parser: "babel" })
140+
)
141+
)
142+
).toMatchSnapshot();
143+
}
144+
);
145+
});
59146
});

0 commit comments

Comments
 (0)