Skip to content

Commit

Permalink
fix(): allow setting default value in api header decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 22, 2020
1 parent 7b77202 commit e1174b5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
7 changes: 6 additions & 1 deletion e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import { PaginationQuery } from './dto/pagination-query.dto';
@ApiSecurity('basic')
@ApiBearerAuth()
@ApiTags('cats')
@ApiHeader({ name: 'header', required: false, description: 'Test' })
@ApiHeader({
name: 'header',
required: false,
description: 'Test',
schema: { default: 'test' }
})
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
Expand Down
24 changes: 14 additions & 10 deletions lib/decorators/api-header.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil } from 'lodash';
import { isNil, isUndefined, negate, pickBy } from 'lodash';
import { DECORATORS } from '../constants';
import {
ParameterLocation,
Expand All @@ -17,15 +17,19 @@ const defaultHeaderOptions: Partial<ApiHeaderOptions> = {
};

export function ApiHeader(options: ApiHeaderOptions): any {
const param: ApiHeaderOptions & { in: ParameterLocation } = {
name: isNil(options.name) ? defaultHeaderOptions.name : options.name,
in: 'header',
description: options.description,
required: options.required,
schema: {
type: 'string'
}
};
const param = pickBy<ApiHeaderOptions & { in: ParameterLocation }>(
{
name: isNil(options.name) ? defaultHeaderOptions.name : options.name,
in: 'header',
description: options.description,
required: options.required,
schema: {
...(options.schema || {}),
type: 'string'
}
},
negate(isUndefined)
);

if (options.enum) {
const enumValues = getEnumValues(options.enum);
Expand Down
7 changes: 6 additions & 1 deletion test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,10 @@ describe('SwaggerExplorer', () => {

@ApiHeader({
name: 'Authorization',
description: 'auth token'
description: 'auth token',
schema: {
default: 'default token'
}
})
@Controller('')
class FooController {
Expand Down Expand Up @@ -758,6 +761,7 @@ describe('SwaggerExplorer', () => {
name: 'Authorization',
in: 'header',
schema: {
default: 'default token',
type: 'string'
}
},
Expand All @@ -776,6 +780,7 @@ describe('SwaggerExplorer', () => {
name: 'Authorization',
in: 'header',
schema: {
default: 'default token',
type: 'string'
}
}
Expand Down

0 comments on commit e1174b5

Please sign in to comment.