JohnRDOrazio
JohnRDOrazio

Reputation: 1534

how to indicate that a parameter is conditionally required when another parameter is used in Swagger Open API 3.0

I have created a RESTful API, and I am now defining the Open API 3.0 JSON representation for the usage of this API.

I am requiring usage of a parameter conditionally, when another parameter is present. So I can't really use either required: true or required: false because it needs to be conditional. Should I just define it as required: false, and then in the summary and / or description say that it is required when the other parameter is being used? Or is there a way of defining dependency between parameters? I haven't found anything in the specs that mention a case like this.

Upvotes: 29

Views: 38150

Answers (2)

Laurent Picquet
Laurent Picquet

Reputation: 140

See https://community.smartbear.com/t5/Swagger-Open-Source-Tools/Defining-conditional-attributes-in-OpenAPI/td-p/222410 where you can use anyOf around the required list of field

anyOf:
       - required: [longitude, latitude]
       - required: [postalCode, countryCode]
       - required: [city, state, countryCode]

Upvotes: 12

rootkonda
rootkonda

Reputation: 1743

From the docs:

Parameter Dependencies

OpenAPI 3.0 does not support parameter dependencies and mutually exclusive parameters. There is an open feature request at github.com/OAI/OpenAPI-Specification/issues/256. What you can do is document the restrictions in the parameter description and define the logic in the 400 Bad Request response.

Upvotes: 15

Related Questions