使用特定的硬编码值创建Swagger 2.0定义的属性

bra*_*ipt 3 swagger

我正在Swagger 2.0中为OAuth JSON有效负载创建一个#definition.grant_type是必需的,但必须是特定值(password).

我怎么能告诉Swagger这个属性的值必须等于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'
Run Code Online (Sandbox Code Playgroud)

Ron*_*Ron 6

严格来说,你会这样定义它:

definitions:
  TokenRequest:
    required:
      - userId
      - password
      - grant_type
    properties:
      userId:
        type: string
      password:
        type: string
      grant_type:
        type: string
        enum:
          - password
Run Code Online (Sandbox Code Playgroud)

但是,您应该知道Swagger 2.0具有指定应用程序安全属性的特定部分,包括OAuth2密码流.然后,您可以为API全局设置它,并在每次操作时根据需要覆盖它.或者,您可以按操作声明它.

有关它的更多信息:

  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