#Swagger nested objects

13 messages · Page 1 of 1 (latest)

hidden spindle
#

Hello guys I am trying to swagger my endpoints which has nested data
For example this is what my response look

{
  "message": "Request successful",
  "data": {
    "(user or users or cart etc.)":"array or object or whatever"
  },
  "statusCode": 200
}

so here is how i am handling it

  @ApiResponse({ status: HttpStatus.OK, description: 'User fetched successfully', type: IUserResponse })

The type i am assigning is this class

export class IUserResponse extends IResponse {
  @ApiProperty({ type: IUserPublic })
  data: {
    user: IUserPublic;
  };
}

the IResponse is this

export class IResponse {
  @ApiProperty({ description: 'Response message', example: 'Request successful' })
  message: string;

  @ApiProperty({ description: 'Response data', example: {}, type: Object })
  data: unknown;

  @ApiProperty({ description: 'HTTP status code', example: HttpStatus.OK })
  statusCode: HttpStatus;
}

So why the response in the swagger looks like this

{
  "message": "Request successful",
  "data": {},
  "statusCode": 200
}

instead of what i specified above

supple matrix
#

Probably because of this

@ApiProperty({ description: 'Response data', example: {}, type: Object })
data: unknown;
#

You set the exmaple to be {}

hidden spindle
#

but it is the general example

hidden spindle
supple matrix
#

Try setting the exmaple here

#
export class IUserResponse extends IResponse {
  @ApiProperty({ type: IUserPublic })
  data: {
    user: IUserPublic;
  };
}
hidden spindle
supple matrix
#

And if you remove the example on the parent class?

supple matrix
#

So it works?

hidden spindle
#

@supple matrix It worth to note that they work good but now inside an object with user key and the value is the Actual user

export class IUserResponse extends IResponse {
  @ApiProperty({ type: () => IUserPublic })
  data: {
    user: IUserPublic;
  };

  @ApiProperty({ type: () => IUserPublic })
  withoutObject?: IUserPublic;
}