-
-
Notifications
You must be signed in to change notification settings - Fork 135
Validation pass when object, list or integer returned instead of string for object string property (must fail) #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Initial report is here: ergoplatform/ergo#555 |
I have recently started experimenting with openapi_core and also came across this issue. I think the problem is that when unmarshalling a string and no format is given I have worked around this issue for now by monkey patching so that I can continue experimenting with this really cool library (thanks for this by the way @p1c2u !) @contextlib.contextmanager
def strict_str():
"""
openapi_core unmarshals and validates strings by converting whatever
is in the response to a str, and then validating that what we get is
a str. This is of course means that lot's of things convert
fine to a string. This means that we cannot validate string
properties.
To workaround this issue this function provides a means to patch
openapi_core to use our own custom formatting for strings which
strictly checks that a value is a string.
For example...
>>> with strict_str():
... validator = ResponseValidator(...)
"""
def strict_to_str(x):
if not isinstance(x, str):
raise OpenAPISchemaError(f"Expected str but got {type(x)} -- {x}")
return x
original = Schema.STRING_FORMAT_CALLABLE_GETTER
patched = dict(original)
patched[SchemaFormat.NONE] = Format(strict_to_str, lambda x: isinstance(x, str))
target = "openapi_core.schema.schemas.models.Schema.STRING_FORMAT_CALLABLE_GETTER"
with patch(target, patched):
yield @p1c2u what do you think about making the unmarshalling of strings more strict so that this patching is not needed? I would be happy to make a PR if you agree with the approach (basically when no format is given we use strict type checking to ensure we actually have a string) |
Ah actually I see there is already a PR! |
Oh thank you! I'll check this within 2-3 weeks. |
Hi!
We faced with following bug. Some object's property declared as string, If this object's property returned by API is object itself, or integer, or list, no errors generated.
If API returns wrong property type for example int property, InvalidMediaTypeValue thrown (as expected).
Tested on version 0.5.0, 0.7.1, master - same results.
Expected behavior
InvalidMediaType should be thrown
Steps to reproduce
Install openapi-core:
2.. Create and save following fatless specification as
test.yml
:Create and save following script as
test.py
:Execute script to validate spec:
Try to comment out test payloads to see actual results.
The text was updated successfully, but these errors were encountered: