How to require at least one of two parameters in OpenAPI?

梦想与她 提交于 2020-06-08 20:12:27

问题


I'm using OpenAPI 3 and have two query parameters, at least one of which is required but it doesn't matter which.

I.e., as sudocode:

if parameter_1 OR parameter_2:
    do stuff
else:
    error

Is this possible in OpenAPI 3? There's no mention of it in the spec as far as I can see, or the JSON Schema spec either.


回答1:


This scenario is very similar to mutually exclusive parameters. Basically, you can use an object-type parameter where parameter_1 and parameter_2 are the object properties; such object will be serialized as ?parameter_1=value1&parameter_2=value2. The "at least one of" constraint can be represented using minProperties or anyOf.

openapi: 3.0.2
...
paths:
  /foo:
    get:
      parameters:
        - in: query
          name: param
          required: true
          schema:
            type: object
            properties:
              parameter_1:
                type: string
              parameter_2:
                type: string
            additionalProperties: false

            # Option 1
            minProperties: 1

            # Option 2
            # anyOf:
            #  - required: [parameter_1]
            #  - required: [parameter_2]


来源:https://stackoverflow.com/questions/53782328/how-to-require-at-least-one-of-two-parameters-in-openapi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!