I'd like to provide a series of custom codes and message for my error 400 but can't find any way to do so. Ideally something like:
Error:
type: object
enum:
- [E01, 'Error1']
- [E02, 'Error2']
And so on
I'd like to provide a series of custom codes and message for my error 400 but can't find any way to do so. Ideally something like:
Error:
type: object
enum:
- [E01, 'Error1']
- [E02, 'Error2']
And so on
So I'm not sure that enums here can help you. If you take a look over official documentation Enums are just strings, not object https://swagger.io/docs/specification/data-models/enums/ .
So my proposal is to use :
ErrorType:
type: object
properties:
code:
type: integer
name:
type: string
example: # Sample object
code: 10
name: Custom Error
Error:
type: object
properties:
errors:
oneOf:
- $ref '#/ErrorType'
- etc...
or directly without properties
Error:
type: object
oneOf:
- $ref '#/ErrorType'
- etc...
You can take a look for more examples on the official page https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
You can check out how Twitter
has described it with $oneOf
( https://api.twitter.com/labs/2/openapi.json ) but unfortunately this convention is not easy nor supported by code generation tools like openapi-generator
.
Until OpenApi supports enum descriptions (proposal is pending), one of the easiest ways are to:
description:
section of the spec define an inner section (with markdown notation #Error codes
) that lists all error codes along with description.© 2022 - 2024 — McMap. All rights reserved.