Skip to content

Commit 9ae6a7c

Browse files
add request and response examples
1 parent 5963a1f commit 9ae6a7c

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/Examples.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import CollapsingDetails from './CollapsingDetails';
2+
import Details from './Details';
3+
import { Example } from './metamodel';
4+
5+
type Props = {
6+
examples: Record<string, Example>;
7+
};
8+
9+
export default function Examples({ examples }: Props) {
10+
return (
11+
<CollapsingDetails header="Examples">
12+
{Object.keys(examples).map(example => (
13+
<CollapsingDetails header={example}>
14+
{examples[example].summary && <Details header="Summary" value={examples[example].summary} />}
15+
{examples[example].description && <Details header="Description" value={examples[example].description} />}
16+
{examples[example].value && <Details header="Value" value={examples[example].value} />}
17+
{examples[example].external_value && <Details header="External Value" value={examples[example].external_value} />}
18+
</CollapsingDetails>
19+
))}
20+
</CollapsingDetails>
21+
);
22+
}

src/Request.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Properties from './Properties';
55
import Body from './Body';
66
import Behaviors from './Behaviors';
77
import AttachedBehaviors from './AttachedBehaviors';
8+
import Examples from './Examples';
89

910
import { Request as RequestType } from './metamodel';
1011

@@ -28,6 +29,7 @@ export default function Request({ type }: Props) {
2829
<Body body={type.body} />
2930
{type.behaviors && <Behaviors behaviors={type.behaviors} />}
3031
{type.attachedBehaviors && <AttachedBehaviors behaviors={type.attachedBehaviors} />}
32+
{type.examples && <Examples examples={type.examples} />}
3133
</>
3234
);
3335
}

src/Response.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Body from './Body';
44
import Behaviors from './Behaviors';
55
import Description from './Description';
66
import AttachedBehaviors from './AttachedBehaviors';
7+
import Examples from './Examples';
78

89
import { Response as ResponseType } from './metamodel';
910

@@ -34,6 +35,7 @@ export default function Response({ type }: Props) {
3435
))}
3536
</CollapsingDetails>
3637
}
38+
{type.examples && <Examples examples={type.examples} />}
3739
</>
3840
);
3941
}

src/metamodel.ts

+19
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,23 @@ export class Interface extends BaseType {
260260
variants?: Container
261261
}
262262

263+
/**
264+
* The Example type is used for both requests and responses
265+
* This type definition is taken from the OpenAPI spec
266+
* https://spec.openapis.org/oas/v3.1.0#example-object
267+
* With the exception of using String as the 'value' type
268+
*/
269+
export class Example {
270+
/** Short description. */
271+
summary?: string
272+
/** Long description. */
273+
description?: string
274+
/** Embedded literal example. Mutually exclusive with `external_value` */
275+
value?: string
276+
/** A URI that points to the literal example */
277+
external_value?: string
278+
}
279+
263280
/**
264281
* A request type
265282
*/
@@ -288,6 +305,7 @@ export class Request extends BaseType {
288305
body: Body
289306
behaviors?: Behavior[]
290307
attachedBehaviors?: string[]
308+
examples?: Record<string, Example>
291309
}
292310

293311
/**
@@ -300,6 +318,7 @@ export class Response extends BaseType {
300318
behaviors?: Behavior[]
301319
attachedBehaviors?: string[]
302320
exceptions?: ResponseException[]
321+
examples?: Record<string, Example>
303322
}
304323

305324
export class ResponseException {

0 commit comments

Comments
 (0)