Skip to content

Commit a08b620

Browse files
author
Diogo Baeder de Paula Pinto
committed
Properly formatting UUID if value to be unmarshalled is already a UUID.
Before this change, if a UUID instance got received as value in the Schema, it was breaking the unmarshal because UUID instances can't be used as values to instantiate other UUIDs.
1 parent 395f68b commit a08b620

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121
from openapi_core.schema.schemas.util import (
2222
forcebool, format_date, format_datetime,
23+
format_uuid,
2324
)
2425
from openapi_core.schema.schemas.validators import (
2526
TypeValidator, AttributeValidator,
@@ -49,7 +50,7 @@ class Schema(object):
4950
format_date, TypeValidator(date, exclude=datetime)),
5051
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
5152
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
52-
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
53+
SchemaFormat.UUID: Format(format_uuid, TypeValidator(UUID)),
5354
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
5455
}
5556

openapi_core/schema/schemas/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from json import dumps
55
from six import string_types
66
import strict_rfc3339
7+
from uuid import UUID
78

89

910
def forcebool(val):
@@ -24,3 +25,9 @@ def format_date(value):
2425
def format_datetime(value):
2526
timestamp = strict_rfc3339.rfc3339_to_timestamp(value)
2627
return datetime.datetime.utcfromtimestamp(timestamp)
28+
29+
30+
def format_uuid(value):
31+
if isinstance(value, UUID):
32+
return value
33+
return UUID(value)

tests/unit/schema/test_schemas.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from openapi_core.extensions.models.models import Model
8+
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
89
from openapi_core.schema.schemas.exceptions import (
910
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema, OpenAPISchemaError,
1011
)
@@ -48,6 +49,22 @@ def test_string_valid(self):
4849

4950
assert result == value
5051

52+
def test_string_valid_uuid_str(self):
53+
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
54+
value = str(uuid.uuid4())
55+
56+
result = schema.unmarshal(value)
57+
58+
assert result == uuid.UUID(value)
59+
60+
def test_string_valid_uuid(self):
61+
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
62+
value = uuid.uuid4()
63+
64+
result = schema.unmarshal(value)
65+
66+
assert result == value
67+
5168
def test_string_none(self):
5269
schema = Schema('string')
5370
value = None

0 commit comments

Comments
 (0)