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
webob 1.7 compat: provide a charset for json responses #2971
Conversation
AFAIK dropping the charset off of a non-text content-type was the main goal of webob 1.7's changes. A JSON response is not supposed to have an explicit charset. I think the first question is - |
We deal with non-ASCII-non-latin1 json so a charset is necessary for the client to properly decode the bytes it receives on the wire. A quick google confirms other frameworks do / require the same thing: https://www.google.com/search?client=ubuntu&channel=fs&q=json+response+encoding&ie=utf-8&oe=utf-8 The specific bit of code that's very angry with no charset: https://github.com/striglia/pyramid_swagger/blob/e89cf43fc91d382d9255669e0cd9f12543988c8b/pyramid_swagger/tween.py#L545-L549 |
JSON is text (not a binary response) so I think it should have a charset |
For non-UTF* json it's certainly important to force a charset, but we're talking about utf8 here which the mimetype for Some other discussions [2] [3] cover this fact as well. [1] https://tools.ietf.org/html/rfc4627#section-3 |
The exception guards against this trace (webob<1.7) -- the code is compatible with webob 1.4 - webob 1.7:
|
I personally would expect the |
It does, but that's only present in webob 1.7+ so we're in a bit of a pickle of not being able to roll forward or back without a charset in the response. |
Sorry I've read this issue several times now and I'm still confused what you mean here. I think you're saying that pyramid_swagger is struggling to perform a check that will work with both webob 1.6 and 1.7. It seems to me that you should just stop checking for the charset because in both versions |
You are not dealing with JSON at this point. JSON itself as defined as either UTF-8/16/32 (and the latter two only with a BOM). You'll want to use something other than Setting a charset will work, and WebOb will dutifully set the Content-Type to Your tests shouldn't be checking for a charset on JSON responses (because it doesn't exist), and always force them to be valid JSON by making sure they at valid UTF-8 or UTF-16/32 with BOM. WebOb 1.7 will do it's best to not send a charset content-type parameter if the content-type shouldn't have one. This change would undo that work and have Pyramid send a response that is invalid, please fix your code that is causing the failure. |
Issue created for pyramid_swagger: https://github.com/striglia/pyramid_swagger/issues/185 |
My understanding, based on RFC 2045 / 2616, is that the See the
|
Understandable. I'm in a bit of a pickle with upgrading however as we're running a SOA environment:
As you can see, this seemingly harmless backward incompatibility is really wedging my plans to upgrade :( I initially opened this in that it was a quick fix that seemed to be a regression, though after consideration it seems that this change is intentional. Thanks for the comments, I'll bang my head against this some more and see if I can't find an upgrade pathway that's possible at all -- feel free to close this PR :) |
I'm not sure I see the pickle here. The only issue seems to be that your downstream consumers were implemented incorrectly to assume a charset would be there. Webob has attempted to fix this a few times in its history. One change of note is https://github.com/Pylons/webob/blob/8669fe335b54697f23a787f67da19552bc03d5c6/HISTORY.txt#L122 which was put into 1.6 but apparently in such a way that it didn't affect you (lucky!). I mean I guess you either keep using your custom renderer to ensure the charset is always there, or you fix your consumers to be compliant with the RFC. Probably a little of a and a little of b until you decide you can drop your shim. |
The pickle is that old webob and new webob unfortunately. Most of our stuff is super old (1.4) and upgrading any one of them breaks downstreams (which leads to circular brokenness). It's fine though, I'll figure it out. |
Hitting this at work while trying to upgrade webob -- we're using this hackety workaround for now:
Demonstration of failing test:
with test, webob<1.7
with test, webob==1.7.1
After the patch, the tests passes in both versions