Skip to content

Commit 713c48c

Browse files
committed
Add response example
1 parent feca721 commit 713c48c

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

lib/decorators/api-response.decorator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface ApiResponseMetadata
1414
type?: Type<unknown> | Function | [Function] | string;
1515
isArray?: boolean;
1616
description?: string;
17+
example?: any;
1718
}
1819

1920
export interface ApiResponseSchemaHost

lib/services/response-object-mapper.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { omit } from 'lodash';
2-
import { ApiResponseSchemaHost } from '../decorators';
2+
import { ApiResponseMetadata, ApiResponseSchemaHost } from '../decorators';
33
import { getSchemaPath } from '../utils';
44
import { MimetypeContentWrapper } from './mimetype-content-wrapper';
55

@@ -19,7 +19,8 @@ export class ResponseObjectMapper {
1919
items: {
2020
$ref: getSchemaPath(name)
2121
}
22-
}
22+
},
23+
...(response.example ? { example: response.example } : {})
2324
})
2425
};
2526
}
@@ -30,20 +31,25 @@ export class ResponseObjectMapper {
3031
...this.mimetypeContentWrapper.wrap(produces, {
3132
schema: {
3233
$ref: getSchemaPath(name)
33-
}
34+
},
35+
...(response.example ? { example: response.example } : {})
3436
})
3537
};
3638
}
3739

38-
wrapSchemaWithContent(response: ApiResponseSchemaHost, produces: string[]) {
39-
if (!response.schema) {
40+
wrapSchemaWithContent(
41+
response: ApiResponseSchemaHost & ApiResponseMetadata,
42+
produces: string[]
43+
) {
44+
if (!response.schema && !response.example) {
4045
return response;
4146
}
4247
const content = this.mimetypeContentWrapper.wrap(produces, {
43-
schema: response.schema
48+
...(response.schema ? { schema: response.schema } : {}),
49+
...(response.example ? { example: response.example } : {})
4450
});
4551
return {
46-
...omit(response, 'schema'),
52+
...omit(response, ['schema', 'example']),
4753
...content
4854
};
4955
}

test/explorer/swagger-explorer.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ describe('SwaggerExplorer', () => {
9797
@ApiOperation({ summary: 'Create foo' })
9898
@ApiCreatedResponse({
9999
type: Foo,
100-
description: 'Newly created Foo object'
100+
description: 'Newly created Foo object',
101+
example: {
102+
id: 'foo',
103+
name: 'Foo'
104+
}
101105
})
102106
create(
103107
@Body() createFoo: CreateFoo,
@@ -138,7 +142,11 @@ describe('SwaggerExplorer', () => {
138142
@Post('foos')
139143
@ApiCreatedResponse({
140144
type: Foo,
141-
description: 'Newly created Foo object'
145+
description: 'Newly created Foo object',
146+
example: {
147+
id: 'foo',
148+
name: 'Foo'
149+
}
142150
})
143151
create(
144152
@Body() createFoo: CreateFoo,
@@ -158,7 +166,11 @@ describe('SwaggerExplorer', () => {
158166
static [METADATA_FACTORY_NAME]() {
159167
return {
160168
create: {
161-
summary: 'Create foo'
169+
summary: 'Create foo',
170+
example: {
171+
id: 'foo',
172+
name: 'Foo'
173+
}
162174
},
163175
find: {
164176
summary: 'List all Foos',
@@ -301,7 +313,8 @@ describe('SwaggerExplorer', () => {
301313
).toEqual({
302314
schema: {
303315
$ref: '#/components/schemas/Foo'
304-
}
316+
},
317+
example: { id: 'foo', name: 'Foo' }
305318
});
306319

307320
// GET

0 commit comments

Comments
 (0)