Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe4840f

Browse files
docs(openapi): Add docs to throw different exceptions under the same http code
1 parent c6e212f commit fe4840f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎content/openapi/types-and-parameters.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,24 @@ pets: Pet[];
298298
> info **Hint** The `getSchemaPath()` function is imported from `@nestjs/swagger`.
299299

300300
Both `Cat` and `Dog` must be defined as extra models using the `@ApiExtraModels()` decorator (at the class-level).
301+
302+
#### Multiple responses in controller
303+
304+
In case your app returns different exceptions with under the same HTTP status code, you might use `@ApiExtraModels()` in this way:
305+
306+
```typescript
307+
@Injectable()
308+
export class CatController {
309+
@ApiExtraModels(BadAppCodeException, BadEmailException)
310+
@ApiBadRequestResponse({
311+
schema: {
312+
oneOf: refs(BadAppCodeException, BadEmailException),
313+
},
314+
})
315+
multipleErrorsResponse() { ... }
316+
}
317+
```
318+
319+
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.
320+
321+
> info **Hint** The `refs()` function is imported from `@nestjs/swagger`.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /