Reputation: 73044
I'm creating a #definition in Swagger 2.0 for an OAuth JSON payload. grant_type
is required, but must be a specific value (password
).
How can I tell Swagger that the value of this property must be equal to password
?
definitions:
TokenRequest:
required:
- userId
- password
- grant_type
properties:
userId:
type: string
password:
type: string
grant_type:
# here we need to describe that it must = 'password'
Upvotes: 8
Views: 3184
Reputation: 3021
An alernative would be to use a pattern
definitions:
TokenRequest:
required:
- userId
- password
- grant_type
properties:
userId:
type: string
password:
type: string
grant_type:
type: string
pattern: ^password$
https://swagger.io/docs/specification/data-models/data-types/#pattern
Upvotes: 2
Reputation: 14850
Strictly speaking, you would it define it like this:
definitions:
TokenRequest:
required:
- userId
- password
- grant_type
properties:
userId:
type: string
password:
type: string
grant_type:
type: string
enum:
- password
However, you should know that Swagger 2.0 has specific section to specify security attributes for your application, including the OAuth2 password flow. You can then set it globally for your API and override it if needed per operation. Alternatively, you can just declare it per operation.
For more information on it:
Upvotes: 11