Skip to content

Commit ce3c40f

Browse files
committed
Any schema type
1 parent 17855ae commit ce3c40f

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

openapi_core/schema/schemas/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
class SchemaType(Enum):
66

7+
ANY = None
78
INTEGER = 'integer'
89
NUMBER = 'number'
910
STRING = 'string'

openapi_core/schema/schemas/factories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, dereferencer):
1616
def create(self, schema_spec):
1717
schema_deref = self.dereferencer.dereference(schema_spec)
1818

19-
schema_type = schema_deref.get('type', 'object')
19+
schema_type = schema_deref.get('type', None)
2020
schema_format = schema_deref.get('format')
2121
model = schema_deref.get('x-model', None)
2222
required = schema_deref.get('required', False)

openapi_core/schema/schemas/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Schema(object):
2323
"""Represents an OpenAPI Schema."""
2424

2525
DEFAULT_CAST_CALLABLE_GETTER = {
26+
SchemaType.ANY: lambda x: x,
2627
SchemaType.INTEGER: int,
2728
SchemaType.NUMBER: float,
2829
SchemaType.BOOLEAN: forcebool,
@@ -47,7 +48,7 @@ def __init__(
4748
schema_format=None, required=None, default=None, nullable=False,
4849
enum=None, deprecated=False, all_of=None, one_of=None,
4950
additional_properties=None):
50-
self.type = schema_type and SchemaType(schema_type)
51+
self.type = SchemaType(schema_type)
5152
self.model = model
5253
self.properties = properties and dict(properties) or {}
5354
self.items = items
@@ -123,9 +124,6 @@ def cast(self, value):
123124
raise InvalidSchemaValue("Null value for non-nullable schema")
124125
return self.default
125126

126-
if self.type is None:
127-
return value
128-
129127
cast_mapping = self.get_cast_mapping()
130128

131129
if self.type is not SchemaType.STRING and value == '':

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ paths:
168168
$ref: "#/components/responses/ErrorResponse"
169169
components:
170170
schemas:
171+
Utctime:
172+
oneOf:
173+
- type: string
174+
enum: [always, now]
175+
- type: string
176+
format: date-time
171177
Address:
172178
type: object
173179
x-model: Address
@@ -202,6 +208,7 @@ components:
202208
type: integer
203209
format: int64
204210
PetCreate:
211+
type: object
205212
x-model: PetCreate
206213
allOf:
207214
- $ref: "#/components/schemas/PetCreatePartOne"
@@ -284,6 +291,8 @@ components:
284291
required:
285292
- name
286293
properties:
294+
created:
295+
$ref: "#/components/schemas/Utctime"
287296
name:
288297
type: string
289298
format: custom

tests/integration/test_petstore.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,3 +1008,75 @@ def test_post_tags_additional_properties(
10081008

10091009
assert response_result.errors == []
10101010
assert response_result.data == data_json
1011+
1012+
def test_post_tags_created_now(
1013+
self, spec, response_validator):
1014+
host_url = 'http://petstore.swagger.io/v1'
1015+
path_pattern = '/v1/tags'
1016+
pet_name = 'Dog'
1017+
data_json = {
1018+
'created': 'now',
1019+
'name': pet_name,
1020+
}
1021+
data = json.dumps(data_json)
1022+
1023+
request = MockRequest(
1024+
host_url, 'POST', '/tags',
1025+
path_pattern=path_pattern, data=data,
1026+
)
1027+
1028+
parameters = request.get_parameters(spec)
1029+
body = request.get_body(spec)
1030+
1031+
assert parameters == {}
1032+
assert body == data_json
1033+
1034+
data_json = {
1035+
'code': 400,
1036+
'message': 'Bad request',
1037+
'rootCause': 'Tag already exist',
1038+
'additionalinfo': 'Tag Dog already exist',
1039+
}
1040+
data = json.dumps(data_json)
1041+
response = MockResponse(data, status_code=404)
1042+
1043+
response_result = response_validator.validate(request, response)
1044+
1045+
assert response_result.errors == []
1046+
assert response_result.data == data_json
1047+
1048+
def test_post_tags_created_datetime(
1049+
self, spec, response_validator):
1050+
host_url = 'http://petstore.swagger.io/v1'
1051+
path_pattern = '/v1/tags'
1052+
pet_name = 'Dog'
1053+
data_json = {
1054+
'created': '2016-04-16T16:06:05Z',
1055+
'name': pet_name,
1056+
}
1057+
data = json.dumps(data_json)
1058+
1059+
request = MockRequest(
1060+
host_url, 'POST', '/tags',
1061+
path_pattern=path_pattern, data=data,
1062+
)
1063+
1064+
parameters = request.get_parameters(spec)
1065+
body = request.get_body(spec)
1066+
1067+
assert parameters == {}
1068+
assert body == data_json
1069+
1070+
data_json = {
1071+
'code': 400,
1072+
'message': 'Bad request',
1073+
'rootCause': 'Tag already exist',
1074+
'additionalinfo': 'Tag Dog already exist',
1075+
}
1076+
data = json.dumps(data_json)
1077+
response = MockResponse(data, status_code=404)
1078+
1079+
response_result = response_validator.validate(request, response)
1080+
1081+
assert response_result.errors == []
1082+
assert response_result.data == data_json

0 commit comments

Comments
 (0)