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

Add JSONResponse class #2569

Merged
merged 37 commits into from Dec 11, 2022
Merged

Add JSONResponse class #2569

merged 37 commits into from Dec 11, 2022

Conversation

prryplatypus
Copy link
Member

@prryplatypus prryplatypus commented Oct 12, 2022

Adds a new JSONResponse class that's returned for JSON responses which introduces some new JSON utilities, such as accessing the raw JSON body and the ability to modify it directly.

@codecov
Copy link

codecov bot commented Oct 12, 2022

Codecov Report

Base: 87.825% // Head: 87.853% // Increases project coverage by +0.027% 🎉

Coverage data is based on head (a76c7ca) compared to base (d404116).
Patch coverage: 84.000% of modified lines in pull request are covered.

❗ Current head a76c7ca differs from pull request most recent head 13562d8. Consider uploading reports for the commit 13562d8 to get more accurate results

Additional details and impacted files
@@              Coverage Diff              @@
##              main     #2569       +/-   ##
=============================================
+ Coverage   87.825%   87.853%   +0.027%     
=============================================
  Files           71        73        +2     
  Lines         5380      5450       +70     
  Branches       897       903        +6     
=============================================
+ Hits          4725      4788       +63     
- Misses         474       477        +3     
- Partials       181       185        +4     
Impacted Files Coverage Δ
sanic/response/types.py 83.333% <83.333%> (ø)
sanic/response/__init__.py 100.000% <100.000%> (ø)
sanic/response/convenience.py 96.396% <100.000%> (ø)
sanic/server/websockets/impl.py 38.479% <0.000%> (+0.691%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@prryplatypus prryplatypus marked this pull request as ready for review October 12, 2022 17:33
@prryplatypus prryplatypus requested a review from a team as a code owner October 12, 2022 17:34
sanic/response/types.py Outdated Show resolved Hide resolved
sanic/response/types.py Outdated Show resolved Hide resolved
sanic/response/types.py Outdated Show resolved Hide resolved
sanic/response/types.py Outdated Show resolved Hide resolved
tests/test_response.py Outdated Show resolved Hide resolved
@prryplatypus
Copy link
Member Author

prryplatypus commented Oct 12, 2022

Just thought about this... what if someone does response.raw_body.update(...)? That wouldn't work as expected. Any suggestions?

EDIT: Only solution I've been able to come up with for this is making .body a read-only property that would just .dumps the raw body when accessed. However, this would probably be a breaking change. What are your thoughts @ahopkins?

@prryplatypus
Copy link
Member Author

Alright, I've approached this differently. Main difference is that now the body doesn't get set when the raw_body is set, and instead it is generated "on-the-fly" when accessing the attribute.

This leaves us with the following outcome:

  1. It is backwards-compatible. You can set body at any point throughout the response's lifetime.
  2. The new raw_body property can be set at any point throughout the response's lifetime (as long as the body property has not been set manually).
  3. Common JSON object methods can be used directly on the raw_body without us having to implement any of the methods, and the body output will remain consistent. For example resp.raw_body.update(...) or resp.raw_body.append(...).

Only "drawback" is that dumps / dumps_kwargs can't be changed once the response has already been created, but this shouldn't be an issue, as that's the way it currently works.

Let me know your thoughts @ahopkins and if you're happy with it I'll write the tests for it.

@prryplatypus prryplatypus marked this pull request as draft October 23, 2022 15:13
@prryplatypus
Copy link
Member Author

prryplatypus commented Oct 23, 2022

Another potentially breaking change: Currently a TypeError is raised from within the constructor if a type can't be serialized by the json encoder. With this change mentioned above it would only be raised when calling resp.body. As a workaround we can just add self.body somewhere in the constructor, but this is not optimal as it would make every response be serialized twice.

However, we could potentially leave this in only for a deprecation period and then remove it (not a fan of it however). Thoughts welcome.

@prryplatypus prryplatypus marked this pull request as ready for review November 5, 2022 18:03
@ahopkins
Copy link
Member

ahopkins commented Nov 6, 2022

Remind me... where did we settle on the API for this?

@prryplatypus
Copy link
Member Author

You suggested doing a json.loads if the body was set manually, but I disagreed because it feels like a cross of domains. Therefore, I think we were both on the same page with documenting that raw_body should not be mutated at all, unless after mutating it you called .set_json(raw_body) or did resp.raw_body = resp.raw_body (which would trigger a dumps call with the new raw body).

That said, we could potentially add the "common methods" that you mentioned previously, such as append or update, if so you wish.

@prryplatypus
Copy link
Member Author

@ahopkins API now should be as agreed 😎

sanic/response/types.py Outdated Show resolved Hide resolved
Copy link
Member

@ahopkins ahopkins left a comment

Choose a reason for hiding this comment

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

The json convenience method needs its return type updated.

@prryplatypus
Copy link
Member Author

The json convenience method needs its return type updated.

It is updated, I believe? https://github.com/sanic-org/sanic/pull/2569/files#diff-9e98d5a6f92def2f06f426094980026882610a5e8d8215b9b291b58620f0f15fR50-R57

@ahopkins
Copy link
Member

ahopkins commented Dec 8, 2022

It is updated, I believe?

image

@prryplatypus
Copy link
Member Author

prryplatypus commented Dec 8, 2022

Ahh the typehint! Done.

@ahopkins ahopkins merged commit 8e72036 into main Dec 11, 2022
@ahopkins ahopkins deleted the prry/json-response branch December 11, 2022 08:37
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

2 participants