Skip to content

Commit fa0c6ff

Browse files
committed
Merge branch 'main' into change-primitive-anyOf-label
2 parents d5fe67b + 8997017 commit fa0c6ff

File tree

8 files changed

+328
-93
lines changed

8 files changed

+328
-93
lines changed

demo/examples/tests/allOf.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,76 @@ paths:
243243
items:
244244
$ref: "#/components/schemas/Book"
245245

246+
/allof-with-properties-in-array-item:
247+
get:
248+
tags:
249+
- allOf
250+
summary: allOf with Properties in Array Item
251+
description: |
252+
A list of books demonstrating allOf with properties in array item.
253+
254+
Schema:
255+
```yaml
256+
type: array
257+
items:
258+
$ref: '#/components/schemas/Book'
259+
```
260+
261+
Schema Components:
262+
```yaml
263+
BookBase:
264+
type: object
265+
required:
266+
- id
267+
- title
268+
- author
269+
properties:
270+
id:
271+
type: integer
272+
format: int64
273+
description: Unique identifier for the book
274+
title:
275+
type: string
276+
description: The title of the book
277+
author:
278+
type: string
279+
description: The author of the book
280+
281+
AdditionalBookInfo:
282+
type: object
283+
properties:
284+
publishedDate:
285+
type: string
286+
format: date
287+
description: The date the book was published
288+
genre:
289+
type: string
290+
description: The genre of the book
291+
tags:
292+
type: array
293+
items:
294+
type: string
295+
description: Tags associated with the book
296+
297+
CategorizedBook:
298+
allOf:
299+
- $ref: '#/components/schemas/BookBase'
300+
- $ref: '#/components/schemas/AdditionalBookInfo'
301+
properties:
302+
category:
303+
type: string
304+
description: The category of the book
305+
```
306+
responses:
307+
"200":
308+
description: A list of books
309+
content:
310+
application/json:
311+
schema:
312+
type: array
313+
items:
314+
$ref: "#/components/schemas/CategorizedBook"
315+
246316
/allof-nested:
247317
get:
248318
tags:
@@ -320,12 +390,15 @@ components:
320390
type: integer
321391
format: int64
322392
description: Unique identifier for the book
393+
example: 1234567890
323394
title:
324395
type: string
325396
description: The title of the book
397+
example: "The Great Gatsby"
326398
author:
327399
type: string
328400
description: The author of the book
401+
example: "F. Scott Fitzgerald"
329402

330403
AdditionalBookInfo:
331404
type: object
@@ -334,16 +407,29 @@ components:
334407
type: string
335408
format: date
336409
description: The date the book was published
410+
example: "2021-01-01"
337411
genre:
338412
type: string
339413
description: The genre of the book
414+
example: "Fiction"
340415
tags:
341416
type: array
342417
items:
343418
type: string
344419
description: Tags associated with the book
420+
example: ["Fiction", "Mystery"]
345421

346422
Book:
347423
allOf:
348424
- $ref: "#/components/schemas/BookBase"
349425
- $ref: "#/components/schemas/AdditionalBookInfo"
426+
427+
CategorizedBook:
428+
allOf:
429+
- $ref: "#/components/schemas/BookBase"
430+
- $ref: "#/components/schemas/AdditionalBookInfo"
431+
properties:
432+
category:
433+
type: string
434+
description: The category of the book
435+
example: "Fiction"

demo/examples/tests/anyOf.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,63 @@ paths:
6363
- type: boolean
6464
title: A boolean
6565
title: A string or integer, or a boolean
66+
67+
/anyof-with-properties-in-array-item:
68+
get:
69+
tags:
70+
- anyOf
71+
summary: anyOf with Properties in Array Item
72+
description: |
73+
Schema:
74+
```yaml
75+
type: array
76+
items:
77+
type: object
78+
anyOf:
79+
- type: object
80+
title: Item
81+
properties:
82+
orderNo:
83+
type: string
84+
example: "123456"
85+
- type: object
86+
title: Error
87+
properties:
88+
error:
89+
type: string
90+
example: "Invalid order number"
91+
properties:
92+
name:
93+
type: string
94+
example: pencil
95+
required:
96+
- orderNo
97+
```
98+
responses:
99+
"200":
100+
description: Successful response
101+
content:
102+
application/json:
103+
schema:
104+
type: array
105+
items:
106+
type: object
107+
anyOf:
108+
- type: object
109+
title: Item
110+
properties:
111+
orderNo:
112+
type: string
113+
example: "123456"
114+
- type: object
115+
title: Error
116+
properties:
117+
error:
118+
type: string
119+
example: "Invalid order number"
120+
properties:
121+
name:
122+
type: string
123+
example: pencil
124+
required:
125+
- orderNo

demo/examples/tests/oneOf.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,63 @@ paths:
264264
requiredPropB:
265265
type: number
266266
required: ["requiredPropB"]
267+
268+
/oneof-with-properties-in-array-item:
269+
get:
270+
tags:
271+
- oneOf
272+
summary: oneOf with Properties in Array Item
273+
description: |
274+
Schema:
275+
```yaml
276+
type: array
277+
items:
278+
type: object
279+
oneOf:
280+
- type: object
281+
title: Item
282+
properties:
283+
orderNo:
284+
type: string
285+
example: "123456"
286+
- type: object
287+
title: Error
288+
properties:
289+
error:
290+
type: string
291+
example: "Invalid order number"
292+
properties:
293+
name:
294+
type: string
295+
example: pencil
296+
required:
297+
- orderNo
298+
```
299+
responses:
300+
"200":
301+
description: Successful response
302+
content:
303+
application/json:
304+
schema:
305+
type: array
306+
items:
307+
type: object
308+
oneOf:
309+
- type: object
310+
title: Item
311+
properties:
312+
orderNo:
313+
type: string
314+
example: "123456"
315+
- type: object
316+
title: Error
317+
properties:
318+
error:
319+
type: string
320+
example: "Invalid order number"
321+
properties:
322+
name:
323+
type: string
324+
example: pencil
325+
required:
326+
- orderNo

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ export function createAuthentication(securitySchemes: SecuritySchemeObject) {
1313
if (!securitySchemes || !Object.keys(securitySchemes).length) return "";
1414

1515
const createAuthenticationTable = (securityScheme: any) => {
16-
const { bearerFormat, flows, name, scheme, type, openIdConnectUrl } =
17-
securityScheme;
16+
const {
17+
bearerFormat,
18+
flows,
19+
name,
20+
scheme,
21+
type,
22+
openIdConnectUrl,
23+
in: paramIn,
24+
} = securityScheme;
1825

1926
const createSecuritySchemeTypeRow = () =>
2027
create("tr", {
@@ -70,7 +77,9 @@ export function createAuthentication(securitySchemes: SecuritySchemeObject) {
7077
createSecuritySchemeTypeRow(),
7178
create("tr", {
7279
children: [
73-
create("th", { children: "Header parameter name:" }),
80+
create("th", {
81+
children: `${paramIn.charAt(0).toUpperCase() + paramIn.slice(1)} parameter name:`,
82+
}),
7483
create("td", { children: name }),
7584
],
7685
}),
@@ -142,7 +151,6 @@ export function createAuthentication(securitySchemes: SecuritySchemeObject) {
142151
return "";
143152
}
144153
};
145-
146154
const formatTabLabel = (key: string, type: string, scheme: string) => {
147155
const formattedLabel = key
148156
.replace(/(_|-)/g, " ")

packages/docusaurus-plugin-openapi-docs/src/openapi/createRequestExample.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ export const sampleRequestFromSchema = (schema: SchemaObject = {}): any => {
178178

179179
if (type === "array") {
180180
if (Array.isArray(items?.anyOf)) {
181-
return items?.anyOf.map((item: any) => sampleRequestFromSchema(item));
181+
return processArrayItems(items, "anyOf");
182182
}
183183

184184
if (Array.isArray(items?.oneOf)) {
185-
return items?.oneOf.map((item: any) => sampleRequestFromSchema(item));
185+
return processArrayItems(items, "oneOf");
186186
}
187187

188188
return normalizeArray(sampleRequestFromSchema(items));
@@ -237,3 +237,26 @@ function normalizeArray(arr: any) {
237237
}
238238
return [arr];
239239
}
240+
241+
function processArrayItems(
242+
items: SchemaObject,
243+
schemaType: "anyOf" | "oneOf"
244+
): any[] {
245+
const itemsArray = items[schemaType] as SchemaObject[];
246+
return itemsArray.map((item: SchemaObject) => {
247+
// If items has properties, merge them with each item
248+
if (items.properties) {
249+
const combinedSchema = {
250+
...item,
251+
properties: {
252+
...items.properties, // Common properties from parent
253+
...item.properties, // Specific properties from this anyOf/oneOf item
254+
},
255+
};
256+
// Remove anyOf/oneOf to prevent infinite recursion when calling sampleRequestFromSchema
257+
delete combinedSchema[schemaType];
258+
return sampleRequestFromSchema(combinedSchema);
259+
}
260+
return sampleRequestFromSchema(item);
261+
});
262+
}

packages/docusaurus-plugin-openapi-docs/src/openapi/createResponseExample.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ export const sampleResponseFromSchema = (schema: SchemaObject = {}): any => {
181181

182182
if (type === "array") {
183183
if (Array.isArray(items?.anyOf)) {
184-
return items?.anyOf.map((item: any) => sampleResponseFromSchema(item));
184+
return processArrayItems(items, "anyOf");
185185
}
186186

187187
if (Array.isArray(items?.oneOf)) {
188-
return items?.oneOf.map((item: any) => sampleResponseFromSchema(item));
188+
return processArrayItems(items, "oneOf");
189189
}
190190

191191
return [sampleResponseFromSchema(items)];
@@ -240,3 +240,26 @@ function normalizeArray(arr: any) {
240240
}
241241
return [arr];
242242
}
243+
244+
function processArrayItems(
245+
items: SchemaObject,
246+
schemaType: "anyOf" | "oneOf"
247+
): any[] {
248+
const itemsArray = items[schemaType] as SchemaObject[];
249+
return itemsArray.map((item: SchemaObject) => {
250+
// If items has properties, merge them with each item
251+
if (items.properties) {
252+
const combinedSchema = {
253+
...item,
254+
properties: {
255+
...items.properties, // Common properties from parent
256+
...item.properties, // Specific properties from this anyOf/oneOf item
257+
},
258+
};
259+
// Remove anyOf/oneOf to prevent infinite recursion when calling sampleResponseFromSchema
260+
delete combinedSchema[schemaType];
261+
return sampleResponseFromSchema(combinedSchema);
262+
}
263+
return sampleResponseFromSchema(item);
264+
});
265+
}

0 commit comments

Comments
 (0)