Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

openapi

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.

like image 967
GIS-Jonathan Avatar asked Dec 14 '18 15:12

GIS-Jonathan


People also ask

How do you pass multiple parameters in swagger?

If you are trying to send a body with multiple parameters, add an object model in the definitions section and refer it in your body parameter, see below (works with editor.swagger.io):

How do you specify optional parameters in swagger?

You can use the default key to specify the default value for an optional parameter. The default value is the one that the server uses if the client does not supply the parameter value in the request. The value type must be the same as the parameter's data type.

What is allOf in OpenAPI?

OpenAPI lets you combine and extend model definitions using the allOf keyword. allOf takes an array of object definitions that are used for independent validation but together compose a single object. Still, it does not imply a hierarchy between the models.


1 Answers

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]

There's also an existing feature request to support interdependencies between individual parameters in the parameters list:
https://github.com/OAI/OpenAPI-Specification/issues/256

like image 156
Helen Avatar answered Oct 19 '22 19:10

Helen