OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Support interdependencies between query parameters

Open cwarny opened this issue 11 years ago • 71 comments

It would be great to be able to specify interdependencies between query parameters. In my app, some query parameters become "required" only when some other query parameter is present. And when conditionally required parameters are missing when the conditions are met, the API fails. Of course I can have the API reply back that some required parameter is missing, but it would be great to have that built into Swagger.

cwarny avatar Jan 29 '15 23:01 cwarny

This is something that's very complicated to describe elegantly. Feel free to suggest a format that you think would be suitable for it.

webron avatar Jan 29 '15 23:01 webron

My suggestion would be as follows: instead of having the field "required" boolean, make it either boolean or an array of references to other parameters. If you put required: true, it would have to always be present. If you put required: [$a, $b], it means that this parameter is required only when parameters a and b are present.

cwarny avatar Jan 29 '15 23:01 cwarny

How you describe an endpoint where one of it's parameters is required if another query parameter is equal to a string or is larger than a number? This can get very complicated. I would say if a parameter is required sometimes it's not required.

mohsen1 avatar Jan 29 '15 23:01 mohsen1

I'd say either keep it simple and only allow simple interdependencies like, if a is present, then bis required (i.e. required: [$a, $b]), or define simple operations like, required: [$a>3, $b=="hello", "world" in $c]. I would favor keeping it simple, but then I have to admit that my only response to "why not keep it even simpler (i.e. as it is now) and only have a boolean required" is: it's a matter of how useful more sophistications are. For me, having simple interdependencies is useful and better than just a boolean required, but I don't think I need more complicated stuff. Other people might disagree.

cwarny avatar Jan 30 '15 00:01 cwarny

While this may be a solution, it can also be a validation nightmare. Definitely not something that can be done with a json schema.

webron avatar Jan 30 '15 02:01 webron

maybe something like "required": false, "depends": [ $a, $b ]

iisurge avatar Jun 17 '15 21:06 iisurge

Don't forget the "OR" dependency. I have a service where one of two query parameters must be present but it doesn't matter which one. For now we are just documenting the interdependency and using a error schema with the appropriate code/message but would be preferable to have this defined in the contract up front.

rcork avatar Sep 10 '15 17:09 rcork

+1 for cwarny simple suggestions

fractalawareness avatar Apr 27 '16 10:04 fractalawareness

My use-case is to have only one property of the parameter required, but from the array of the definitions.

filipnowakswissre avatar Oct 13 '16 09:10 filipnowakswissre

You can solve the OR use case already using an enumerated filter_type paired with a filter_value query parameter(s).

Example:

        - name: filter_type
          in: query
          description: What type of values to filter on
          required: false
          type: string
          enum: [location, service, name]
          default: location
        - name: filter_value
          in: query
          description: The value to apply to the filter_type. Should match what was in the filter, i.e. if filtering on location use a location name.
          required: false
          type: string
          default: all

craig-bayley avatar Nov 23 '16 23:11 craig-bayley

wow, good one, thanks

filipnowakswissre avatar Nov 24 '16 07:11 filipnowakswissre

@craig-bayley could you format your example as YAML, please? Just adding

```yaml
[...]
```

around it makes it much easier to read.

ePaul avatar Dec 12 '16 19:12 ePaul

@ePaul Thanks for the tip. Done.

craig-bayley avatar Dec 14 '16 12:12 craig-bayley

+1 Something like this would be very nice.

scottmas avatar Feb 14 '17 16:02 scottmas

Folks just to set expectations, we're not doing this in the initial cut of 3.0

fehguy avatar Feb 14 '17 16:02 fehguy

some query parameters become "required" only when some other query parameter is present

The JSON Schema "dependencies" keyword does exactly this. The "if"/"then"/"else" keywords (json-schema-org/json-schema-spec#180) that seem likely to go into a near-future draft would also accomplish this (probably more clearly for most people).

handrews avatar Feb 14 '17 16:02 handrews

I don't see how JSON Schema would be relevant for this at all.

webron avatar Feb 14 '17 18:02 webron

@webron further up you said:

Definitely not something that can be done with a json schema.

so I assumed that meant that JSON Schema was at least potentially relevant, which meant that I should point out that JSON Schema can in fact do this. I think you were specifically referring to an approach based on "required" which would not be valid JSON Schema, but there is JSON Schema support for this use case.

handrews avatar Feb 14 '17 18:02 handrews

Gotcha. That referred to the validation of it, not the representation of it in the spec. I assumed you were referring to the representation, making it my bad.

webron avatar Feb 14 '17 18:02 webron

When it going to be on air ? much needed one.

k11k2 avatar May 05 '17 10:05 k11k2