Skip to content

Commit 106cbbd

Browse files
authored
Add support for examples in OpenAPI block (#2314)
1 parent 56e5c3c commit 106cbbd

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

packages/react-openapi/src/OpenAPISchema.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ export function OpenAPISchemaProperty(
4747
? null
4848
: getSchemaAlternatives(schema, new Set(circularRefs.keys()));
4949

50+
const shouldDisplayExample = (schema: OpenAPIV3.SchemaObject): boolean => {
51+
return (typeof schema.example === 'string' || typeof schema.example === 'number' || typeof schema.example === 'boolean')
52+
}
5053
return (
5154
<InteractiveSection
5255
id={id}
@@ -92,6 +95,9 @@ export function OpenAPISchemaProperty(
9295
className="openapi-schema-description"
9396
/>
9497
) : null}
98+
{shouldDisplayExample(schema) ? (
99+
<span className="openapi-schema-example">Example: <code>{JSON.stringify(schema.example)}</code></span>
100+
) : null}
95101
</div>
96102
}
97103
>

packages/react-openapi/src/OpenAPISpec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export function OpenAPISpec(props: { rawData: any; context: OpenAPIClientContext
4848
// Description of the parameter is defined at the parameter level
4949
// we use display it if the schema doesn't override it
5050
description: parameter.description,
51+
example: parameter.example,
5152
...(noReference(parameter.schema) ?? {}),
5253
},
5354
required: parameter.required,

src/components/DocumentView/OpenAPI/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@
172172
@apply prose-sm;
173173
}
174174

175+
.openapi-schema-example {
176+
@apply prose-sm mt-2 text-dark/10 dark:text-light/10;
177+
}
178+
175179
/** Authentication */
176180

177181
.openapi-securities {

0 commit comments

Comments
 (0)