Skip to content

Commit 09ac11f

Browse files
authored
feat(api-explorer): add OpenAPI 3.1 schema.examples support (#1269)
Adds support for the OpenAPI 3.1 schema.examples array in the API Explorer Body component. This is different from Media Type examples (which is an object with named examples) - schema.examples is an array of example values as per JSON Schema alignment in OAS 3.1. - Handle schema.examples for JSON content types - Handle schema.examples for XML content types - Add test cases for schema.examples Closes #1164 (alternative implementation)
1 parent 46b5124 commit 09ac11f

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Schema Examples API (OAS 3.1)
4+
description: Demonstrates OpenAPI 3.1 schema.examples support.
5+
version: 1.0.0
6+
tags:
7+
- name: schemaExamples
8+
description: OpenAPI 3.1 schema examples tests
9+
paths:
10+
/schema-examples-array:
11+
post:
12+
tags:
13+
- schemaExamples
14+
summary: Schema with examples array (OAS 3.1)
15+
description: |
16+
OpenAPI 3.1 allows `examples` at the schema level as an array of values.
17+
This is different from Media Type `examples` which is an object with named examples.
18+
19+
Schema:
20+
```yaml
21+
schema:
22+
type: object
23+
properties:
24+
name:
25+
type: string
26+
age:
27+
type: integer
28+
examples:
29+
- name: "John Doe"
30+
age: 30
31+
- name: "Jane Smith"
32+
age: 25
33+
```
34+
requestBody:
35+
description: Request body with schema.examples array
36+
required: true
37+
content:
38+
application/json:
39+
schema:
40+
type: object
41+
properties:
42+
name:
43+
type: string
44+
age:
45+
type: integer
46+
examples:
47+
- name: "John Doe"
48+
age: 30
49+
- name: "Jane Smith"
50+
age: 25
51+
responses:
52+
"200":
53+
description: Successful response
54+
content:
55+
application/json:
56+
schema:
57+
type: object
58+
properties:
59+
id:
60+
type: integer
61+
message:
62+
type: string
63+
examples:
64+
- id: 1
65+
message: "User created successfully"
66+
- id: 2
67+
message: "User updated successfully"
68+
69+
/schema-example-vs-examples:
70+
post:
71+
tags:
72+
- schemaExamples
73+
summary: Schema example vs examples comparison
74+
description: |
75+
Demonstrates the difference between:
76+
- `schema.example` (singular) - used for generated example
77+
- `schema.examples` (plural, OAS 3.1) - array of example values
78+
- `content.example` (Media Type level)
79+
- `content.examples` (Media Type level, named examples object)
80+
requestBody:
81+
description: Request with both schema.example and schema.examples
82+
required: true
83+
content:
84+
application/json:
85+
schema:
86+
type: object
87+
properties:
88+
status:
89+
type: string
90+
enum:
91+
- active
92+
- inactive
93+
- pending
94+
count:
95+
type: integer
96+
minimum: 0
97+
examples:
98+
- status: "active"
99+
count: 10
100+
- status: "inactive"
101+
count: 0
102+
- status: "pending"
103+
count: 5
104+
responses:
105+
"200":
106+
description: Successful response

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/index.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ function Body({
9696
const schema = requestBodyMetadata?.content?.[contentType]?.schema;
9797
const example = requestBodyMetadata?.content?.[contentType]?.example;
9898
const examples = requestBodyMetadata?.content?.[contentType]?.examples;
99+
// OpenAPI 3.1 / JSON Schema: schema.examples is an array of example values
100+
const schemaExamples = schema?.examples as any[] | undefined;
99101

100102
if (schema?.format === "binary") {
101103
return (
@@ -254,6 +256,17 @@ function Body({
254256
});
255257
}
256258
}
259+
// OpenAPI 3.1: schema.examples is an array of example values
260+
if (schemaExamples && Array.isArray(schemaExamples)) {
261+
schemaExamples.forEach((schemaExample, index) => {
262+
const body = JSON.stringify(schemaExample, null, 2);
263+
examplesBodies.push({
264+
label: `Example ${index + 1}`,
265+
body,
266+
summary: undefined,
267+
});
268+
});
269+
}
257270
language = "json";
258271
}
259272

@@ -299,6 +312,26 @@ function Body({
299312
});
300313
}
301314
}
315+
// OpenAPI 3.1: schema.examples is an array of example values
316+
if (schemaExamples && Array.isArray(schemaExamples)) {
317+
schemaExamples.forEach((schemaExample, index) => {
318+
let formattedXmlBody;
319+
try {
320+
formattedXmlBody = format(json2xml(schemaExample, ""), {
321+
indentation: " ",
322+
lineSeparator: "\n",
323+
collapseContent: true,
324+
});
325+
} catch {
326+
formattedXmlBody = json2xml(schemaExample);
327+
}
328+
examplesBodies.push({
329+
label: `Example ${index + 1}`,
330+
body: formattedXmlBody,
331+
summary: undefined,
332+
});
333+
});
334+
}
302335
language = "xml";
303336
}
304337

0 commit comments

Comments
 (0)