Skip to content

Commit 91c5dcc

Browse files
sserrataentropitor
andauthored
feat: support schema.examples array in example generator (#1270)
Adds support for the OpenAPI 3.1 / JSON Schema 'examples' array in the schema example generator (createSchemaExample.ts): - Check schema.examples after schema.example in sampleFromSchema - Check property.examples after property.example in sampleFromProp - Uses first example from array when examples is non-empty - Guards against empty arrays to prevent fallback issues Closes #1219 Co-authored-by: Jens Claes <[email protected]>
1 parent 98c3f4a commit 91c5dcc

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ function sampleFromProp(
9191

9292
// TODO: handle discriminators
9393

94-
if (prop.oneOf) {
94+
// Check for explicit example/examples first (OAS 3.1 support)
95+
if (prop.example !== undefined) {
96+
obj[name] = prop.example;
97+
} else if (prop.examples !== undefined && prop.examples.length > 0) {
98+
obj[name] = prop.examples[0];
99+
} else if (prop.oneOf) {
95100
obj[name] = sampleFromSchema(prop.oneOf[0], context);
96101
} else if (prop.anyOf) {
97102
obj[name] = sampleFromSchema(prop.anyOf[0], context);
@@ -114,6 +119,7 @@ export const sampleFromSchema = (
114119
let {
115120
type,
116121
example,
122+
examples,
117123
allOf,
118124
properties,
119125
items,
@@ -126,6 +132,11 @@ export const sampleFromSchema = (
126132
return example;
127133
}
128134

135+
// OAS 3.1 / JSON Schema: examples is an array
136+
if (examples !== undefined && examples.length > 0) {
137+
return examples[0];
138+
}
139+
129140
if (oneOf) {
130141
if (properties) {
131142
const combinedSchemas = merge(schemaCopy, oneOf[0]);

0 commit comments

Comments
 (0)