Skip to content

Commit 2a46f82

Browse files
committed
add support for rendering example to SchemaItem
1 parent 6f7b058 commit 2a46f82

File tree

1 file changed

+27
-0
lines changed
  • packages/docusaurus-theme-openapi-docs/src/theme/SchemaItem

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default function SchemaItem(props: Props) {
6666
let deprecated;
6767
let schemaDescription;
6868
let defaultValue: string | undefined;
69+
let example: string | undefined;
6970
let nullable;
7071
let enumDescriptions: [string, string][] = [];
7172

@@ -74,6 +75,7 @@ export default function SchemaItem(props: Props) {
7475
schemaDescription = schema.description;
7576
enumDescriptions = transformEnumDescriptions(schema["x-enumDescriptions"]);
7677
defaultValue = schema.default;
78+
example = schema.example;
7779
nullable = schema.nullable;
7880
}
7981

@@ -157,6 +159,30 @@ export default function SchemaItem(props: Props) {
157159
return undefined;
158160
}
159161

162+
function renderExample() {
163+
if (example !== undefined) {
164+
if (typeof example === "string") {
165+
return (
166+
<div>
167+
<strong>Example: </strong>
168+
<span>
169+
<code>{example}</code>
170+
</span>
171+
</div>
172+
);
173+
}
174+
return (
175+
<div>
176+
<strong>Example: </strong>
177+
<span>
178+
<code>{JSON.stringify(example)}</code>
179+
</span>
180+
</div>
181+
);
182+
}
183+
return undefined;
184+
}
185+
160186
const schemaContent = (
161187
<div>
162188
<span className="openapi-schema__container">
@@ -179,6 +205,7 @@ export default function SchemaItem(props: Props) {
179205
{renderEnumDescriptions}
180206
{renderQualifierMessage}
181207
{renderDefaultValue()}
208+
{renderExample()}
182209
{collapsibleSchemaContent ?? collapsibleSchemaContent}
183210
</div>
184211
);

0 commit comments

Comments
 (0)