In swagger, is there a way to specify different response objects based on query param? (Workaround?)

1,092 views
Skip to first unread message

umuk

unread,
May 30, 2017, 8:24:45 AM5/30/17
to Swagger

Is there a way to specify an entirely different response object for a RESTAPI (in Swagger) for a different specification of a query param?

e.g.

URL : /A/B/C query param format=short response ...

query param format=long response ...



NOTES:


Found this stack overflow thread: https://stackoverflow.com/questions/40495880/having-different-query-parameters-on-same-path-in-yaml


So aparently it's not supported in swagger 2.0 spec.

The original poster proposes a workaround, which is to define two different endpoints with the query params of significance shown with the URL:


<baseURL>/path?filter1={value}&filter2={value}.....&filterN=‌​{value}


Would this be a good practice or is there some other workaround that would be preferred?


Thanks

umuk

unread,
May 30, 2017, 9:00:51 AM5/30/17
to Swagger


On Tuesday, May 30, 2017 at 8:24:45 AM UTC-4, umuk wrote:

Is there a way to specify an entirely different response object for a RESTAPI (in Swagger) for a different specification of a query param?


<SNIP> 


<baseURL>/path?filter1={value}&filter2={value}.....&filterN=‌​{value}


Would this be a good practice or is there some other workaround that would be preferred?



Swagger editor throws an error when I try this:

Semantic error at paths./stats/v1/bandwidth?format=timeseries

Query strings in paths are not allowed. 

umuk

unread,
May 30, 2017, 9:52:08 AM5/30/17
to Swagger
Further I have found this FAQ at bottom of this page:

Can I have different responses based on a request parameter? Such as:

GET /something -> {200, schema_1}
GET /something?foo=bar -> {200, schema_2}

No, this is not supported.



I am still wondering how others have overcome this? Maybe some workaround exists (regardless of how elegant or unnatural it is) ?

Ron Ratovsky

unread,
May 30, 2017, 11:41:08 PM5/30/17
to swagger-sw...@googlegroups.com

There’s no way of doing it. A unique identifier for an operation is a combination of the verb and the path. Query parameters cannot be used to differentiate operations.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages