Merge pull request #16 from p1c2u/fix/boolean-value-cast-fix

Boolean value cast fix
This commit is contained in:
A 2018-02-28 13:47:43 +00:00 committed by GitHub
commit c30bef179d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 2 deletions

View file

@ -17,10 +17,18 @@ from openapi_core.models import ModelFactory
log = logging.getLogger(__name__)
def forcebool(val):
if isinstance(val, str):
val = strtobool(val)
return bool(val)
DEFAULT_CAST_CALLABLE_GETTER = {
SchemaType.INTEGER: int,
SchemaType.NUMBER: float,
SchemaType.BOOLEAN: lambda x: bool(strtobool(x)),
SchemaType.BOOLEAN: forcebool,
}

View file

@ -161,6 +161,8 @@ components:
$ref: "#/components/schemas/Address"
position:
$ref: "#/components/schemas/Position"
healthy:
type: boolean
Pets:
type: array
items:

View file

@ -370,6 +370,7 @@ class TestPetstore(object):
pet_tag = 'cats'
pet_street = 'Piekna'
pet_city = 'Warsaw'
pet_healthy = False
data_json = {
'name': pet_name,
'tag': pet_tag,
@ -377,7 +378,8 @@ class TestPetstore(object):
'address': {
'street': pet_street,
'city': pet_city,
}
},
'healthy': pet_healthy,
}
data = json.dumps(data_json)
@ -402,6 +404,50 @@ class TestPetstore(object):
assert body.address.__class__.__name__ == address_model
assert body.address.street == pet_street
assert body.address.city == pet_city
assert body.healthy == pet_healthy
def test_post_pets_boolean_string(self, spec, spec_dict):
host_url = 'http://petstore.swagger.io/v1'
path_pattern = '/v1/pets'
pet_name = 'Cat'
pet_tag = 'cats'
pet_street = 'Piekna'
pet_city = 'Warsaw'
pet_healthy = 'false'
data_json = {
'name': pet_name,
'tag': pet_tag,
'position': '2',
'address': {
'street': pet_street,
'city': pet_city,
},
'healthy': pet_healthy,
}
data = json.dumps(data_json)
request = MockRequest(
host_url, 'POST', '/pets',
path_pattern=path_pattern, data=data,
)
parameters = request.get_parameters(spec)
assert parameters == {}
body = request.get_body(spec)
schemas = spec_dict['components']['schemas']
pet_model = schemas['PetCreate']['x-model']
address_model = schemas['Address']['x-model']
assert body.__class__.__name__ == pet_model
assert body.name == pet_name
assert body.tag == pet_tag
assert body.position == 2
assert body.address.__class__.__name__ == address_model
assert body.address.street == pet_street
assert body.address.city == pet_city
assert body.healthy is False
def test_post_pets_empty_body(self, spec, spec_dict):
host_url = 'http://petstore.swagger.io/v1'