Skip to content

Commit

Permalink
Merge pull request nestjs#1174 from jiqiang90/fix-api-extension
Browse files Browse the repository at this point in the history
fix(api-extension): Handle correct array value
  • Loading branch information
kamilmysliwiec authored Mar 19, 2021
2 parents 1129aae + eb4916a commit 7703755
Showing 4 changed files with 14 additions and 4 deletions.
6 changes: 6 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
@@ -309,6 +309,12 @@
},
"get": {
"operationId": "CatsController_findAll",
"x-codeSamples": [
{
"lang": "JavaScript",
"source": "console.log('Hello World');"
}
],
"parameters": [
{
"name": "header",
3 changes: 3 additions & 0 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -55,6 +55,9 @@ export class CatsController {
}

@Get()
@ApiExtension('x-codeSamples', [
{ lang: 'JavaScript', source: "console.log('Hello World');" }
])
findAll(@Query() paginationQuery: PaginationQuery) {}

@ApiQuery({ type: PaginationQuery })
3 changes: 3 additions & 0 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -46,6 +46,9 @@ describe('Validate OpenAPI schema', () => {
api.info.version
);
expect(api.info.title).toEqual('Cats example');
expect(api.paths['/api/cats']['get']['x-codeSamples'][0]['lang']).toEqual(
'JavaScript'
);
} catch (err) {
console.log(doc);
expect(err).toBeUndefined();
6 changes: 2 additions & 4 deletions lib/decorators/api-extension.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DECORATORS } from '../constants';
import { createMixedDecorator } from './helpers';
import { clone } from 'lodash';

export function ApiExtension(extensionKey: string, extensionProperties: any) {
if (!extensionKey.startsWith('x-')) {
@@ -9,10 +10,7 @@ export function ApiExtension(extensionKey: string, extensionProperties: any) {
}

const extensionObject = {
[extensionKey]:
typeof extensionProperties !== 'string'
? { ...extensionProperties }
: extensionProperties
[extensionKey]: clone(extensionProperties)
};

return createMixedDecorator(DECORATORS.API_EXTENSION, extensionObject);

0 comments on commit 7703755

Please sign in to comment.