[ ] 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.
Swagger only allows one response per status code, but sometimes we have more than one response body.
We can work around this in the decorator by:
number to number | stringstatus already exists@ApiForbiddenResponse({ type: UnauthorizedTokenException })
@ApiForbiddenResponse({ type: InsufficientRolesException })
I have more than one error response with different Dto classes using the same status code.
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.

@fwoelffel
Yes, I tried.
However, refs require function array type and my UserOrerInfoDto is Class type, so type error occured.
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()

Thank you for your help!!
Most helpful comment
Any updates?