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

Support custom JSON decoders for views #3709

Merged
merged 3 commits into from
Nov 22, 2024

Conversation

DoctorJohn
Copy link
Member

@DoctorJohn DoctorJohn commented Nov 21, 2024

Description

This PR introduces a decode_json method which allows users to customize decoding of HTTP JSON requests and WebSocket messages. This is similar to the encode_json method which we already had for a while.

Since this is a new method, this PR also includes docs and tests making sure HTTP, Multipart, and WebSocket protocols all use this new method.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Summary by Sourcery

Introduce a decode_json method to allow custom JSON decoding in HTTP and WebSocket protocols, update documentation with usage examples, and add tests to verify the new functionality.

New Features:

  • Introduce a decode_json method in view classes to allow customization of JSON decoding for HTTP requests and WebSocket messages.

Documentation:

  • Add documentation for the new decode_json method across various integrations, explaining its usage and providing examples.

Tests:

  • Add tests to ensure the decode_json method is used in HTTP, Multipart, and WebSocket protocols.

Copy link
Contributor

sourcery-ai bot commented Nov 21, 2024

Reviewer's Guide by Sourcery

This PR introduces a new decode_json method across all framework integrations to allow customization of JSON request decoding for HTTP and WebSocket messages. The implementation includes comprehensive test coverage and documentation updates for all supported frameworks. The changes are similar to the existing encode_json functionality but for request decoding.

Class diagram for the new decode_json method

classDiagram
    class BaseView {
        +decode_json(data: Union<str, bytes>) object
        +encode_json(data: object) str
    }
    class AsyncBaseHTTPView {
        +decode_json(data: Union<str, bytes>) object
        +encode_json(data: object) str
    }
    class GraphQLView {
        +decode_json(data: Union<str, bytes>) object
        +encode_json(data: object) str
    }
    class AsyncGraphQLView {
        +decode_json(data: Union<str, bytes>) object
        +encode_json(data: object) str
    }
    class GraphQLRouter {
        +decode_json(data: Union<str, bytes>) object
        +encode_json(data: object) str
    }
    class MyGraphQLView {
        +decode_json(data: Union<str, bytes>) object
    }
    class MyGraphQLRouter {
        +decode_json(data: Union<str, bytes>) object
    }
    BaseView <|-- AsyncBaseHTTPView
    BaseView <|-- GraphQLView
    BaseView <|-- AsyncGraphQLView
    BaseView <|-- GraphQLRouter
    GraphQLView <|-- MyGraphQLView
    GraphQLRouter <|-- MyGraphQLRouter
    note for BaseView "Base class for views with JSON encoding/decoding"
    note for MyGraphQLView "Example subclass overriding decode_json"
    note for MyGraphQLRouter "Example subclass overriding decode_json"
Loading

File-Level Changes

Change Details Files
Added new decode_json method to base HTTP view class
  • Implemented decode_json method that defaults to json.loads
  • Updated parse_json to use the new decode_json method
  • Added type hints for decode_json parameters and return type
strawberry/http/base.py
Updated WebSocket handlers to use decode_json
  • Modified WebSocket message parsing to use view's decode_json method
  • Updated return type annotations for iter_json to use generic object type
  • Refactored message handling to separate text reception from JSON decoding
strawberry/asgi/__init__.py
strawberry/aiohttp/views.py
strawberry/channels/handlers/ws_handler.py
strawberry/litestar/controller.py
strawberry/http/async_base_view.py
Added comprehensive test coverage
  • Added tests for decode_json usage in HTTP handlers
  • Added tests for decode_json in WebSocket handlers
  • Added tests for decode_json in multipart subscriptions
tests/http/test_http.py
tests/websockets/test_websockets.py
tests/http/test_multipart_subscription.py
Updated documentation for all framework integrations
  • Added decode_json method documentation with examples
  • Added notes about JSONDecodeError requirements
  • Included orjson example for all frameworks
docs/integrations/asgi.md
docs/integrations/aiohttp.md
docs/integrations/django.md
docs/integrations/fastapi.md
docs/integrations/chalice.md
docs/integrations/flask.md
docs/integrations/quart.md
docs/integrations/sanic.md

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @DoctorJohn - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

docs/integrations/aiohttp.md Show resolved Hide resolved
docs/integrations/django.md Show resolved Hide resolved
docs/integrations/fastapi.md Show resolved Hide resolved
@botberry
Copy link
Member

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


The view classes of all integrations now have a decode_json method that allows
you to customize the decoding of HTTP JSON requests.

This is useful if you want to use a different JSON decoder, for example, to
optimize performance.

Here's the tweet text:

🆕 Release (next) is out! Thanks to @NucleonJohn for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

Copy link

codspeed-hq bot commented Nov 21, 2024

CodSpeed Performance Report

Merging #3709 will not alter performance

Comparing DoctorJohn:add-decode-json (b2b1323) with main (a3dd2df)

Summary

✅ 15 untouched benchmarks

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.00%. Comparing base (a3dd2df) to head (b2b1323).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3709      +/-   ##
==========================================
- Coverage   97.01%   97.00%   -0.02%     
==========================================
  Files         500      500              
  Lines       33454    33483      +29     
  Branches     5591     5593       +2     
==========================================
+ Hits        32457    32479      +22     
  Misses        793      793              
- Partials      204      211       +7     
---- 🚨 Try these New Features:

@patrick91 patrick91 merged commit e8f4b6e into strawberry-graphql:main Nov 22, 2024
111 of 113 checks passed
@DoctorJohn DoctorJohn deleted the add-decode-json branch November 22, 2024 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants