mirror of
https://github.com/correl/openapi-core.git
synced 2025-01-06 11:04:50 +00:00
Merge pull request #112 from diogobaeder/master
Properly formatting UUID if value to be unmarshalled is already a UUID.
This commit is contained in:
commit
c846b2e453
3 changed files with 26 additions and 1 deletions
|
@ -20,6 +20,7 @@ from openapi_core.schema.schemas.exceptions import (
|
||||||
)
|
)
|
||||||
from openapi_core.schema.schemas.util import (
|
from openapi_core.schema.schemas.util import (
|
||||||
forcebool, format_date, format_datetime,
|
forcebool, format_date, format_datetime,
|
||||||
|
format_uuid,
|
||||||
)
|
)
|
||||||
from openapi_core.schema.schemas.validators import (
|
from openapi_core.schema.schemas.validators import (
|
||||||
TypeValidator, AttributeValidator,
|
TypeValidator, AttributeValidator,
|
||||||
|
@ -46,7 +47,7 @@ class Schema(object):
|
||||||
format_date, TypeValidator(date, exclude=datetime)),
|
format_date, TypeValidator(date, exclude=datetime)),
|
||||||
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
|
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
|
||||||
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
|
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)),
|
SchemaFormat.BYTE: Format(b64decode, TypeValidator(binary_type)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ from distutils.util import strtobool
|
||||||
from json import dumps
|
from json import dumps
|
||||||
from six import string_types
|
from six import string_types
|
||||||
import strict_rfc3339
|
import strict_rfc3339
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
|
||||||
def forcebool(val):
|
def forcebool(val):
|
||||||
|
@ -24,3 +25,9 @@ def format_date(value):
|
||||||
def format_datetime(value):
|
def format_datetime(value):
|
||||||
timestamp = strict_rfc3339.rfc3339_to_timestamp(value)
|
timestamp = strict_rfc3339.rfc3339_to_timestamp(value)
|
||||||
return datetime.datetime.utcfromtimestamp(timestamp)
|
return datetime.datetime.utcfromtimestamp(timestamp)
|
||||||
|
|
||||||
|
|
||||||
|
def format_uuid(value):
|
||||||
|
if isinstance(value, UUID):
|
||||||
|
return value
|
||||||
|
return UUID(value)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from openapi_core.extensions.models.models import Model
|
from openapi_core.extensions.models.models import Model
|
||||||
|
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
|
||||||
from openapi_core.schema.schemas.exceptions import (
|
from openapi_core.schema.schemas.exceptions import (
|
||||||
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema, OpenAPISchemaError,
|
InvalidSchemaValue, MultipleOneOfSchema, NoOneOfSchema, OpenAPISchemaError,
|
||||||
UndefinedSchemaProperty
|
UndefinedSchemaProperty
|
||||||
|
@ -49,6 +50,22 @@ class TestSchemaUnmarshal(object):
|
||||||
|
|
||||||
assert result == value
|
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):
|
def test_string_float_invalid(self):
|
||||||
schema = Schema('string')
|
schema = Schema('string')
|
||||||
value = 1.23
|
value = 1.23
|
||||||
|
|
Loading…
Reference in a new issue