Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 675
JohnRDOrazio Avatar asked Aug 01 '20 20:08

JohnRDOrazio


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):

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. For that purpose, you should include the discriminator .

Can a path parameter be optional?

Hello, like described here (swagger-api/swagger-ui#380), path parameters are required and can't be optional.


2 Answers

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.

  • For more info - swagger.io/docs/specification/describing-parameters
like image 96
rootkonda Avatar answered Oct 23 '22 04:10

rootkonda


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]
like image 23
Laurent Picquet Avatar answered Oct 23 '22 05:10

Laurent Picquet