midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.38k stars 574 forks source link

swagger组件,@ApiQuery()装饰器仅能正确识别Controller路由方法中第一个参数为@Query()的情况 #1927

Closed Pt-11111 closed 2 years ago

Pt-11111 commented 2 years ago

示例

示例一:@Body()在@Query()前面,Body无法正常显示,Query多了参数

@Post('/create1', { summary: 'test' })
    @Validate()
    @ApiQuery({
        name: 'test'
    })
    async create1(@Body() userInfo: UserDTO, @Query() userInfo1: UserDTO): Promise<object> {
        console.log(this.ctx.request.query, userInfo1)
        return {
            success: true, message: 'OK', data: { userInfo, test: this.ctx.request.query }
        };
    }

image

示例二:参数顺序为@Param(),@Body(),@Query(),Param无法正常显示,Query自定义名字参数消失,多的参数和示例一一样,Body正常显示

@Post('/create1_1', { summary: 'test' })
    @Validate()
    @ApiQuery({
        name: 'test'
    })
    async create1_1(@Param() id: number, @Body() userInfo: UserDTO, @Query() userInfo1: UserDTO): Promise<object> {
        console.log(this.ctx.request.query, userInfo1)
        return {
            success: true, message: 'OK', data: { userInfo, test: this.ctx.request.query }
        };
    }

image

其他 @Query() 在第一个参数位置均能正常识别

@Post('/create2', { summary: 'test' })
    @Validate()
    @ApiQuery({
        name: 'test'
    })
    async create2(@Query() userInfo1: UserDTO, @Body() userInfo: UserDTO): Promise<object> {
        console.log(this.ctx.request.query, userInfo1)
        return {
            success: true, message: 'OK', data: { userInfo, test: this.ctx.request.query }
        };
    }

    @Post('/create3', { summary: 'test' })
    @Validate()
    async create3(@Body() userInfo: UserDTO): Promise<object> {

        return {
            success: true, message: 'OK', data: { userInfo }
        };
    }

    @Post('/create4', { summary: 'test' })
    @Validate()
    @ApiQuery({
        name: 'test'
    })
    async create4(@Query() userInfo1: UserDTO,): Promise<object> {
        console.log(this.ctx.request.query, userInfo1)
        return {
            success: true, message: 'OK', data: { test: this.ctx.request.query }
        };
    }

UserDTO

import { ApiProperty } from "@midwayjs/swagger";
import { Rule, RuleType } from "@midwayjs/validate";

export class UserDTO {
    @ApiProperty({
        example: 123, description: '用户ID'
    })
    @Rule(RuleType.number().required())
    id: number;

    @ApiProperty({ example: "Bo", description: '姓' })
    @Rule(RuleType.string().required())
    firstName: string;

    @ApiProperty({ example: "Pang", description: '名' })
    @Rule(RuleType.string().max(10))
    lastName: string;

    @ApiProperty({ example: 20, description: '年龄' })
    @Rule(RuleType.number().max(60))
    age: number;
}

// configuration.ts
@Configuration({
  imports: [
    koa,
    validate,
    {
      component: info,
      enabledEnvironment: ['local'],
    },
    {
      component: swagger,
      enabledEnvironment: ['local']
    }
  ],
  importConfigs: [join(__dirname, './config')]
})

//config.default.ts
 export default {
  // use for cookie sign key, should change to your own and keep security
  keys: '1647590420920_4786',
  koa: {
    port: 7001,
    globalPrefix: '/dev-api/agriculture-iot'
  },
  swagger: {
    title: 'xxx'
  }
} as MidwayConfig;
kurten commented 2 years ago

装饰器是按照顺序匹配对应的参数的,顺序不对,或者少了,都是不对的。

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.