brandonscript
brandonscript

Reputation: 73044

Create a Swagger 2.0 defined property with a specific hard-coded value

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

Answers (2)

Sergej Popov
Sergej Popov

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

Ron
Ron

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:

  1. https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swaggerSecurityDefinitions
  2. https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swaggerSecurity
  3. https://github.com/swagger-api/swagger-spec/blob/master/fixtures/v2.0/json/resources/securityExample.json

Upvotes: 11

Related Questions