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: skip empty choice value #687

Merged

Conversation

fabien-michel
Copy link
Contributor

@fabien-michel fabien-michel commented Jan 6, 2025

Seems they are not supported by GraphQL

Having an empty choice make generate enum from choice feature crash, trying to use None as a string.

class Choice(models.TextChoices):
    A = "a", "A description"
    B = "b", "B description"
    __empty__ =  "Empty"

Description

The fix, for now, is to skip empty value, so the behavior is the same as TextChoiceField.
It make the schema building pass, but sadly, data with an empty value will still fail with Enum 'MyAutoGeneratedEnum' cannot represent value: '' and I'm note sure what to do with that.

Types of Changes

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

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Summary by Sourcery

Bug Fixes:

  • Prevent crashes when generating enums from choices with empty values by skipping them.

Seems they are not supported by GraphQL
Copy link
Contributor

sourcery-ai bot commented Jan 6, 2025

Reviewer's Guide by Sourcery

This pull request fixes a bug where empty choice values in Django models were causing crashes when generating enums. The fix skips empty choice values during enum generation, aligning the behavior with TextChoiceField.

Sequence diagram for enum generation with empty choice handling

sequenceDiagram
    participant Django as Django Model Field
    participant Resolver as Field Type Resolver
    participant GraphQL as GraphQL Schema

    Django->>Resolver: Get choices from model field
    activate Resolver
    loop For each choice
        alt Choice value is empty
            Resolver-->>Resolver: Skip empty choice
        else Choice value is not empty
            Resolver->>Resolver: Convert choice to GraphQL enum
            Note right of Resolver: Clean name using regex
        end
    end
    Resolver->>GraphQL: Return enum type
    deactivate Resolver
Loading

Class diagram showing Choice model changes

classDiagram
    class Choice {
        <<TextChoices>>
        +A: str
        +B: str
        +__empty__: str
        +get_choices()
    }
    note for Choice "Empty choices are now skipped
during GraphQL enum generation"
Loading

File-Level Changes

Change Details Files
Skip empty choice values during enum generation.
  • Added a condition to skip empty choice values in the resolve_model_field_type function.
  • Added a test case for empty choice values in the test_enums.py file.
  • Modified the type hint for choices to include `str
None` to handle empty choices correctly.

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 @fabien-michel - I've reviewed your changes - here's some feedback:

Overall Comments:

  • You've marked that this change requires documentation updates, but no documentation changes were made. Please update the documentation to reflect that empty choices are skipped in GraphQL enum generation.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 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.

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.01%. Comparing base (0b28264) to head (ecdaa64).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #687   +/-   ##
=======================================
  Coverage   89.00%   89.01%           
=======================================
  Files          42       42           
  Lines        3821     3823    +2     
=======================================
+ Hits         3401     3403    +2     
  Misses        420      420           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fabien-michel
Copy link
Contributor Author

fabien-michel commented Jan 6, 2025

Hmm, seems the problem is more than that.
Having object with an enum field value set to empty string, make GraphQL fail with Enum 'MyAutoGeneratedEnum' cannot represent value: ''

I'm not sure what to do here.

@bellini666
Copy link
Member

Ty! :)

@bellini666 bellini666 merged commit 266895a into strawberry-graphql:main Jan 7, 2025
28 checks passed
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.

3 participants