Skip to content
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
21 changes: 11 additions & 10 deletions openapi_core/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class Schema(object):
"""Represents an OpenAPI Schema."""

def __init__(
self, schema_type, model=None, properties=None, items=None,
self, schema_type=None, model=None, properties=None, items=None,
schema_format=None, required=None, default=None, nullable=False,
enum=None, deprecated=False, all_of=None):
self.type = SchemaType(schema_type)
self.type = schema_type and SchemaType(schema_type)
self.model = model
self.properties = properties and dict(properties) or {}
self.items = items
Expand Down Expand Up @@ -76,12 +76,12 @@ def cast(self, value):
"""Cast value to schema type"""
if value is None:
if not self.nullable:
raise InvalidValueType(
"Failed to cast value of {0} to {1}".format(
value, self.type)
)
raise InvalidValueType("Null value for non-nullable schema")
return self.default

if self.type is None:
return value

cast_mapping = self.get_cast_mapping()

if self.type in cast_mapping and value == '':
Expand Down Expand Up @@ -167,7 +167,7 @@ def __init__(self, dereferencer):
def create(self, schema_spec):
schema_deref = self.dereferencer.dereference(schema_spec)

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

return Schema(
schema_type, model=model, properties=properties, items=items,
schema_format=schema_format, required=required, default=default,
nullable=nullable, enum=enum, deprecated=deprecated, all_of=all_of,
schema_type=schema_type, model=model, properties=properties,
items=items, schema_format=schema_format, required=required,
default=default, nullable=nullable, enum=enum,
deprecated=deprecated, all_of=all_of,
)

@property
Expand Down
17 changes: 11 additions & 6 deletions tests/integration/data/v3.0/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ paths:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
$ref: "#/components/responses/ErrorResponse"
/pets/{petId}:
get:
summary: Info for a specific pet
Expand Down Expand Up @@ -192,10 +188,19 @@ components:
format: int32
message:
type: string
ExtendedError:
allOf:
- $ref: "#/components/schemas/Error"
- type: object
required:
- rootCause
properties:
rootCause:
type: string
responses:
ErrorResponse:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
$ref: "#/components/schemas/ExtendedError"
36 changes: 36 additions & 0 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,39 @@ def test_get_pet(self, spec, response_validator):

assert response_result.errors == []
assert response_result.data == data_json

def test_get_pet_not_found(self, spec, response_validator):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets/{petId}'
view_args = {
'petId': '1',
}
request = MockRequest(
host_url, 'GET', '/pets/1',
path_pattern=path_pattern, view_args=view_args,
)

parameters = request.get_parameters(spec)

assert parameters == {
'path': {
'petId': 1,
}
}

body = request.get_body(spec)

assert body is None

data_json = {
'code': 404,
'message': 'Not found',
'rootCause': 'Pet not found',
}
data = json.dumps(data_json)
response = MockResponse(data, status_code=404)

response_result = response_validator.validate(request, response)

assert response_result.errors == []
assert response_result.data == data