question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@ApiProperty 's operation is not correct on OpenApi, Swagger queries are not sending

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Minimum reproduction code

https://github.com/p29hieu-sub-projects/nestjs-swagger

Steps to reproduce

Proplem: When i use ApiProperty decorator from nestjs/swagger, i can’t use them on OpenApi.

I created a dto file like:

export class GetProductsDto implements IGetProductsDto {
 @ApiProperty({
   example: 37.7752315,
 })
 @IsNotEmpty()
 latitude: number;

 @ApiProperty({
   example: -122.418075,
 })
 @IsNotEmpty()
 longitude: number;
}

then, i used it:

 @ApiQuery({
      type: GetProductsDto,
    })
  @Get("products")
  async getProducts(@Query() query: GetProductsDto): Promise<any> {
    return {
      data: [],
    };
  }

In my browser: image

When i press Try it out and Excecute buttons: image

Input fields have been filled but the error still occurs.

When i use required: false in ApiProperty

export class GetProductsDto implements IGetProductsDto {
 @ApiProperty({
   example: 37.7752315,
   required: false
 })
 @IsNotEmpty()
 latitude: number;

 @ApiProperty({
   example: -122.418075,
   required: false
 })
 @IsNotEmpty()
 longitude: number;
}

Then I press Try it out and Excecute buttons again, In Curl, the params is not true:

image

Please give me some help. Thanks!

  @nestjs/cli: "^8.0.0",
  OS: ubuntu 20.04
 Browser: Microsoft Edge Version 94.0.992.38 

Package version

^5.0.9

NestJS version

^8.0.0

Node.js version

14.17.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Oct 5, 2021

Please, report this issue in the corresponding repository https://github.com/swagger-api/swagger-ui

0reactions
mathis-mcommented, Oct 8, 2021

Workarround, put this in your main.ts it will make the parameters distinct:

const document = SwaggerModule.createDocument(app, options);
const distinctParams : (arr: any) => (ParameterObject | ReferenceObject)[] = 
  (arr) => [...new Map(arr.map(item => [item.in + item.name + item.$ref, item])).values()] as (ParameterObject | ReferenceObject)[]
for (const path in document.paths || {}) {
  const pathObj = document.paths[path]
  const methodParams = [
    {key: "get", params: pathObj.get?.parameters},
    {key: "put", params: pathObj.put?.parameters},
    {key: "post", params: pathObj.post?.parameters},
    {key: "delete", params: pathObj.delete?.parameters},
    {key: "options", params: pathObj.options?.parameters},
    {key: "head", params: pathObj.head?.parameters},
    {key: "patch", params: pathObj.patch?.parameters},
    {key: "trace", params: pathObj.trace?.parameters},
  ]
  if(pathObj.parameters) {
    document.paths[path].parameters = distinctParams(document.paths[path].parameters)
  }
  methodParams.forEach((x) => {
    if(x.params) {
      document.paths[path][x.key].parameters = distinctParams(x.params)
    }
  })
}

SwaggerModule.setup('/docs', app, document);

_Originally posted by @mathis-m in https://github.com/swagger-api/swagger-ui/issues/7539#issuecomment-938268189_

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - Version 3.0.3 - Swagger
If the servers property is not provided, or is an empty array, the default value would be a Server Object with a url...
Read more >
API with NestJS #60. The OpenAPI specification and Swagger
The OpenAPI is a specification used to describe our API and gives us a way to provide the details of our endpoints. It...
Read more >
Fix Swagger Validator errors in Power Platform connectors
The operation is not valid, it must contain at least one response definition. Your swagger has an operation that doesn't have at least...
Read more >
OpenAPI (Swagger) | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >
Nest + Swagger not recognizing endpoints or parameters
My understanding is that methods will only fail to get unique operationId 's when they are not sufficiently unique: 2 methods with identical ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found