Skip to content

Commit c846b2e

Browse files
authored
Merge pull request #112 from diogobaeder/master
Properly formatting UUID if value to be unmarshalled is already a UUID.
2 parents 9aa16df + aa206d8 commit c846b2e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

openapi_core/schema/schemas/models.py

+2-1
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,
@@ -46,7 +47,7 @@ class Schema(object):
4647
format_date, TypeValidator(date, exclude=datetime)),
4748
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
4849
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
49-
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
50+
SchemaFormat.UUID: Format(format_uuid, TypeValidator(UUID)),
5051
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
5152
}
5253

openapi_core/schema/schemas/util.py

+7
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

+17
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
UndefinedSchemaProperty
@@ -49,6 +50,22 @@ def test_string_valid(self):
4950

5051
assert result == value
5152

53+
def test_string_format_uuid_valid(self):
54+
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
55+
value = str(uuid.uuid4())
56+
57+
result = schema.unmarshal(value)
58+
59+
assert result == uuid.UUID(value)
60+
61+
def test_string_format_uuid_uuid_quirks_valid(self):
62+
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
63+
value = uuid.uuid4()
64+
65+
result = schema.unmarshal(value, strict=False)
66+
67+
assert result == value
68+
5269
def test_string_float_invalid(self):
5370
schema = Schema('string')
5471
value = 1.23

0 commit comments

Comments
 (0)