Skip to content

Commit c733f94

Browse files
committed
Allow undefined schema type
1 parent c954275 commit c733f94

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

openapi_core/schemas.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class Schema(object):
3636
"""Represents an OpenAPI Schema."""
3737

3838
def __init__(
39-
self, schema_type, model=None, properties=None, items=None,
39+
self, schema_type=None, model=None, properties=None, items=None,
4040
schema_format=None, required=None, default=None, nullable=False,
4141
enum=None, deprecated=False, all_of=None):
42-
self.type = SchemaType(schema_type)
42+
self.type = schema_type and SchemaType(schema_type)
4343
self.model = model
4444
self.properties = properties and dict(properties) or {}
4545
self.items = items
@@ -76,12 +76,12 @@ def cast(self, value):
7676
"""Cast value to schema type"""
7777
if value is None:
7878
if not self.nullable:
79-
raise InvalidValueType(
80-
"Failed to cast value of {0} to {1}".format(
81-
value, self.type)
82-
)
79+
raise InvalidValueType("Null value for non-nullable schema")
8380
return self.default
8481

82+
if self.type is None:
83+
return value
84+
8585
cast_mapping = self.get_cast_mapping()
8686

8787
if self.type in cast_mapping and value == '':
@@ -167,7 +167,7 @@ def __init__(self, dereferencer):
167167
def create(self, schema_spec):
168168
schema_deref = self.dereferencer.dereference(schema_spec)
169169

170-
schema_type = schema_deref['type']
170+
schema_type = schema_deref.get('type')
171171
schema_format = schema_deref.get('format')
172172
model = schema_deref.get('x-model', None)
173173
required = schema_deref.get('required', False)
@@ -192,9 +192,10 @@ def create(self, schema_spec):
192192
items = self._create_items(items_spec)
193193

194194
return Schema(
195-
schema_type, model=model, properties=properties, items=items,
196-
schema_format=schema_format, required=required, default=default,
197-
nullable=nullable, enum=enum, deprecated=deprecated, all_of=all_of,
195+
schema_type=schema_type, model=model, properties=properties,
196+
items=items, schema_format=schema_format, required=required,
197+
default=default, nullable=nullable, enum=enum,
198+
deprecated=deprecated, all_of=all_of,
198199
)
199200

200201
@property

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ paths:
8484
'201':
8585
description: Null response
8686
default:
87-
description: unexpected error
88-
content:
89-
application/json:
90-
schema:
91-
$ref: "#/components/schemas/Error"
87+
$ref: "#/components/responses/ErrorResponse"
9288
/pets/{petId}:
9389
get:
9490
summary: Info for a specific pet
@@ -192,10 +188,19 @@ components:
192188
format: int32
193189
message:
194190
type: string
191+
ExtendedError:
192+
allOf:
193+
- $ref: "#/components/schemas/Error"
194+
- type: object
195+
required:
196+
- rootCause
197+
properties:
198+
rootCause:
199+
type: string
195200
responses:
196201
ErrorResponse:
197202
description: unexpected error
198203
content:
199204
application/json:
200205
schema:
201-
$ref: "#/components/schemas/Error"
206+
$ref: "#/components/schemas/ExtendedError"

tests/integration/test_petstore.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,39 @@ def test_get_pet(self, spec, response_validator):
619619

620620
assert response_result.errors == []
621621
assert response_result.data == data_json
622+
623+
def test_get_pet_not_found(self, spec, response_validator):
624+
host_url = 'http://petstore.swagger.io/v1'
625+
path_pattern = '/v1/pets/{petId}'
626+
view_args = {
627+
'petId': '1',
628+
}
629+
request = MockRequest(
630+
host_url, 'GET', '/pets/1',
631+
path_pattern=path_pattern, view_args=view_args,
632+
)
633+
634+
parameters = request.get_parameters(spec)
635+
636+
assert parameters == {
637+
'path': {
638+
'petId': 1,
639+
}
640+
}
641+
642+
body = request.get_body(spec)
643+
644+
assert body is None
645+
646+
data_json = {
647+
'code': 404,
648+
'message': 'Not found',
649+
'rootCause': 'Pet not found',
650+
}
651+
data = json.dumps(data_json)
652+
response = MockResponse(data, status_code=404)
653+
654+
response_result = response_validator.validate(request, response)
655+
656+
assert response_result.errors == []
657+
assert response_result.data == data

0 commit comments

Comments
 (0)