Skip to content

Ensure JSON representation is compact. #3363 - #3367

Merged
lovelydinosaur merged 7 commits into
encode:masterfrom
BERRADA-Omar:master
Oct 28, 2024
Merged

lovelydinosaur merged 7 commits into
encode:masterfrom
BERRADA-Omar:master

Conversation

@BERRADA-Omar

@BERRADA-Omar BERRADA-Omar commented Oct 26, 2024 •

Copy link
Copy Markdown
Contributor

Summary

this PR solves the issues mentionned here #3363

The following related issues could all be resolved together...

We should use ensure_ascii=False for more compact text representations.
We should use separators = (',', ':') for more compact list and object representations.
We should use allow_nan=False to disallow invalid Infinity and NaN representations.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Comment thread .gitignore Outdated
Comment thread .gitignore Outdated

@lovelydinosaur lovelydinosaur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, thx. 鈽猴笍

@lovelydinosaur
lovelydinosaur merged commit 9fd6f0c into encode:master Oct 28, 2024
Comment thread tests/test_content.py
import pytest

import httpx
from httpx._content import encode_json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just noticed this accidentally snuck in a private import.

@BERRADA-Omar would you be up for refactoring these tests?

We can test this against public API using httpx.Request(). Eg...

data = {...}
req = httpx.Request("POST", "https://www.example.com/", json=data)
assert req.content == ...
assert req.headers = ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, i'll make the change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomchristie i see that this is already fixed ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Thanks, tho.

@Kludex

Kludex commented Nov 30, 2024 •

Copy link
Copy Markdown
Contributor

This broke the test suite on Starlette.

ref.: Kludex/starlette#2773

@lovelydinosaur

Copy link
Copy Markdown
Member

Thanks for the feedback.

@beliaev-maksim

Copy link
Copy Markdown

@tomchristie This PR not only adjusts the ASCII representation but also alters the default JSON separators. These changes lead to unnecessary modifications of JSON objects, resulting in altered validation hashes that broke some of our tests.

beliaev-maksim added a commit to canonical/gh-jira-sync-bot that referenced this pull request Dec 10, 2024
* Update pyproject.toml
* Update test_server.py: hash is changed because of HTTPX changes: encode/httpx#3367
@lovelydinosaur

Copy link
Copy Markdown
Member

Thoughts...

  • CHANGELOG entry here is insufficiently emphasised. Not an API change, but nonetheless changes to representation-on-the-wire are likely to negatively impact teams in the short run, as evidenced.
  • Unclear how we should improve evaluation process of trade-offs such as this one.

@beliaev-maksim

Copy link
Copy Markdown

@tomchristie More detailed CHANGELOG is a "must". While it won鈥檛 solve the technical issue, it will simplify the debugging.

When moving forward a codebase, tough changes might be necessary, that is obvious. However, providing a grace period for users can ease the process. E.g., you could introduce a warning in the codebase and maintain it for two minor versions before enforcing the update.
As one of the options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants