From dadf075670d9bcb146120dd9ba37fdc3c1708a99 Mon Sep 17 00:00:00 2001 From: Diogo Baeder de Paula Pinto Date: Tue, 26 Feb 2019 02:57:50 -0300 Subject: [PATCH 1/2] Accepting uuid string format and validating accordingly. --- openapi_core/schema/schemas/enums.py | 1 + openapi_core/schema/schemas/models.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/openapi_core/schema/schemas/enums.py b/openapi_core/schema/schemas/enums.py index 648ad5b..8b77e7c 100644 --- a/openapi_core/schema/schemas/enums.py +++ b/openapi_core/schema/schemas/enums.py @@ -25,3 +25,4 @@ class SchemaFormat(Enum): DATE = 'date' DATETIME = 'date-time' PASSWORD = 'password' + UUID = 'uuid' diff --git a/openapi_core/schema/schemas/models.py b/openapi_core/schema/schemas/models.py index 9429424..cea36d7 100644 --- a/openapi_core/schema/schemas/models.py +++ b/openapi_core/schema/schemas/models.py @@ -8,6 +8,7 @@ import re import warnings from six import iteritems, integer_types, binary_type, text_type +from uuid import UUID from openapi_core.extensions.models.factories import ModelFactory from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType @@ -46,6 +47,7 @@ class Schema(object): SchemaFormat.DATE: Format(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)), } TYPE_VALIDATOR_CALLABLE_GETTER = { @@ -54,7 +56,7 @@ class Schema(object): SchemaType.INTEGER: TypeValidator(integer_types, exclude=bool), SchemaType.NUMBER: TypeValidator(integer_types, float, exclude=bool), SchemaType.STRING: TypeValidator( - text_type, date, datetime, binary_type), + text_type, date, datetime, binary_type, UUID), SchemaType.ARRAY: TypeValidator(list, tuple), SchemaType.OBJECT: AttributeValidator('__dict__'), } From 48d1d1c8ae13b6d21adf1f05e480eb2473b0f216 Mon Sep 17 00:00:00 2001 From: Artur Maciag Date: Tue, 26 Feb 2019 10:37:49 +0000 Subject: [PATCH 2/2] UUID string format tests --- tests/integration/data/v3.0/petstore.yaml | 3 +++ tests/integration/test_petstore.py | 4 ++++ tests/unit/schema/test_schemas.py | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/tests/integration/data/v3.0/petstore.yaml b/tests/integration/data/v3.0/petstore.yaml index 2dc3083..733c587 100644 --- a/tests/integration/data/v3.0/petstore.yaml +++ b/tests/integration/data/v3.0/petstore.yaml @@ -317,6 +317,9 @@ components: required: - rootCause properties: + correlationId: + type: string + format: uuid rootCause: type: string suberror: diff --git a/tests/integration/test_petstore.py b/tests/integration/test_petstore.py index d7fd1a0..f1068c1 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/test_petstore.py @@ -1,5 +1,6 @@ import json import pytest +from uuid import UUID from six import iteritems from openapi_core.extensions.models.models import BaseModel @@ -1154,11 +1155,13 @@ class TestPetstore(object): code = 400 message = 'Bad request' + correlationId = UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') rootCause = 'Tag already exist' additionalinfo = 'Tag Dog already exist' data_json = { 'code': code, 'message': message, + 'correlationId': str(correlationId), 'rootCause': rootCause, 'additionalinfo': additionalinfo, } @@ -1171,5 +1174,6 @@ class TestPetstore(object): assert isinstance(response_result.data, BaseModel) assert response_result.data.code == code assert response_result.data.message == message + assert response_result.data.correlationId == correlationId assert response_result.data.rootCause == rootCause assert response_result.data.additionalinfo == additionalinfo diff --git a/tests/unit/schema/test_schemas.py b/tests/unit/schema/test_schemas.py index fb9675e..47f4011 100644 --- a/tests/unit/schema/test_schemas.py +++ b/tests/unit/schema/test_schemas.py @@ -1,4 +1,5 @@ import datetime +import uuid import mock import pytest @@ -416,6 +417,26 @@ class TestSchemaValidate(object): assert result == value + @pytest.mark.parametrize('value', [ + uuid.UUID('{12345678-1234-5678-1234-567812345678}'), + ]) + def test_string_format_uuid(self, value): + schema = Schema('string', schema_format='uuid') + + result = schema.validate(value) + + assert result == value + + @pytest.mark.parametrize('value', [ + b('true'), u('true'), False, 1, 3.14, [1, 3], + datetime.date(2018, 1, 2), datetime.datetime(2018, 1, 2, 23, 59, 59), + ]) + def test_string_format_uuid_invalid(self, value): + schema = Schema('string', schema_format='uuid') + + with pytest.raises(InvalidSchemaValue): + schema.validate(value) + @pytest.mark.parametrize('value', [ b('true'), u('true'), False, 1, 3.14, [1, 3], datetime.date(1989, 1, 2),