Skip to content

Properly formatting UUID if value to be unmarshalled is already a UUID. #112

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

Merged
merged 4 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion openapi_core/schema/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from openapi_core.schema.schemas.util import (
forcebool, format_date, format_datetime,
format_uuid,
)
from openapi_core.schema.schemas.validators import (
TypeValidator, AttributeValidator,
Expand All @@ -46,7 +47,7 @@ class Schema(object):
format_date, TypeValidator(date, exclude=datetime)),
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
SchemaFormat.UUID: Format(format_uuid, TypeValidator(UUID)),
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
}

Expand Down
7 changes: 7 additions & 0 deletions openapi_core/schema/schemas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from json import dumps
from six import string_types
import strict_rfc3339
from uuid import UUID


def forcebool(val):
Expand All @@ -24,3 +25,9 @@ def format_date(value):
def format_datetime(value):
timestamp = strict_rfc3339.rfc3339_to_timestamp(value)
return datetime.datetime.utcfromtimestamp(timestamp)


def format_uuid(value):
if isinstance(value, UUID):
return value
return UUID(value)
17 changes: 17 additions & 0 deletions tests/unit/schema/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from openapi_core.extensions.models.models import Model
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
from openapi_core.schema.schemas.exceptions import (
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema, OpenAPISchemaError,
UndefinedSchemaProperty
Expand Down Expand Up @@ -49,6 +50,22 @@ def test_string_valid(self):

assert result == value

def test_string_format_uuid_valid(self):
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
value = str(uuid.uuid4())

result = schema.unmarshal(value)

assert result == uuid.UUID(value)

def test_string_format_uuid_uuid_quirks_valid(self):
schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID)
value = uuid.uuid4()

result = schema.unmarshal(value, strict=False)

assert result == value

def test_string_float_invalid(self):
schema = Schema('string')
value = 1.23
Expand Down