From 31c8dc4dd0ef48778bb6d34d3d8709ef1da02600 Mon Sep 17 00:00:00 2001 From: Axel Espinosa Date: Sat, 5 Feb 2022 12:50:45 -0600 Subject: [PATCH] docs(openapi): Add docs to throw different exceptions under the same http code --- content/openapi/types-and-parameters.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/content/openapi/types-and-parameters.md b/content/openapi/types-and-parameters.md index 2bf60bd6c3..c2959f3584 100644 --- a/content/openapi/types-and-parameters.md +++ b/content/openapi/types-and-parameters.md @@ -298,3 +298,24 @@ pets: Pet[]; > info **Hint** The `getSchemaPath()` function is imported from `@nestjs/swagger`. Both `Cat` and `Dog` must be defined as extra models using the `@ApiExtraModels()` decorator (at the class-level). + +#### Multiple responses in controller + +In case your app returns different exceptions with under the same HTTP status code, you might use `@ApiExtraModels()` in this way: + +```typescript +@Injectable() +export class CatController { +@ApiExtraModels(BadAppCodeException, BadEmailException) +@ApiBadRequestResponse({ + schema: { + oneOf: refs(BadAppCodeException, BadEmailException), + }, +}) +multipleErrorsResponse() { ... } +} +``` + +First, you tell swagger that `BadAppCodeException` and `BadEmailException` are models it needs to look at. After that, you use `oneOf` and `refs` to set the different exceptions your API might throw. + +> info **Hint** The `refs()` function is imported from `@nestjs/swagger`.