Skip to content

Commit 2311829

Browse files
authored
Merge pull request #109 from diogobaeder/master
Accepting uuid string format and validating accordingly.
2 parents 922fce4 + 48d1d1c commit 2311829

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

openapi_core/schema/schemas/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ class SchemaFormat(Enum):
2525
DATE = 'date'
2626
DATETIME = 'date-time'
2727
PASSWORD = 'password'
28+
UUID = 'uuid'

openapi_core/schema/schemas/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99

1010
from six import iteritems, integer_types, binary_type, text_type
11+
from uuid import UUID
1112

1213
from openapi_core.extensions.models.factories import ModelFactory
1314
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
@@ -46,6 +47,7 @@ class Schema(object):
4647
SchemaFormat.DATE: Format(format_date, TypeValidator(date, exclude=datetime)),
4748
SchemaFormat.DATETIME: Format(format_datetime, TypeValidator(datetime)),
4849
SchemaFormat.BINARY: Format(binary_type, TypeValidator(binary_type)),
50+
SchemaFormat.UUID: Format(UUID, TypeValidator(UUID)),
4951
}
5052

5153
TYPE_VALIDATOR_CALLABLE_GETTER = {
@@ -54,7 +56,7 @@ class Schema(object):
5456
SchemaType.INTEGER: TypeValidator(integer_types, exclude=bool),
5557
SchemaType.NUMBER: TypeValidator(integer_types, float, exclude=bool),
5658
SchemaType.STRING: TypeValidator(
57-
text_type, date, datetime, binary_type),
59+
text_type, date, datetime, binary_type, UUID),
5860
SchemaType.ARRAY: TypeValidator(list, tuple),
5961
SchemaType.OBJECT: AttributeValidator('__dict__'),
6062
}

tests/integration/data/v3.0/petstore.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ components:
317317
required:
318318
- rootCause
319319
properties:
320+
correlationId:
321+
type: string
322+
format: uuid
320323
rootCause:
321324
type: string
322325
suberror:

tests/integration/test_petstore.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import pytest
3+
from uuid import UUID
34
from six import iteritems
45

56
from openapi_core.extensions.models.models import BaseModel
@@ -1154,11 +1155,13 @@ def test_post_tags_created_invalid_type(
11541155

11551156
code = 400
11561157
message = 'Bad request'
1158+
correlationId = UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')
11571159
rootCause = 'Tag already exist'
11581160
additionalinfo = 'Tag Dog already exist'
11591161
data_json = {
11601162
'code': code,
11611163
'message': message,
1164+
'correlationId': str(correlationId),
11621165
'rootCause': rootCause,
11631166
'additionalinfo': additionalinfo,
11641167
}
@@ -1171,5 +1174,6 @@ def test_post_tags_created_invalid_type(
11711174
assert isinstance(response_result.data, BaseModel)
11721175
assert response_result.data.code == code
11731176
assert response_result.data.message == message
1177+
assert response_result.data.correlationId == correlationId
11741178
assert response_result.data.rootCause == rootCause
11751179
assert response_result.data.additionalinfo == additionalinfo

tests/unit/schema/test_schemas.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import uuid
23

34
import mock
45
import pytest
@@ -416,6 +417,26 @@ def test_string_format_date(self, value):
416417

417418
assert result == value
418419

420+
@pytest.mark.parametrize('value', [
421+
uuid.UUID('{12345678-1234-5678-1234-567812345678}'),
422+
])
423+
def test_string_format_uuid(self, value):
424+
schema = Schema('string', schema_format='uuid')
425+
426+
result = schema.validate(value)
427+
428+
assert result == value
429+
430+
@pytest.mark.parametrize('value', [
431+
b('true'), u('true'), False, 1, 3.14, [1, 3],
432+
datetime.date(2018, 1, 2), datetime.datetime(2018, 1, 2, 23, 59, 59),
433+
])
434+
def test_string_format_uuid_invalid(self, value):
435+
schema = Schema('string', schema_format='uuid')
436+
437+
with pytest.raises(InvalidSchemaValue):
438+
schema.validate(value)
439+
419440
@pytest.mark.parametrize('value', [
420441
b('true'), u('true'), False, 1, 3.14, [1, 3],
421442
datetime.date(1989, 1, 2),

0 commit comments

Comments
 (0)