-
Notifications
You must be signed in to change notification settings - Fork 9.1k
[oas3] Default 'explode' for cookie parameters? #1528
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
Comments
The Specification is the source of truth - so the cookies are not exploded by default. I've fixed the swagger.io doc. If you notice any other info on the Swagger website that contradicts the info in the OpenAPI Specification, please file an issue in the https://github.com/swagger-api/swagger.io repository. Thanks! |
What does explode even mean for a cookie, though? If I have a list in a cookie "foo", does this mean I have a Cookie header with multiple copies of the cookie?
RFC 6265 doesn't disallow this, and some clients will even send multiple copies of the same cookie in certain weird circumstances (if you set a cookie "foo" with the domain "*.myserver.com", and then set a second cookie "foo" with the domain "baz.myserver.com"). But... That'd be a weird way to pass parameters around. :P |
And, cookies default to "form", but they aren't really "form". An array encoded in "form" with
A cookie parser would turn that into |
Does this discourage you from using cookies as parameters? |
Using cookies as parameters is weird, and I know there's a history of debate about whether to include them in the spec or not in the first place. I'm not using cookies as parameters; I'm writing an implementation of the spec. If someone is using my implementation, they expect it to work correctly, even if they are using cookie parameters. So I need to work out what "correctly" is. If we compare the table for cookies and headers, you can see they are very nearly identical, except that "header" uses As far as I can tell, to correctly implement the spec, if the cookie parameter is So, in short, treat cookie parameters like simple, even though they are from and don't even support simple in the first place. This seems like an error in the spec; cookie parameters should only support 'simple', not only support 'form', and then "exploded" cookies make sense, just like they do for header parameters. |
Not quite. In In I get the potential issue with parsing the cookie 'naturally', however, using cookies to transfer such information is not ideal anyway. If you have proposals for improvement, we'd love to hear. |
Well... it depends on what you count as the "rendered" version. If you have a header pameter, for example:
The "name" of the parameter is right there; it's "X-MyHeader", so I could argue that this is really form, right? Semantically, that's not right, because the "value" of this header is "5", and that's the bit we want to parse. With a cookie header, I'd make the same argument though:
Even though "id" appears in this cookie header, I would argue that semantically there is an "id" cookie, and it's value is 5, just like in the "header" case. I can't just pass the contents of the Cookie header through a 'form' parser, because there's a "; " in there that's going to confuse the form parser (or is going to give me an "id" of "5; session=blahblahblah", since there's no & separating the arguments like there should be in 'form' style). So first I need to run this through a cookie parser, and I get out something that looks like:
So at this point I have the value '5' and I need to parse it. I can't pass this through a 'form' parser either. But I can pass it through a 'simple' parser. If you encode an
But in order for that to be the case, I have to treat the
The spec itself has no examples of how to encode a cookie, so going strictly by the spec, this is what I would expect. This doesn't look anything like the example given on swagger.io though. If I was writing the spec from scratch with no concern for backwards compatibility, I'd say "Cookie parameters only support the "simple" style. To encode a value, use simple encoding rules, and put the result in the cookie value." (Edited: I just realized that parsing my cookie header example with the form parser wouldn't generate an id of "5; " but "5; session=blahblahblah"). |
Thanks for the elaborate explanation, it helps clarifying a few things. Before diving into the explanation, I'll say this - the documentation on swagger.io is auxiliary to the spec, and can contain errors. It is not endorsed by the OAI, and I say that as one of the people behind the Swagger project. You can point out errors in documentation there, but you can't use it as a filler to missing spec documentation. I'm beginning to understand what you're getting at, however, you have one wrong assumption. You have the following part in your response:
That is actually wrong. I assume in this case you mean that
Before you jump - yes, the parameter name disappears (as it does in query parameters), and yes, it's pretty much broken when it comes to cookies. Now comes the question - what gives? The answer is... (and this is also the answer as to why there are no examples for everything) - we never really thought anyone would go through the trouble of describing objects in cookies in an exploded way (and let's put aside the why for the moment). But that's not all - you brought up a fairly important point. It might make more sense to use As for the next steps - looking to hear from others in the community and of course, the @OAI/tsc. If there's general agreement that this should change, we can decide how and when. |
If we're using the encoding as described on swagger.io, then yes, and explode is true, the parameter name disappears and it becomes:
I mean, first when I run this through a cookie parser I get But I think, by the letter of the spec, this is wrong. The spec doesn't say the cookie header is encoded as form data. The spec says if the parameter location is "cookie", this is "used to pass a specific cookie value to the API". So if I have: parameter:
in: cookie
name: 'myParam'
style: 'form'
explode: true
schema:
type: object So to encode this, I take my object
Similarly, for: parameter:
in: cookie
name: 'myParam'
style: 'form'
schema:
type: number I encode my parameter using form to get "myParam=7", then I put this in a cookie named "myParam", and I get:
So maybe the best thing to do here is to release an OAS 3.1.0 which adds "simple" to the list of allowed cookie styles, update the docs on swagger.io to show the above as the "form" encoding for cookies (because that's literally what the spec says) and then add some "simple" examples, too. This leaves the default for cookies as |
Or, to put this more succinctly: RFC 6265 says a cookie is:
Or, less formally:
OAS 3.0.1 says:
So the value part of the cookie, according to OAS3, has to be form encoded. This example from the swagger site:
Is non-conformant, because the (Edited: hit 'shift-enter' and sent too soon. :P) |
Not sure what you're referring to as 'form encoding' - it sounds like you're referring to |
Yeah, by "a form encoded string", I really meant "a RFC 6570 form-style query expansion". :) Although, now that I go back an read RFC 6570, in order to be complaint the correct example would actually be:
Because the cookie-value needs to start with a "?" to meet RFC 6570. |
Yup, that's what I referred to as the problem with the current wording. |
Related request: OAI/learn.openapis.org#100 "More and complete examples for serialization styles" |
I think this is wrong - I think it would just be:
I think to get
|
My preference would also be to have cookie values use style simple serialization. |
Should perhaps only string values be accepted? https://datatracker.ietf.org/doc/html/rfc6265#section-4.2.2
|
The way to get around the automatic RFC6570 percent-encoding is to use the From what I see here, most of the |
PR merged for 3.0.4 and ported to 3.1.1 via PR #3921! |
According to the spec, "When
style
isform
, the default value [for explode] is true. For all other styles, the default value is false." However, according to this documentation, in the "Cookie Parameters" section, there are "*"s indicating the default serialization, and it is style: form, explode: false.So which is it? Do cookies use explode by default or not?
The text was updated successfully, but these errors were encountered: