Skip to content

Commit

Permalink
test: add more case
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Apr 8, 2024
1 parent b7de1ec commit 43c86ed
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/swagger/src/decorators/api-header.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DECORATORS } from '../constants';
import { SwaggerEnumType, ParameterObject, Type } from '../interfaces';
import { getEnumType, getEnumValues } from '../common/enum.utils';
import { createMixedDecorator, createParamDecorator } from './helpers';
import { createMixedDecorator } from './helpers';

export interface ApiHeaderOptions extends Omit<ParameterObject, 'in'> {
enum?: SwaggerEnumType;
Expand Down Expand Up @@ -37,7 +37,7 @@ export function ApiHeader(options: ApiHeaderOptions): any {
descriptor?: TypedPropertyDescriptor<any>
): any => {
if (descriptor) {
return createParamDecorator(param, defaultHeaderOptions as any)(
return createMixedDecorator(DECORATORS.API_HEADERS, param)(
target,
key,
descriptor
Expand Down
11 changes: 10 additions & 1 deletion packages/swagger/src/swaggerExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,19 @@ export class SwaggerExplorer {
parameters.push(p);
}
// class header 需要使用 ApiHeader 装饰器
if (headers) {
if (headers && headers.length) {
headers.forEach(header => parameters.unshift(header));
}

// 获取方法上的 @ApiHeader
const methodHeaders = metaForMethods.filter(
item => item.key === DECORATORS.API_HEADERS
);

if (methodHeaders.length > 0) {
methodHeaders.forEach(item => parameters.unshift(item.metadata));
}

opts[webRouter.requestMethod].parameters = parameters;

const responses = metaForMethods.filter(
Expand Down
132 changes: 131 additions & 1 deletion packages/swagger/test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,137 @@ exports[`/test/parser.test.ts should test ApiHeader in method 1`] = `
"get": {
"description": undefined,
"operationId": "apicontroller_getuser",
"parameters": [],
"parameters": [
{
"description": "this is test one",
"in": "header",
"name": "x-test-one",
"required": undefined,
"schema": {
"type": "string",
},
},
{
"description": "this is test two",
"in": "header",
"name": "x-test-two",
"required": undefined,
"schema": {
"type": "string",
},
},
],
"responses": {
"200": {
"description": "OK",
},
},
"summary": undefined,
"tags": [
"api",
],
},
},
},
"servers": [],
"tags": [
{
"description": "api",
"externalDocs": undefined,
"name": "api",
},
],
}
`;

exports[`/test/parser.test.ts should test ApiHeaders in class and method 1`] = `
{
"components": {},
"info": {
"contact": {},
"description": "",
"title": "",
"version": "1.0.0",
},
"openapi": "3.0.1",
"paths": {
"/api/get_user": {
"get": {
"description": undefined,
"operationId": "apicontroller_getuser",
"parameters": [
{
"description": "this is test four",
"in": "header",
"name": "x-test-four",
"required": undefined,
"schema": {
"type": "string",
},
},
{
"description": "this is test three",
"in": "header",
"name": "x-test-three",
"required": undefined,
"schema": {
"type": "string",
},
},
{
"description": "this is test two",
"in": "header",
"name": "x-test-two",
"required": undefined,
"schema": {
"type": "string",
},
},
{
"description": "this is test one",
"in": "header",
"name": "x-test-one",
"required": undefined,
"schema": {
"type": "string",
},
},
],
"responses": {
"200": {
"description": "OK",
},
},
"summary": undefined,
"tags": [
"api",
],
},
},
"/api/get_user2": {
"get": {
"description": undefined,
"operationId": "apicontroller_anothermethod",
"parameters": [
{
"description": "this is test two",
"in": "header",
"name": "x-test-two",
"required": undefined,
"schema": {
"type": "string",
},
},
{
"description": "this is test one",
"in": "header",
"name": "x-test-one",
"required": undefined,
"schema": {
"type": "string",
},
},
],
"responses": {
"200": {
"description": "OK",
Expand Down
50 changes: 48 additions & 2 deletions packages/swagger/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {
ApiBody,
ApiExcludeController,
ApiExcludeEndpoint,
ApiExcludeSecurity, ApiExtension,
ApiExtraModel, ApiHeader,
ApiExcludeSecurity,
ApiExtension,
ApiExtraModel,
ApiHeader,
ApiHeaders,
ApiOperation,
ApiProperty,
ApiResponse,
Expand Down Expand Up @@ -596,6 +599,10 @@ describe('/test/parser.test.ts', function () {
name: 'x-test-one',
description: 'this is test one'
})
@ApiHeader({
name: 'x-test-two',
description: 'this is test two'
})
async getUser() {
// ...
}
Expand All @@ -605,4 +612,43 @@ describe('/test/parser.test.ts', function () {
explorer.generatePath(APIController);
expect(explorer.getData()).toMatchSnapshot();
});

it('should test ApiHeaders in class and method', () => {
@Controller('/api')
@ApiHeaders([
{
name: 'x-test-one',
description: 'this is test one'
},
{
name: 'x-test-two',
description: 'this is test two'
}
])
class APIController {
@Get('/get_user')
@ApiHeaders([
{
name: 'x-test-three',
description: 'this is test three'
},
{
name: 'x-test-four',
description: 'this is test four'
}
])
async getUser() {
// ...
}

@Get('/get_user2')
async anotherMethod(){
// ...
}
}

const explorer = new CustomSwaggerExplorer();
explorer.generatePath(APIController);
expect(explorer.getData()).toMatchSnapshot();
});
});

0 comments on commit 43c86ed

Please sign in to comment.