openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

[Bug]: `forcebool` inconsistency with `BooleanCaster`

Open indigane opened this issue 1 year ago • 0 comments

Actual Behavior

I have API tests similar to the one below that test 'True', 'true' and '1' for a boolean query parameter. https://github.com/python-openapi/openapi-core/blob/b8f0644bde3afb4e0273c4cfa8f2ac3eee9f76b3/tests/unit/test_util.py#L6-L22

The last one, '1', no longer passes validation.

Expected Behavior

This behavior has changed at some point after 0.16.x where the test passed. This came up attempting to update past 0.16.x where the test started failing due to CastingError.

As for expected behavior, it may be that the new behavior is more correct but it is also somewhat common to allow '1' in APIs.

The older versions used forcebool as the caster/validator but the current version first calls BooleanCaster.validate before calling forcebool.

Steps to Reproduce

Pass for example '1' to BooleanCaster.__call__.

OpenAPI Core Version

0.19.2

OpenAPI Core Integration

django

Affected Area(s)

casting, validation

References

Old code:

https://github.com/python-openapi/openapi-core/blob/efaa5aca494341837470552e905ca6f35f1fe88e/openapi_core/casting/schemas/factories.py#L19-L23

https://github.com/python-openapi/openapi-core/blob/efaa5aca494341837470552e905ca6f35f1fe88e/openapi_core/util.py#L7-L17

Current code:

https://github.com/python-openapi/openapi-core/blob/b8f0644bde3afb4e0273c4cfa8f2ac3eee9f76b3/openapi_core/casting/schemas/init.py#L25-L34

https://github.com/python-openapi/openapi-core/blob/8e2c5a8346ea9cd88f84308515c992a170429b91/openapi_core/casting/schemas/casters.py#L61-L78

Anything else we need to know?

The main thing I want to point out is that forcebool is only ever called after validate which means that values other than 'true' and 'false' can never get to forcebool.

If this is on purpose, then I assume the way around this is to provide a custom BooleanCaster?

Or are there other suggested solutions if I do want to allow '1' in the API?

Would you like to implement a fix?

None

indigane avatar Nov 26 '24 10:11 indigane