Skip to content

Commit ae3a1c5

Browse files
authored
Improve support for additional properties, cleanup nested <li>, support SchemaItem children (#563)
1 parent 98d4b9c commit ae3a1c5

File tree

4 files changed

+88
-101
lines changed

4 files changed

+88
-101
lines changed

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

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function mergeAllOf(allOf: SchemaObject[]) {
5353
*/
5454
function createAnyOneOf(schema: SchemaObject): any {
5555
const type = schema.oneOf ? "oneOf" : "anyOf";
56-
return create("li", {
56+
return create("div", {
5757
children: [
5858
create("span", {
5959
className: "badge badge--info",
@@ -68,14 +68,17 @@ function createAnyOneOf(schema: SchemaObject): any {
6868

6969
if (anyOneSchema.properties !== undefined) {
7070
anyOneChildren.push(createProperties(anyOneSchema));
71+
delete anyOneSchema.properties;
7172
}
7273

7374
if (anyOneSchema.allOf !== undefined) {
7475
anyOneChildren.push(createNodes(anyOneSchema));
76+
delete anyOneSchema.allOf;
7577
}
7678

7779
if (anyOneSchema.items !== undefined) {
7880
anyOneChildren.push(createItems(anyOneSchema));
81+
delete anyOneSchema.items;
7982
}
8083

8184
if (
@@ -159,6 +162,18 @@ function createAdditionalProperties(schema: SchemaObject) {
159162
// }
160163
const additionalProperties = schema.additionalProperties;
161164
const type: string | unknown = additionalProperties?.type;
165+
// Handle free-form objects
166+
if (String(additionalProperties) === "true" && schema.type === "object") {
167+
return create("SchemaItem", {
168+
name: "property name*",
169+
required: false,
170+
schemaName: "any",
171+
qualifierMessage: getQualifierMessage(schema.additionalProperties),
172+
schema: schema,
173+
collapsible: false,
174+
discriminator: false,
175+
});
176+
}
162177
if (
163178
(type === "object" || type === "array") &&
164179
(additionalProperties?.properties ||
@@ -168,12 +183,12 @@ function createAdditionalProperties(schema: SchemaObject) {
168183
additionalProperties?.oneOf ||
169184
additionalProperties?.anyOf)
170185
) {
171-
const title = additionalProperties.title;
172-
const schemaName = title ? `object (${title})` : "object";
186+
const title = additionalProperties.title as string;
187+
const schemaName = getSchemaName(additionalProperties);
173188
const required = schema.required ?? false;
174189
return createDetailsNode(
175190
"property name*",
176-
schemaName,
191+
title ?? schemaName,
177192
additionalProperties,
178193
required,
179194
schema.nullable
@@ -191,51 +206,30 @@ function createAdditionalProperties(schema: SchemaObject) {
191206
schema.additionalProperties?.additionalProperties;
192207
if (additionalProperties !== undefined) {
193208
const type = schema.additionalProperties?.additionalProperties?.type;
194-
const format = schema.additionalProperties?.additionalProperties?.format;
195-
return create("li", {
196-
children: create("div", {
197-
children: [
198-
create("code", { children: `property name*` }),
199-
guard(type, (type) =>
200-
create("span", {
201-
style: { opacity: "0.6" },
202-
children: ` ${type}`,
203-
})
204-
),
205-
guard(format, (format) =>
206-
create("span", {
207-
style: { opacity: "0.6" },
208-
children: ` (${format})`,
209-
})
210-
),
211-
guard(getQualifierMessage(schema.additionalProperties), (message) =>
212-
create("div", {
213-
style: { marginTop: "var(--ifm-table-cell-padding)" },
214-
children: createDescription(message),
215-
})
216-
),
217-
],
218-
}),
209+
const schemaName = getSchemaName(
210+
schema.additionalProperties?.additionalProperties!
211+
);
212+
return create("SchemaItem", {
213+
name: "property name*",
214+
required: false,
215+
schemaName: schemaName ?? type,
216+
qualifierMessage:
217+
schema.additionalProperties ??
218+
getQualifierMessage(schema.additionalProperties),
219+
schema: schema,
220+
collapsible: false,
221+
discriminator: false,
219222
});
220223
}
221-
return create("li", {
222-
children: create("div", {
223-
children: [
224-
create("code", { children: `property name*` }),
225-
guard(type, (type) =>
226-
create("span", {
227-
style: { opacity: "0.6" },
228-
children: ` ${type}`,
229-
})
230-
),
231-
guard(getQualifierMessage(schema.additionalProperties), (message) =>
232-
create("div", {
233-
style: { marginTop: "var(--ifm-table-cell-padding)" },
234-
children: createDescription(message),
235-
})
236-
),
237-
],
238-
}),
224+
const schemaName = getSchemaName(schema.additionalProperties!);
225+
return create("SchemaItem", {
226+
name: "property name*",
227+
required: false,
228+
schemaName: schemaName,
229+
qualifierMessage: getQualifierMessage(schema),
230+
schema: schema.additionalProperties,
231+
collapsible: false,
232+
discriminator: false,
239233
});
240234
}
241235
return Object.entries(schema.additionalProperties!).map(([key, val]) =>

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

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ export function mergeAllOf(allOf: SchemaObject[]) {
5858
*/
5959
function createAnyOneOf(schema: SchemaObject): any {
6060
const type = schema.oneOf ? "oneOf" : "anyOf";
61-
return create("li", {
61+
return create("div", {
6262
children: [
6363
create("span", {
6464
className: "badge badge--info",
6565
children: type,
6666
}),
6767
create("SchemaTabs", {
68-
className: "openapi-tabs__schema",
6968
children: schema[type]!.map((anyOneSchema, index) => {
7069
const label = anyOneSchema.title
7170
? anyOneSchema.title
@@ -74,14 +73,17 @@ function createAnyOneOf(schema: SchemaObject): any {
7473

7574
if (anyOneSchema.properties !== undefined) {
7675
anyOneChildren.push(createProperties(anyOneSchema));
76+
delete anyOneSchema.properties;
7777
}
7878

7979
if (anyOneSchema.allOf !== undefined) {
8080
anyOneChildren.push(createNodes(anyOneSchema));
81+
delete anyOneSchema.allOf;
8182
}
8283

8384
if (anyOneSchema.items !== undefined) {
8485
anyOneChildren.push(createItems(anyOneSchema));
86+
delete anyOneSchema.items;
8587
}
8688

8789
if (
@@ -92,7 +94,6 @@ function createAnyOneOf(schema: SchemaObject): any {
9294
) {
9395
anyOneChildren.push(createNodes(anyOneSchema));
9496
}
95-
9697
if (anyOneChildren.length) {
9798
if (schema.type === "array") {
9899
return create("TabItem", {
@@ -110,7 +111,7 @@ function createAnyOneOf(schema: SchemaObject): any {
110111
return create("TabItem", {
111112
label: label,
112113
value: `${index}-item-properties`,
113-
children: anyOneChildren,
114+
children: anyOneChildren.filter(Boolean).flat(),
114115
});
115116
}
116117

@@ -166,6 +167,18 @@ function createAdditionalProperties(schema: SchemaObject) {
166167
// }
167168
const additionalProperties = schema.additionalProperties;
168169
const type: string | unknown = additionalProperties?.type;
170+
// Handle free-form objects
171+
if (String(additionalProperties) === "true" && schema.type === "object") {
172+
return create("SchemaItem", {
173+
name: "property name*",
174+
required: false,
175+
schemaName: "any",
176+
qualifierMessage: getQualifierMessage(schema.additionalProperties),
177+
schema: schema,
178+
collapsible: false,
179+
discriminator: false,
180+
});
181+
}
169182
if (
170183
(type === "object" || type === "array") &&
171184
(additionalProperties?.properties ||
@@ -175,12 +188,12 @@ function createAdditionalProperties(schema: SchemaObject) {
175188
additionalProperties?.oneOf ||
176189
additionalProperties?.anyOf)
177190
) {
178-
const title = additionalProperties.title;
179-
const schemaName = title ? `object (${title})` : "object";
191+
const title = additionalProperties.title as string;
192+
const schemaName = getSchemaName(additionalProperties);
180193
const required = schema.required ?? false;
181194
return createDetailsNode(
182195
"property name*",
183-
schemaName,
196+
title ?? schemaName,
184197
additionalProperties,
185198
required,
186199
schema.nullable
@@ -198,51 +211,30 @@ function createAdditionalProperties(schema: SchemaObject) {
198211
schema.additionalProperties?.additionalProperties;
199212
if (additionalProperties !== undefined) {
200213
const type = schema.additionalProperties?.additionalProperties?.type;
201-
const format = schema.additionalProperties?.additionalProperties?.format;
202-
return create("li", {
203-
children: create("div", {
204-
children: [
205-
create("code", { children: `property name*` }),
206-
guard(type, (type) =>
207-
create("span", {
208-
style: { opacity: "0.6" },
209-
children: ` ${type}`,
210-
})
211-
),
212-
guard(format, (format) =>
213-
create("span", {
214-
style: { opacity: "0.6" },
215-
children: ` (${format})`,
216-
})
217-
),
218-
guard(getQualifierMessage(schema.additionalProperties), (message) =>
219-
create("div", {
220-
style: { marginTop: "var(--ifm-table-cell-padding)" },
221-
children: createDescription(message),
222-
})
223-
),
224-
],
225-
}),
214+
const schemaName = getSchemaName(
215+
schema.additionalProperties?.additionalProperties!
216+
);
217+
return create("SchemaItem", {
218+
name: "property name*",
219+
required: false,
220+
schemaName: schemaName ?? type,
221+
qualifierMessage:
222+
schema.additionalProperties ??
223+
getQualifierMessage(schema.additionalProperties),
224+
schema: schema,
225+
collapsible: false,
226+
discriminator: false,
226227
});
227228
}
228-
return create("li", {
229-
children: create("div", {
230-
children: [
231-
create("code", { children: `property name*` }),
232-
guard(type, (type) =>
233-
create("span", {
234-
style: { opacity: "0.6" },
235-
children: ` ${type}`,
236-
})
237-
),
238-
guard(getQualifierMessage(schema.additionalProperties), (message) =>
239-
create("div", {
240-
style: { marginTop: "var(--ifm-table-cell-padding)" },
241-
children: createDescription(message),
242-
})
243-
),
244-
],
245-
}),
229+
const schemaName = getSchemaName(schema.additionalProperties!);
230+
return create("SchemaItem", {
231+
name: "property name*",
232+
required: false,
233+
schemaName: schemaName,
234+
qualifierMessage: getQualifierMessage(schema),
235+
schema: schema.additionalProperties,
236+
collapsible: false,
237+
discriminator: false,
246238
});
247239
}
248240
return Object.entries(schema.additionalProperties!).map(([key, val]) =>

packages/docusaurus-theme-openapi-docs/src/theme/ParamsItem/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function ParamsItem({
119119
});
120120

121121
return (
122-
<li className="openapi-params__list-item">
122+
<div className="openapi-params__list-item">
123123
<span className="openapi-schema__container">
124124
<strong>{name}</strong>
125125
{renderSchemaName}
@@ -131,7 +131,7 @@ function ParamsItem({
131131
{renderDescription}
132132
{renderExample}
133133
{renderExamples}
134-
</li>
134+
</div>
135135
);
136136
}
137137

packages/docusaurus-theme-openapi-docs/src/theme/SchemaItem/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ function SchemaItem({
110110
{renderQualifierMessage}
111111
{renderDefaultValue}
112112
{renderSchemaDescription}
113+
{collapsibleSchemaContent ?? collapsibleSchemaContent}
113114
</div>
114115
);
115116

116117
return (
117-
<li className="openapi-schema__list-item">
118+
<div className="openapi-schema__list-item">
118119
{collapsible ? collapsibleSchemaContent : schemaContent}
119-
</li>
120+
</div>
120121
);
121122
}
122123

0 commit comments

Comments
 (0)