Swagger: [Feature Request] Multiple response with the same status code

Created on 6 Apr 2019  路  12Comments  路  Source: nestjs/swagger

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Swagger only allows one response per status code, but sometimes we have more than one response body.

Expected behavior

We can work around this in the decorator by:

  • Changing the status field type from number to number | string
  • Checking if the status already exists
  • If it exists, we add a space to the response code

Minimal reproduction of the problem

@ApiForbiddenResponse({ type: UnauthorizedTokenException })
@ApiForbiddenResponse({ type: InsufficientRolesException })

What is the motivation / use case for changing the behavior?

I have more than one error response with different Dto classes using the same status code.

PRs open enhancement

Most helpful comment

Any updates?

All 12 comments

Would you like to create a PR? :)

Would you like to create a PR? :)

Yes I have created a PR resolving the issue.

Any updates?

something new?

+1

Would love to see this merged.

I might be late for this but why don't you use the SchemaObject interface to document your responses?

@ApiForbiddenResponse({
  schema: {
    anyOf: refs([
      UnauthorizedTokenException,
      InsufficientRolesException
    ])
  }
})

@fwoelffel
Hi
I came in while Googleing because I needed a multiple response from swagger.
The multi error response you have attached is resolved, but it does not apply to the 200 success responses. Because the arrangement of "refs" only request "funcion types."
What kind of settings do I need to set to respond to 200 multiple responses?
I can't see anything in browser with Schema Object like this:

  @ApiOkResponse({
    schema: { anyOf: [{ $ref: getSchemaPath(UserOrderInfoDto) }] },
  })

// or

  @ApiOkResponse({
    schema: { anyOf: [{ type: getSchemaPath(UserOrderInfoDto) }] },
  })

;(

@fwoelffel
Hi
I came in while Googleing because I needed a multiple response from swagger.
The multi error response you have attached is resolved, but it does not apply to the 200 success responses. Because the arrangement of "refs" only request "funcion types."
What kind of settings do I need to set to respond to 200 multiple responses?
I can't see anything with Schema Object setting like this:

I'm not sure I fully understand your issue, but I'll try to help.

Have you tried with refs like I did in my example? refs can be imported from @nestjs/swagger.

import {
  ApiOkResponse,
  refs,
} from '@nestjs/swagger';

// ...

@ApiOkResponse({
  schema: {
    anyOf: refs([
      UserOrderInfoDto
    ]),
  },
})

@fwoelffel
Yes, I tried.
However, refs require function array type and my UserOrerInfoDto is Class type, so type error occured.
鞀ろ伂毽办兎 2020-08-05 16 57 50

@fwoelffel
Yes, I tried.
However, refs require function array type and my UserOrerInfoDto is Class type, so type error occured.
鞀ろ伂毽办兎 2020-08-05 16 57 50

Could you share your UserOrderInfoDto implementation? Make sure it is a class and not an interface.


EDIT: Ok, I'm sorry I didn't realize this earlier. Your DTOs should not be wrapped in an array:

@ApiOkResponse({
  schema: {
    anyOf: refs(
      UserOrderInfoDto
    ),
  },
})

Sorry for misleading you. 馃槥

Thank you very much!
Your code works well and expresses multiple responses well.
In fact, the essential problem was that what I was going to designate as a response "schema" was not registered as a "schemas", so swagger showed empty object.

For those who have missed the official document like me, write additional.
If your DTO is not represented or read by an empty object, you must register the DTO through @ApiExtraModels()

鞀ろ伂毽办兎 2020-08-06 00 55 43

Thank you for your help!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alisherks picture alisherks  路  4Comments

djedlajn picture djedlajn  路  4Comments

dennisameling picture dennisameling  路  4Comments

mogusbi picture mogusbi  路  3Comments

otroboe picture otroboe  路  3Comments