Skip to content
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

[Bug]: Error desc not specifying what went wrong #711

Open
itsMGA opened this issue Nov 1, 2023 · 4 comments
Open

[Bug]: Error desc not specifying what went wrong #711

itsMGA opened this issue Nov 1, 2023 · 4 comments
Labels
kind/bug Indicates an issue

Comments

@itsMGA
Copy link

itsMGA commented Nov 1, 2023

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

@itsMGA itsMGA added the kind/bug Indicates an issue label Nov 1, 2023
@itsMGA
Copy link
Author

itsMGA commented Nov 1, 2023

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

@p1c2u
Copy link
Collaborator

p1c2u commented Nov 3, 2023

Hi @itsMGA

thanks for the report. Please provide working example with full traceback.

@grinspins
Copy link

grinspins commented Nov 21, 2023

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 ValidationErrors. 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.

@ladaflac
Copy link

ladaflac commented Feb 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Indicates an issue
Projects
None yet
Development

No branches or pull requests

4 participants