Skip to content

Commit 6cfc4c7

Browse files
committed
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.
1 parent ccae736 commit 6cfc4c7

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

graphene/types/tests/test_uuid.py

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ def test_uuidstring_query_variable():
3636
assert result.data == {"uuid": uuid_value}
3737

3838

39+
def test_uuidstring_invalid_argument():
40+
uuid_value = {"not": "a string"}
41+
42+
result = schema.execute(
43+
"""query Test($uuid: UUID){ uuid(input: $uuid) }""",
44+
variables={"uuid": uuid_value},
45+
)
46+
assert result.errors
47+
assert len(result.errors) == 1
48+
assert (
49+
result.errors[0].message
50+
== "Variable '$uuid' got invalid value {'not': 'a string'}; UUID must be a string"
51+
)
52+
53+
3954
def test_uuidstring_optional_uuid_input():
4055
"""
4156
Test that we can provide a null value to an optional input

graphene/types/uuid.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from uuid import UUID as _UUID
22

3+
from graphql.error import GraphQLError
34
from graphql.language.ast import StringValueNode
45
from graphql import Undefined
56

@@ -28,4 +29,6 @@ def parse_literal(node, _variables=None):
2829

2930
@staticmethod
3031
def parse_value(value):
32+
if not isinstance(value, (str, _UUID)):
33+
raise GraphQLError("UUID must be a string")
3134
return _UUID(value)

0 commit comments

Comments
 (0)