Skip to content
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

Fix Content-Length Mismatch while using json and ujson #1403

Conversation

harshanarayana
Copy link
Contributor

The current implementation of sanic attempts to make use of ujson if it's available in the system and if not, it will default to the inbuilt json module provided by python.

The current implementation of ujson does not provide a mechanism to provide a custom separators parameter as part of the dumps method invocation and the default behavior of the module is to strip all the spaces around separators such as : and ,. This leads to an inconsistency in the response length when the response is generated using the ujson and inbuilt json module provided by python.

To maintain the consistency, this commit overrides the default behavior of the dumps method provided by the json module to add a separators argument that will strip the white spaces around these character like the default behavior of ujson

This addresses the issue referenced in #1398

PS: Fixed some basic linter reported issue in the test files as well.

The current implementation of `sanic` attempts to make use of `ujson` if
it's available in the system and if not, it will default to the inbuilt
`json` module provided by python.

The current implementation of `ujson` does not provide a mechanism to
provide a custom `seperators` parameter as part of the `dumps` method
invocation and the default behavior of the module is to strip all the
spaces around seperators such as `:` and `,`. This leads to an
inconsistency in the response length when the response is generated
using the `ujson` and in built `json` module provided by python.

To maintain the consistency, this commit overrides the default behavior
of the `dumps` method provided by the `json` module to add a `seperators`
argument that will strip the white spaces around these character like
the default behavior of `ujson`

This addresses the issue referenced in sanic-org#1398

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
@ahopkins
Copy link
Member

ahopkins commented Nov 7, 2018

Awesome catch! Taking a look now.

ahopkins
ahopkins previously approved these changes Nov 7, 2018
@harshanarayana
Copy link
Contributor Author

harshanarayana commented Nov 7, 2018

@ahopkins Adding a few more unit test with responses containing different data types. Please wait before the merge.

Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
@harshanarayana
Copy link
Contributor Author

@ahopkins Done.

@ahopkins
Copy link
Member

ahopkins commented Nov 7, 2018

No worries. I will not merge.

As a general practice, we've adopted a strategy to have two release managers for a year cycle. Their job is to do all merges to master and steer the release cycle. Right now it is @sjsadowski and @yunstanford. But the implementation looks sound. And more tests can never hurt.

@codecov-io
Copy link

codecov-io commented Nov 7, 2018

Codecov Report

Merging #1403 into master will increase coverage by 0.02%.
The diff coverage is 33.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1403      +/-   ##
==========================================
+ Coverage    81.3%   81.33%   +0.02%     
==========================================
  Files          17       17              
  Lines        1696     1698       +2     
  Branches      322      322              
==========================================
+ Hits         1379     1381       +2     
- Misses        248      249       +1     
+ Partials       69       68       -1
Impacted Files Coverage Δ
sanic/response.py 86.75% <33.33%> (-0.5%) ⬇️
sanic/router.py 92.69% <0%> (+0.45%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 92b73a6...f1f1b8a. Read the comment docs.

@harshanarayana
Copy link
Contributor Author

Looks like the CI jobs is stuck somewhere. It ran the Unit Tests for the second commit as well , but the results aren't being reported. Strange.

@ahopkins
Copy link
Member

ahopkins commented Nov 7, 2018

checking Travis

@yunstanford
Copy link
Member

I've restarted travis ci build

@yunstanford yunstanford merged commit 4cb40f2 into sanic-org:master Nov 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants