Skip to content

Accepting uuid string format and validating accordingly. #109

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 2 commits into from
Feb 26, 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
1 change: 1 addition & 0 deletions openapi_core/schema/schemas/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ class SchemaFormat(Enum):
DATE = 'date'
DATETIME = 'date-time'
PASSWORD = 'password'
UUID = 'uuid'
4 changes: 3 additions & 1 deletion openapi_core/schema/schemas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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__'),
}
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/data/v3.0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ components:
required:
- rootCause
properties:
correlationId:
type: string
format: uuid
rootCause:
type: string
suberror:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import pytest
from uuid import UUID
from six import iteritems

from openapi_core.extensions.models.models import BaseModel
Expand Down Expand Up @@ -1154,11 +1155,13 @@ def test_post_tags_created_invalid_type(

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,
}
Expand All @@ -1171,5 +1174,6 @@ def test_post_tags_created_invalid_type(
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
21 changes: 21 additions & 0 deletions tests/unit/schema/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import uuid

import mock
import pytest
Expand Down Expand Up @@ -416,6 +417,26 @@ def test_string_format_date(self, value):

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),
Expand Down