-
Notifications
You must be signed in to change notification settings - Fork 9.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Querystring in Path Specification #164
Comments
This is the same issue with Content-Type operation overloading we are discussing at #146 |
Another scenario to consider why this should be added: Get: /photos?mentions=Jim "retrieves photos where jim is mentioned" I want my interface to be narrow per SOLID coding principles and don't have a use case that requires searching on both of these attributes simultaneously so it would just be confusing to merge them into one operation. Without the swagger spec supporting parameter based operation discrimination I can't describe both of these operations separately. |
@chrisdostert If I understand your example correctly, the functionality is the same in both cases (return a list of photos), but the query string is serving as a filter on the responses. Is what you're hoping to do a primarily a documentation concern (i.e. list them as separate methods in Swagger-UI), or is it something more? Swagger's point of view is that resource paths plus verb are the signature, and that the response object is deterministic from that signature. So far, the project has resisted allowing query parameters to factor into the operation signature, and it is a pretty fundamental to the Swager point of view on API design. |
@earth2marsh yes they are distinct methods. They perform different operations and should be individually documented. |
We did spend time on this subject and decided it wasn't the right direction. That can change for future versions, but here are some of the reasons.
The conclusion was that this was a can of worms and it'd make the api harder to work with. There are other ways to represent the same logic without relying on query parameters that may be more easily understood. The rest of the toolchain would become very, very complicated to make consistent and reliable. I hope that helps at least with the past decision making. |
An operation can return different type of responses (with the same response code) in many APIs. The reason why an operation is returning certain response can be a lot of things. It can be because a query param is determining it (your example), it can be content negotiation (Content-Type header), it can be internationalization or localization. Current version of Swagger does not support operation overloading (having different response for an operation with the same response code) but that doesn't mean we can't have it in the next next version of Swagger. That's why this issue has Swagger.Next tag. Swagger should not describe logic of your API. In your API query param is determining the response shape but in other APIs it can be different. My proposal for this is to describe all possible responses without binding each response to a query param or a header param or anything else. That logic can be described in Here is my proposal for operator overloading: swagger: '2.1'
info:
version: 1.0.0
title: Overloading example
paths:
/:
get:
responses:
200:
oneOf:
- description: Returns a string
schema:
type: string
- description: Returns a number
schema:
type: integer |
@fehguy the concerns raised by your examples are squelched if you only consider the presence of query parameters and not their values (as operation discriminators). Thoughts? |
side question. isn't that intent more clearly represented in a well-designed path as opposed to query params? |
I mean you get into personal preferences with that question right? obviously there are alternative path and query parameter based implementations out there from what i've given in my examples (that ultimately expose the same operations) but I think you'd end up with nearly a fair split with regards to people preferring path based over parameter based and I'd certainly say it would be case by case with respect to the particular operation being exposed. |
+1 In our case we need it for
Every action is implemented as different method with different POST body. We don't want to distinguish actions by path (eg. |
@zdila in this specific case you could perhaps model your api as follows: PUT /v1/security/credentials/password -> changes the password (body contains new password) if you agree that a "reset" could be a noun (as in "I'd like to request a password reset") - then the (somewhat lofty) requirement "paths should be nouns, not verbs." is at least fulfilled :-) /Ole |
I have a similar issue but in my case my response type is the same: For example, let's there there is a User service that allows you to create, remove, or search for users:
And search API:
Theoretically they all can be defined as a single Swagger operation
But how to you document which fields belong to which operation? Making them all optional is miss-information. Also client code should really have 4 separate methods. Hypothetically, you could add:
But this would go against REST-ful principle of a verbs in the resource URI - "/user/searchByUsername" is not a resource. You could also go all out and treat search as a resource:
The first option is preferred and property describes the API. Any recommendations? |
I'd love to see this get implemented. It looks like @SamG1000 has already said what I was about to say. QueryString params are useful for searches, and they keeps the code clean. Documentation is also better when you're able to split it up by the parameter set. /REST/Customers/ @olensmar please consider adding this ability. We'd like to start using this at my current company, but to do that we'd need to be able to use the QueryString paths so we wouldn't have to change all of our routes across the project. |
+1 |
+1 There's another useful scenario where the structure of a response can change substantially based on a query parameter - expansion of nested collections. To reduce the number of API queries a client needs to make, it's often valuable to provide the option to include all instances of a related collection in a single response. Say you have an |
+1 |
Parent: #574. |
I came here looking for help with deciding how to deal with different versions of objects returned from a RESTful API which I think is a subset of the wider problem of returning different responses from the same resource. |
Can you point to specifications that substantiate this claim? My understanding is this a misconception based on text in RFC 2396 that was corrected in RFC 3986 back in 2005. A resource is identified by a URI and query parameters are an integral part of that identifier. Calling other people's designs out because they don't conform to your perspective of what is RESTful is not helpful. If you see issues in designs that violate RESTful constraints then you are welcome to explain the negative system effects. However, recognize that every choice is a trade off and there may be valid reasons for choosing to violate a constraint. OpenAPI is a specification to help describe HTTP APIs, it uses the term REST loosely because that is the term people have come to expect when building HTTP APIs. Actual REST based systems do not use design time descriptions of resources, as that would inhibit evolvability, so debating REST constraints here is not useful. |
After doing a lot of extra reading, I must now eat humble pie. I have been labouring under out-of-date knowledge. It also turns out all my knowledge about internet caching was also out-of-date too as that was the (previous) primary reason for being against this proposal. Now armed with the most up-to-date information, I'm still apprehensive about this proposal. While I realise there are many influences on the RESTful design an organisation might take, it looks like there must be a matrix of query parameters, headers, paths and whatever else in order to describe what combinations will have what return types. |
@davidnewcomb I agree that using only the path to identify the resource is a nice simplification that is sufficient for most cases. However, a significant number of people come to OpenAPI to try and document an existing API. We have to balance encouraging good practices with being compatible with the wide range of ways HTTP APIs are implemented. |
I just want to know if this feature has been added to the latest version of the spec and if it is implemeted in swagger editor. Could anybody help? |
sorry for late response but @davidnewcomb wait.. I said no such thing AFAICT; not sure where that quote back in 2016 is coming from |
@iambingoNJU No. Version 3.0 of the spec still only uses the path portion of the resource identifier to identify operations. |
@darrelmiller Thx! And sorry for my late reply. And how do you fix this kind of problem? Maybe I think I should change the service endpoint. |
Is there any progress on this? |
No. It is one of the topics under consideration for the next version #2572 |
Sometimes there are the same path, but there are different operations with query param. Open API spec is designed to understand and interact with consumers. So each path which has a different query param should be defined to understand. I hope this problem will be solved in the next version. |
This general concept of this proposal has been accepted into Moonwalk under the "operation signatures" principle. 🎉 Since a change of this magnitude can't go in the 3.x release line, I'm closing this issue in favor of the Moonwalk discussions. |
Issue
In some APIs, different query string values specifies the operation done on the resource, thus, result in dramatically different response types and/or parameter requirements. In the case of Foursquare Venues Search API, depending on the
intent
query string value, the requirement specs of other query string vary.Such distinction deserves a separate path entity, to allow different descriptions, parameter requirements, responses, etc.
Current State
Currently due to Swagger Spec, on swagger-ui, the generated url for testing is incorrect if a query string is included in the path.
becomes:
.../bluelights?query=nearby?more=...
Proposal
Allow Query Strings in the path key
The example above is allowed. so for example, depending on the path key,
"/bluelights?query=nearby"
and"/bluelights"
are considered separate path entities.The text was updated successfully, but these errors were encountered: