From a08b62035e49eecf72ef9889be34dc42093fa53e Mon Sep 17 00:00:00 2001 From: Diogo Baeder de Paula Pinto Date: Thu, 28 Feb 2019 18:25:22 -0300 Subject: [PATCH 1/3] 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. --- openapi_core/schema/schemas/models.py | 3 ++- openapi_core/schema/schemas/util.py | 7 +++++++ tests/unit/schema/test_schemas.py | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/openapi_core/schema/schemas/models.py b/openapi_core/schema/schemas/models.py index 6ae4dc9a..f221e506 100644 --- a/openapi_core/schema/schemas/models.py +++ b/openapi_core/schema/schemas/models.py @@ -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, @@ -49,7 +50,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)), } diff --git a/openapi_core/schema/schemas/util.py b/openapi_core/schema/schemas/util.py index 19c8b5cb..7c274ecc 100644 --- a/openapi_core/schema/schemas/util.py +++ b/openapi_core/schema/schemas/util.py @@ -4,6 +4,7 @@ from json import dumps from six import string_types import strict_rfc3339 +from uuid import UUID def forcebool(val): @@ -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) diff --git a/tests/unit/schema/test_schemas.py b/tests/unit/schema/test_schemas.py index 302719fb..a6b415d5 100644 --- a/tests/unit/schema/test_schemas.py +++ b/tests/unit/schema/test_schemas.py @@ -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, ) @@ -48,6 +49,22 @@ def test_string_valid(self): assert result == value + def test_string_valid_uuid_str(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_valid_uuid(self): + schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID) + value = uuid.uuid4() + + result = schema.unmarshal(value) + + assert result == value + def test_string_none(self): schema = Schema('string') value = None From d0e163b5baf435fa591b79f77185b6e5b4076d8b Mon Sep 17 00:00:00 2001 From: Artur Maciag Date: Fri, 22 Mar 2019 14:05:54 +0000 Subject: [PATCH 2/3] Test string format UUID non strict fix --- tests/unit/schema/test_schemas.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/schema/test_schemas.py b/tests/unit/schema/test_schemas.py index 6bedd65f..64407386 100644 --- a/tests/unit/schema/test_schemas.py +++ b/tests/unit/schema/test_schemas.py @@ -50,7 +50,7 @@ def test_string_valid(self): assert result == value - def test_string_valid_uuid_str(self): + def test_string_format_uuid_valid(self): schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID) value = str(uuid.uuid4()) @@ -58,11 +58,11 @@ def test_string_valid_uuid_str(self): assert result == uuid.UUID(value) - def test_string_valid_uuid(self): + def test_string_format_uuid_valid(self): schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID) value = uuid.uuid4() - result = schema.unmarshal(value) + result = schema.unmarshal(value, strict=False) assert result == value From aa206d868e9cd20a5b224cdf265bd38f7538810f Mon Sep 17 00:00:00 2001 From: Artur Maciag Date: Fri, 22 Mar 2019 14:14:42 +0000 Subject: [PATCH 3/3] Test string format quirks rename fix --- tests/unit/schema/test_schemas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/schema/test_schemas.py b/tests/unit/schema/test_schemas.py index 64407386..4eedcc27 100644 --- a/tests/unit/schema/test_schemas.py +++ b/tests/unit/schema/test_schemas.py @@ -58,7 +58,7 @@ def test_string_format_uuid_valid(self): assert result == uuid.UUID(value) - def test_string_format_uuid_valid(self): + def test_string_format_uuid_uuid_quirks_valid(self): schema = Schema(SchemaType.STRING, schema_format=SchemaFormat.UUID) value = uuid.uuid4()