I have a series of parameters in Swagger like this
"parameters": [ { "name": "username", "description": "Fetch username by username/email", "required": false, "type": "string", "paramType": "query" }, { "name": "site", "description": "Fetch username by site", "required": false, "type": "string", "paramType": "query" }, { "name": "survey", "description": "Fetch username by survey", "required": false, "type": "string", "paramType": "query" } ],
One parameter MUST be filled out but it doesn't matter which one, the others can be left blank. Is there a way to represent this in Swagger?
The most you can do is define these parameters in the global parameters section (in OpenAPI 2.0) or the components/parameters section (in OpenAPI 3.0) and then $ref all parameters explicitly in each operation. The drawback is that you need to duplicate the $ref s in each operation.
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):
Mutually exclusive parameters are possible (sort of) in OpenAPI 3.x:
oneOf
or maxProperties
to limit the object to just 1 property.style: form
and explode: true
, so that the object is serialized as ?propName=value
.An example using the minProperties
and maxProperties
constraints:
openapi: 3.0.0 ... paths: /foo: get: parameters: - in: query name: filter required: true style: form explode: true schema: type: object properties: username: type: string site: type: string survey: type: string minProperties: 1 maxProperties: 1 additionalProperties: false
Using oneOf
:
parameters: - in: query name: filter required: true style: form explode: true schema: type: object oneOf: - properties: username: type: string required: [username] additionalProperties: false - properties: site: type: string required: [site] additionalProperties: false - properties: survey: type: string required: [survey] additionalProperties: false
Another version using oneOf
:
parameters: - in: query name: filter required: true style: form explode: true schema: type: object properties: username: type: string site: type: string survey: type: string additionalProperties: false oneOf: - required: [username] - required: [site] - required: [survey]
Note that Swagger UI and Swagger Editor do not support the examples above yet (as of March 2018). This issue seems to cover the parameter rendering part.
There's also an open proposal in the OpenAPI Specification repository to support interdependencies between query parameters so maybe future versions of the Specification will have a better way to define such scenarios.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With