Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add response example #2871

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
11 changes: 10 additions & 1 deletion lib/services/mimetype-content-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { ContentObject } from '../interfaces/open-api-spec.interface';

function removeUndefinedKeys(obj: { [x: string]: any }) {
Object.entries(obj).forEach(([key, value]) => {
if (value === undefined) {
delete obj[key];
}
});
return obj;
}

export class MimetypeContentWrapper {
wrap(
mimetype: string[],
obj: Record<string, any>
): Record<'content', ContentObject> {
const content = mimetype.reduce(
(acc, item) => ({ ...acc, [item]: obj }),
(acc, item) => ({ ...acc, [item]: removeUndefinedKeys(obj) }),
{}
);
return { content };
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)
}
}
},
example: response.example
})
};
}
Expand All @@ -30,20 +31,25 @@ export class ResponseObjectMapper {
...this.mimetypeContentWrapper.wrap(produces, {
schema: {
$ref: getSchemaPath(name)
}
},
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
schema: response.schema,
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