Skip to content

Commit

Permalink
Add response example
Browse files Browse the repository at this point in the history
  • Loading branch information
lit26 committed Mar 11, 2024
1 parent feca721 commit 713c48c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/decorators/api-response.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ApiResponseMetadata
type?: Type<unknown> | Function | [Function] | string;
isArray?: boolean;
description?: string;
example?: any;
}

export interface ApiResponseSchemaHost
Expand Down
20 changes: 13 additions & 7 deletions lib/services/response-object-mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { omit } from 'lodash';
import { ApiResponseSchemaHost } from '../decorators';
import { ApiResponseMetadata, ApiResponseSchemaHost } from '../decorators';
import { getSchemaPath } from '../utils';
import { MimetypeContentWrapper } from './mimetype-content-wrapper';

Expand All @@ -19,7 +19,8 @@ export class ResponseObjectMapper {
items: {
$ref: getSchemaPath(name)
}
}
},
...(response.example ? { example: response.example } : {})
})
};
}
Expand All @@ -30,20 +31,25 @@ export class ResponseObjectMapper {
...this.mimetypeContentWrapper.wrap(produces, {
schema: {
$ref: getSchemaPath(name)
}
},
...(response.example ? { example: response.example } : {})
})
};
}

wrapSchemaWithContent(response: ApiResponseSchemaHost, produces: string[]) {
if (!response.schema) {
wrapSchemaWithContent(
response: ApiResponseSchemaHost & ApiResponseMetadata,
produces: string[]
) {
if (!response.schema && !response.example) {
return response;
}
const content = this.mimetypeContentWrapper.wrap(produces, {
schema: response.schema
...(response.schema ? { schema: response.schema } : {}),
...(response.example ? { example: response.example } : {})
});
return {
...omit(response, 'schema'),
...omit(response, ['schema', 'example']),
...content
};
}
Expand Down
21 changes: 17 additions & 4 deletions test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ describe('SwaggerExplorer', () => {
@ApiOperation({ summary: 'Create foo' })
@ApiCreatedResponse({
type: Foo,
description: 'Newly created Foo object'
description: 'Newly created Foo object',
example: {
id: 'foo',
name: 'Foo'
}
})
create(
@Body() createFoo: CreateFoo,
Expand Down Expand Up @@ -138,7 +142,11 @@ describe('SwaggerExplorer', () => {
@Post('foos')
@ApiCreatedResponse({
type: Foo,
description: 'Newly created Foo object'
description: 'Newly created Foo object',
example: {
id: 'foo',
name: 'Foo'
}
})
create(
@Body() createFoo: CreateFoo,
Expand All @@ -158,7 +166,11 @@ describe('SwaggerExplorer', () => {
static [METADATA_FACTORY_NAME]() {
return {
create: {
summary: 'Create foo'
summary: 'Create foo',
example: {
id: 'foo',
name: 'Foo'
}
},
find: {
summary: 'List all Foos',
Expand Down Expand Up @@ -301,7 +313,8 @@ describe('SwaggerExplorer', () => {
).toEqual({
schema: {
$ref: '#/components/schemas/Foo'
}
},
example: { id: 'foo', name: 'Foo' }
});

// GET
Expand Down

0 comments on commit 713c48c

Please sign in to comment.