Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(openapi): Add docs to throw different exceptions under the same … #2198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions content/openapi/types-and-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.