Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swagger

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'
like image 953
brandonscript Avatar asked Feb 11 '23 13:02

brandonscript


1 Answers

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
like image 177
Ron Avatar answered Feb 15 '23 09:02

Ron