0

I want to add some custom example to an ApiResponse. I tried the following code but the output is the default:

  @Get('/:id')
  @ApiResponse({
    status: HttpStatus.OK,
    type: ResponseDto,
  })
  @ApiResponse({
    description: 'Failure retrieving card details',
    status: HttpStatus.BAD_REQUEST,
    schema: {
      $ref: getSchemaPath(ErrorMessageDto),
      example: {
        errors: [
          {
            message: 'Invalid format',
            property: 'id',
            detail: null,
          },
        ],
      },
    },
  })
  public async someMethod(): Promise<ResponseDto> {
    return ...
  }

output

I cannot put the example in ErrorMessageDto because will be used in all endpoints, and I want to put deferents examples in deferents endpoints.

I know that swagger natively allows you to do this. how do it via nestjs?

Miguel Borges
  • 7,549
  • 8
  • 39
  • 57
  • It probably happens because you put the `example` alongside `$ref`, which causes the example to be ignored. You need to wrap the `$ref` into `allOf` as [explained here](https://stackoverflow.com/a/41752575/113116). – Helen Nov 21 '22 at 17:51

0 Answers0