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.
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):
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 .
Hello, like described here (swagger-api/swagger-ui#380), path parameters are required and can't be optional.
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.
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]
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