Swagger: Bug - Swagger - Properties from inner DTO are expanded

Created on 13 Apr 2021  Â·  3Comments  Â·  Source: nestjs/swagger

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

Most helpful comment

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

All 3 comments

Can anyone confirm or direct me what I am doing wrong?

no one?

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

patilrevansidh picture patilrevansidh  Â·  4Comments

malbertSC picture malbertSC  Â·  5Comments

arfaWong picture arfaWong  Â·  5Comments

otroboe picture otroboe  Â·  3Comments

Diluka picture Diluka  Â·  4Comments