openapi-core
openapi-core copied to clipboard
[Bug]: Error desc not specifying what went wrong
Actual Behavior
So i use:
from openapi_core import validate_request
To validate request data versus schema:
validate_request(request_data, self.spec)
Here's the snipped for request body:
getRequest:
type: object
properties:
type_of:
type: string
name:
type: string
My request: r = requests.post(url='host/something/v012/get', data={'type_of': 'user', 'name': 'test'}) The error message raised is:
E Request body validation error
Not sure what's wrong based on the error desc Do you guys know a better way to see more exact error descriotion (what's wrong with the request data vs yaml?)
Openapi_core version 0.18.1
Expected Behavior
Exected to isolate or relate what was not ok from request data vs yaml schema
Steps to Reproduce
Not sure what is wrong with my request yet
OpenAPI Core Version
0.18.1
OpenAPI Core Integration
requests
Affected Area(s)
No response
References
No response
Anything else we need to know?
No response
Would you like to implement a fix?
None
So i pinpointed the problem, my request uses basic auth, i was encoding user/pass with base64 which caused the above problem, still the error is really not as expected
Hi @itsMGA
thanks for the report. Please provide working example with full traceback.
I found you have to dig a little deeper into the exceptions to get the exact problem. OpenApiError has a __cause__
attribute that points to the InvalidSchemaValue
that's raised by the validator. InvalidSchemaValue has a schema_errors
attribute, which is a list of jsonschema ValidationError
s. They contain the exact error message via ValidationError.message
and if it's a nested field, the .path
to the bad field. Looks like path parameter names can be retrieved from OpenApiError.name
instead.
Hope that helps someone.
I was able to get some more information about a failed validation for my cases by calling __cause__
like @grinspins suggested.
I didn't have in the path errors though, so my results were a bit different.
In one case I got a RequestBodyValidationError which returns a "Request body validation error" string.
Getting the __cause__
on the error object returned:
MediaTypeNotFound(mimetype='application/vnd.2e.api.v2+json', availableMimetypes=['application/vnd.2e.shp.web.v1+json'])
and inspecting it showed an AttributeError, as "'dict' object has no attribute 'exc.__cause__
'.
In another case I got a CastError, and __cause__
returned:
Failed to cast value to object type: b'{...}'
(with entire json body)
so a nice error message could be retrieved with __cause__.value
.
Anyway, these maneuvers just don't feel right. Validation result should be something more readable and easily accessible. If there is a better way, but I haven't found it yet.