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

Do not raise AttributeError when parsing non-string UUIDs #1582

Merged
merged 2 commits into from
Nov 9, 2024

Conversation

phihag
Copy link
Contributor

@phihag phihag commented Nov 7, 2024

When a user sends a dictionary or other object as a UUID variable like {[123]}, previously graphene crashed with an AttributeError, like this:

(…)
  File "…/lib/python3.12/site-packages/graphql/utils/is_valid_value.py", line 78, in is_valid_value
    parse_result = type.parse_value(value)
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "…/lib/python3.12/site-packages/graphene/types/uuid.py", line 33, in parse_value
    return _UUID(value)
           ^^^^^^^^^^^^
  File "/usr/lib/python3.12/uuid.py", line 175, in __init__
    hex = hex.replace('urn:', '').replace('uuid:', '')
          ^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'replace'

But an AttributeError makes it seem like this is the server's fault, when it's obviously the client's.

Report a proper GraphQLError.

When a user sends a dictionary or other object as a UUID variable like `{[123]}`, previously graphene crashed with an `AttributeError`, like this:

```
(…)
  File "…/lib/python3.12/site-packages/graphql/utils/is_valid_value.py", line 78, in is_valid_value
    parse_result = type.parse_value(value)
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "…/lib/python3.12/site-packages/graphene/types/uuid.py", line 33, in parse_value
    return _UUID(value)
           ^^^^^^^^^^^^
  File "/usr/lib/python3.12/uuid.py", line 175, in __init__
    hex = hex.replace('urn:', '').replace('uuid:', '')
          ^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'replace'
```

But an `AttributeError` makes it seem like this is the server's fault, when it's obviously the client's.

Report a proper GraphQLError.
@erikwrede
Copy link
Member

Good catch! I adjusted your PR to keep allowing tuple-representations of UUIDs as well, and kept the error message consistent. Thanks for the Contribution 😊

Copy link

codecov bot commented Nov 9, 2024

Codecov Report

Attention: Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.00%. Comparing base (f2e6814) to head (e354759).
Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
graphene/types/uuid.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1582      +/-   ##
==========================================
- Coverage   96.04%   96.00%   -0.04%     
==========================================
  Files          51       51              
  Lines        1720     1728       +8     
==========================================
+ Hits         1652     1659       +7     
- Misses         68       69       +1     
Flag Coverage Δ
?

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@erikwrede erikwrede merged commit 4a274b8 into graphql-python:master Nov 9, 2024
8 of 10 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.

2 participants