Skip to content

Commit 56622e8

Browse files
authoredJul 1, 2024··
Merge pull request #2871 from lit26/feature/add-response-example
feat: add response example
2 parents d675211 + ff2cd2e commit 56622e8

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed
 

‎lib/decorators/api-response.decorator.ts

+1
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/mimetype-content-wrapper.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { ContentObject } from '../interfaces/open-api-spec.interface';
22

3+
function removeUndefinedKeys(obj: { [x: string]: any }) {
4+
Object.entries(obj).forEach(([key, value]) => {
5+
if (value === undefined) {
6+
delete obj[key];
7+
}
8+
});
9+
return obj;
10+
}
11+
312
export class MimetypeContentWrapper {
413
wrap(
514
mimetype: string[],
615
obj: Record<string, any>
716
): Record<'content', ContentObject> {
817
const content = mimetype.reduce(
9-
(acc, item) => ({ ...acc, [item]: obj }),
18+
(acc, item) => ({ ...acc, [item]: removeUndefinedKeys(obj) }),
1019
{}
1120
);
1221
return { content };

‎lib/services/response-object-mapper.ts

+13-7
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+
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+
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+
schema: response.schema,
49+
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

+17-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ describe('SwaggerExplorer', () => {
103103
@ApiOperation({ summary: 'Create foo' })
104104
@ApiCreatedResponse({
105105
type: Foo,
106-
description: 'Newly created Foo object'
106+
description: 'Newly created Foo object',
107+
example: {
108+
id: 'foo',
109+
name: 'Foo'
110+
}
107111
})
108112
create(
109113
@Body() createFoo: CreateFoo,
@@ -144,7 +148,11 @@ describe('SwaggerExplorer', () => {
144148
@Post('foos')
145149
@ApiCreatedResponse({
146150
type: Foo,
147-
description: 'Newly created Foo object'
151+
description: 'Newly created Foo object',
152+
example: {
153+
id: 'foo',
154+
name: 'Foo'
155+
}
148156
})
149157
create(
150158
@Body() createFoo: CreateFoo,
@@ -164,7 +172,11 @@ describe('SwaggerExplorer', () => {
164172
static [METADATA_FACTORY_NAME]() {
165173
return {
166174
create: {
167-
summary: 'Create foo'
175+
summary: 'Create foo',
176+
example: {
177+
id: 'foo',
178+
name: 'Foo'
179+
}
168180
},
169181
find: {
170182
summary: 'List all Foos',
@@ -315,7 +327,8 @@ describe('SwaggerExplorer', () => {
315327
).toEqual({
316328
schema: {
317329
$ref: '#/components/schemas/Foo'
318-
}
330+
},
331+
example: { id: 'foo', name: 'Foo' }
319332
});
320333

321334
// GET

0 commit comments

Comments
 (0)
Please sign in to comment.