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(printer): Fix printer ignoring input arguments using snake_case #3780

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

bellini666
Copy link
Member

@bellini666 bellini666 commented Feb 12, 2025

Fix #3760

Summary by Sourcery

Bug Fixes:

  • Fix an issue where directives with snake_case input arguments were not printed correctly in the schema.

@bellini666 bellini666 self-assigned this Feb 12, 2025
Copy link
Contributor

sourcery-ai bot commented Feb 12, 2025

Reviewer's Guide by Sourcery

This pull request fixes an issue with printer behavior where input arguments in snake_case were being ignored. It introduces a new parameter 'name_converter' in the serialization function to correctly process snake_case keys, propagates this change through the directive printing functions, adds a corresponding test, and documents the change in the release notes.

Sequence diagram for schema directive printing with name conversion

sequenceDiagram
    participant SDP as SchemaDirectivePrinter
    participant SD as _serialize_dataclasses
    participant NC as NameConverter
    participant AV as ast_from_value
    participant PA as print_ast

    SDP->>SD: Call _serialize_dataclasses(value, name_converter=schema.config.name_converter.apply_naming_config)
    SD->>NC: For each key in dataclass, convert key (name_converter(key))
    NC-->>SD: Return converted key
    SD-->>SD: Build and return serialized object
    SD-->>SD: (Recursive calls for list/tuple and dict branches)
    SD-->>SD: Return serialized value
    SD-->>SD: (Return serialized result)
    SDP->>AV: Call ast_from_value(serialized_value, arg.type)
    AV-->>SDP: Return AST
    SDP->>PA: Call print_ast(AST)
    PA-->>SDP: Return printed directive argument
    SDP-->>SDP: Combine params to form final printed directive
Loading

File-Level Changes

Change Details Files
Modified the _serialize_dataclasses function to support dynamic name conversion.
  • Added a new optional parameter 'name_converter' with a default identity function to _serialize_dataclasses overloads and implementation.
  • Updated dictionary comprehensions to apply 'name_converter' to keys for dataclasses and dictionaries.
strawberry/printer/printer.py
Updated directive printing logic to utilize the new serialization parameter.
  • Modified the print_schema_directive_params function to pass the schema-specific name_converter to _serialize_dataclasses.
  • Propagated the schema parameter where necessary to maintain consistency in name conversion.
strawberry/printer/printer.py
Added a test case to validate the correct printing of snake_case input arguments.
  • Created a new test 'test_print_directive_with_snake_case_arguments' in the printer test suite to check for proper conversion of snake_case to camelCase (or configured naming) in schema directives.
tests/test_printer/test_schema_directives.py
Included release notes documenting the bug fix and the changes to input argument naming.
  • Added a new RELEASE.md file outlining the patch release and providing an example of the fixed behavior.
RELEASE.md

Assessment against linked issues

Issue Objective Addressed Explanation
#3760 Ensure that input arguments using snake_case are correctly serialized in the schema
#3760 Fix the schema printer to include all input arguments, including those with snake_case names

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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

@bellini666 bellini666 requested a review from patrick91 February 12, 2025 17:34
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 @bellini666 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It might be worth adding a test case that uses a schema directive on a type, to ensure that the name conversion works in that case as well.
Here's what I looked at during the review
  • 🟡 General issues: 2 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.

strawberry/printer/printer.py Outdated Show resolved Hide resolved
RELEASE.md Outdated Show resolved Hide resolved
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@botberry
Copy link
Member

botberry commented Feb 12, 2025

Apollo Federation Subgraph Compatibility Results

Federation 1 Support Federation 2 Support
_service🟢
@key (single)🟢
@key (multi)🟢
@key (composite)🟢
repeatable @key🟢
@requires🟢
@provides🟢
federated tracing🔲
@link🟢
@shareable🟢
@tag🟢
@override🟢
@inaccessible🟢
@composeDirective🟢
@interfaceObject🟢

Learn more:

bellini666 and others added 2 commits February 12, 2025 18:36
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@botberry
Copy link
Member

botberry commented Feb 12, 2025

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes an issue where directives with input types using snake_case
would not be printed in the schema.

For example, the following:

@strawberry.input
class FooInput:
    hello: str
    hello_world: str


@strawberry.schema_directive(locations=[Location.FIELD_DEFINITION])
class FooDirective:
    input: FooInput


@strawberry.type
class Query:
    @strawberry.field(
        directives=[
            FooDirective(input=FooInput(hello="hello", hello_world="hello world")),
        ]
    )
    def foo(self, info) -> str: ...

Would previously print as:

directive @fooDirective(
  input: FooInput!
  optionalInput: FooInput
) on FIELD_DEFINITION

type Query {
  foo: String! @fooDirective(input: { hello: "hello" })
}

input FooInput {
  hello: String!
  hello_world: String!
}

Now it will be correctly printed as:

directive @fooDirective(
  input: FooInput!
  optionalInput: FooInput
) on FIELD_DEFINITION

type Query {
  foo: String!
    @fooDirective(input: { hello: "hello", helloWorld: "hello world" })
}

input FooInput {
  hello: String!
  hello_world: String!
}

Here's the tweet text:

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

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

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 97.23%. Comparing base (3c0f9e4) to head (14f7607).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3780      +/-   ##
==========================================
- Coverage   97.24%   97.23%   -0.01%     
==========================================
  Files         502      502              
  Lines       33460    33501      +41     
  Branches     1702     1716      +14     
==========================================
+ Hits        32537    32575      +38     
- Misses        706      707       +1     
- Partials      217      219       +2     

Copy link

codspeed-hq bot commented Feb 12, 2025

CodSpeed Performance Report

Merging #3780 will not alter performance

Comparing fix_directive_printer (14f7607) with main (4c4e07b)

Summary

✅ 21 untouched benchmarks

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.

snake_case input arguments are ignored in schema serialization
2 participants