Skip to content

Commit b1f58d7

Browse files
committed
Boolean value cast fix
1 parent e451c9f commit b1f58d7

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

openapi_core/schemas.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@
1717

1818
log = logging.getLogger(__name__)
1919

20+
21+
def forcebool(val):
22+
if isinstance(val, str):
23+
val = strtobool(val)
24+
25+
return bool(val)
26+
27+
2028
DEFAULT_CAST_CALLABLE_GETTER = {
2129
SchemaType.INTEGER: int,
2230
SchemaType.NUMBER: float,
23-
SchemaType.BOOLEAN: lambda x: bool(strtobool(x)),
31+
SchemaType.BOOLEAN: forcebool,
2432
}
2533

2634

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ components:
161161
$ref: "#/components/schemas/Address"
162162
position:
163163
$ref: "#/components/schemas/Position"
164+
healthy:
165+
type: boolean
164166
Pets:
165167
type: array
166168
items:

tests/integration/test_petstore.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,59 @@ def test_post_pets(self, spec, spec_dict):
370370
pet_tag = 'cats'
371371
pet_street = 'Piekna'
372372
pet_city = 'Warsaw'
373+
pet_healthy = False
373374
data_json = {
374375
'name': pet_name,
375376
'tag': pet_tag,
376377
'position': '2',
377378
'address': {
378379
'street': pet_street,
379380
'city': pet_city,
380-
}
381+
},
382+
'healthy': pet_healthy,
383+
}
384+
data = json.dumps(data_json)
385+
386+
request = MockRequest(
387+
host_url, 'POST', '/pets',
388+
path_pattern=path_pattern, data=data,
389+
)
390+
391+
parameters = request.get_parameters(spec)
392+
393+
assert parameters == {}
394+
395+
body = request.get_body(spec)
396+
397+
schemas = spec_dict['components']['schemas']
398+
pet_model = schemas['PetCreate']['x-model']
399+
address_model = schemas['Address']['x-model']
400+
assert body.__class__.__name__ == pet_model
401+
assert body.name == pet_name
402+
assert body.tag == pet_tag
403+
assert body.position == 2
404+
assert body.address.__class__.__name__ == address_model
405+
assert body.address.street == pet_street
406+
assert body.address.city == pet_city
407+
assert body.healthy == pet_healthy
408+
409+
def test_post_pets_boolean_string(self, spec, spec_dict):
410+
host_url = 'http://petstore.swagger.io/v1'
411+
path_pattern = '/v1/pets'
412+
pet_name = 'Cat'
413+
pet_tag = 'cats'
414+
pet_street = 'Piekna'
415+
pet_city = 'Warsaw'
416+
pet_healthy = 'false'
417+
data_json = {
418+
'name': pet_name,
419+
'tag': pet_tag,
420+
'position': '2',
421+
'address': {
422+
'street': pet_street,
423+
'city': pet_city,
424+
},
425+
'healthy': pet_healthy,
381426
}
382427
data = json.dumps(data_json)
383428

@@ -402,6 +447,7 @@ def test_post_pets(self, spec, spec_dict):
402447
assert body.address.__class__.__name__ == address_model
403448
assert body.address.street == pet_street
404449
assert body.address.city == pet_city
450+
assert body.healthy is False
405451

406452
def test_post_pets_empty_body(self, spec, spec_dict):
407453
host_url = 'http://petstore.swagger.io/v1'

0 commit comments

Comments
 (0)