Skip to content

Commit

Permalink
fix: raise proper error when UUID parsing fails (#1582)
Browse files Browse the repository at this point in the history
* Do not raise AttributeError when parsing non-string UUIDs

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.

* fix: adjust exception message structure

---------

Co-authored-by: Erik Wrede <erikwrede@users.noreply.github.com>
  • Loading branch information
phihag and erikwrede authored Nov 9, 2024
1 parent b3db1c0 commit 4a274b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions graphene/types/tests/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ def test_uuidstring_query_variable():
assert result.data == {"uuid": uuid_value}


def test_uuidstring_invalid_argument():
uuid_value = {"not": "a string"}

result = schema.execute(
"""query Test($uuid: UUID){ uuid(input: $uuid) }""",
variables={"uuid": uuid_value},
)
assert result.errors
assert len(result.errors) == 1
assert (
result.errors[0].message
== "Variable '$uuid' got invalid value {'not': 'a string'}; UUID cannot represent value: {'not': 'a string'}"
)


def test_uuidstring_optional_uuid_input():
"""
Test that we can provide a null value to an optional input
Expand Down
8 changes: 7 additions & 1 deletion graphene/types/uuid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from uuid import UUID as _UUID

from graphql.error import GraphQLError
from graphql.language.ast import StringValueNode
from graphql import Undefined

Expand Down Expand Up @@ -28,4 +29,9 @@ def parse_literal(node, _variables=None):

@staticmethod
def parse_value(value):
return _UUID(value)
if isinstance(value, _UUID):
return value
try:
return _UUID(value)
except (ValueError, AttributeError):
raise GraphQLError(f"UUID cannot represent value: {repr(value)}")

0 comments on commit 4a274b8

Please sign in to comment.