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.

Bug - Swagger - Properties from inner DTO are expanded

See original GitHub issue

Bug Report

Current behavior

I have an action that gets an object in the query. The object has an optional property which itself is an object with a required property. In the swagger UI, the inner required property shows as required incorrectly at the level action (without its parent property):

 class childObject {
  @swagger.ApiProperty({
    required: true,
    type: String,
  })
  requiredInnerProp!: string;
}

class parentObject {
  @swagger.ApiProperty({
    required: false,
    type: childObject,
  })
  optionalChildProp?: childObject;
}

 @common.Get("/action")
  async getWithChildObjectInQuery(
    @common.Query() query: parentObject
  ): Promise<string> {
    return "";
  }

image

I also tried to add ApiQuery decorator like this:

@common.Get("/action")
  @swagger.ApiQuery({
    required: false,
    style: "deepObject",
    explode: true,
    type: parentObject,
  })
  async getWithChildObjectInQuery(
    @common.Query() query: parentObject
  ): Promise<string> {
    return "";
  }

but then I see both properties

image

Input Code

Link to repo with reproducable code

https://github.com/yuval-hazaz/nest-swagger-child-prop

Expected behavior

I expect to see two only the optional parameter of type object with inner required property

Environment


Nest version: 7.6.15

 
For Tooling issues:
- Node version: v15.13.0  
- Platform:  Mac & Windows

Others:
reproduced on your sample swagger project

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
yuval-hazazcommented, May 7, 2021

After some further investigation, I found the way to make it work.

Instead of @query I used @ApiQuery with style:“deepObject” and explode:true. I also had to remove @Query and use @Req to get the query values - otherwise all the parameters were listed twice on the open-api Json

   @Get("/action")
+ @ApiQuery({
+   type:parentObject,
+   style:"deepObject",
+    explode:true
+ })
   async getWithChildObjectInQuery(
+   @Req() request: Request
-    @Query() query: parentObject
   ): Promise<string> {
     const query :parentObject = request.query
     console.log(query);
    
     return "";
  }

When using this method I see the parameters correctly in the open-api json and on swagger UI This looks good: image

But, there is still a minor issue in Swagger UI - When I use this value, it is sent incorrectly to the server (as an immediate parameter called “rquiredInnerLoop”

{
    "requiredInnerProp": "string"
}  

Instead, I have to use this value for it to work correctly (note the additional “optionalChildPop” object although it is the name of the parameter

{
   "optionalChildProp":
   {
       "requiredInnerProp": "string"
   }
}  

image

0reactions
laurynas-wncommented, Feb 8, 2022

Would be great to have an option to overwrite @Query generated params.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parameter Serialization - Swagger
Parameter Serialization · style defines how multiple values are delimited. Possible styles depend on the parameter location – path, query, header or cookie....
Read more >
Swagger Inheritance and Composition - Stack Overflow
The response_header and response_body objects can be extended and inserted into any response object, which is done in the case of a filename_ ......
Read more >
F.A.Q - Springdoc-openapi
You can use the same swagger properties in the documentation as Spring Boot ... default expansion setting for the operations and tags, in...
Read more >
Step 5: The components object (OpenAPI tutorial)
The properties for each object inside components are the same as they ... Replace the existing paths object in the Swagger Editor with...
Read more >
Hide a Request Field in Swagger API - Baeldung
@ApiParam is also a Swagger annotation that we can use to specify metadata related to request parameters. We can set the hidden property...
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