python-openapi / openapi-core

Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI v3.0 and OpenAPI v3.1 specification.
BSD 3-Clause "New" or "Revised" License
287 stars 131 forks source link

Werkzeug validate_request: 'request' argument is not type of Request #806

Open htmhmeer opened 4 months ago

htmhmeer commented 4 months ago

Actual Behavior

openapi_request = WerkzeugOpenAPIRequest(request) type of openapi_request: openapi_core.contrib.werkzeug.requests.WerkzeugOpenAPIRequest

openapi.validate_request(openapi_request)

│ File "/home/odoo/.local/lib/python3.8/site-packages/openapi_core/app.py", line 347, in validate_request
│ self.validate_apicall_request(request)
│ File "/home/odoo/.local/lib/python3.8/site-packages/openapi_core/app.py", line 359, in validate_apicall_request
│ raise TypeError("'request' argument is not type of Request")

Expected Behavior

that it works like documentated here: https://openapi-core.readthedocs.io/en/latest/integrations/werkzeug.html

Steps to Reproduce

see above

OpenAPI Core Version

Version: 0.19.0

OpenAPI Core Integration

Werkzeug

Affected Area(s)

No response

References

No response

Anything else we need to know?

am I missing something?

Would you like to implement a fix?

None

p1c2u commented 4 months ago

Hi @htmhmeer

can you provide working example. Your code doesn't show what type of request object did you use.

htmhmeer commented 4 months ago
from werkzeug.wrappers import Request

environ = request.httprequest._HTTPRequest__wrapped.environ
openapi_request = WerkzeugOpenAPIRequest(Request(environ))

openapi_request = WerkzeugOpenAPIRequest(request.httprequest._HTTPRequest__wrapped)
result = openapi.unmarshal_request(openapi_request)

The problem is that the system (Odoo) has a wrapper around the original werkzeug request so I tried making a new one with the original environ variable or using the original wrapped one, both methods give the same error when validating or unamrshalling, the WerkzeugOpenAPIRequest does work fine though.

The thing is though, I think unmarshal would work perfectly fine if that openapi-core check wouldn't be there.

htmhmeer commented 3 months ago

If I do isinstance(Request(environ), Request) it returns True, however, it still gives me the same error: TypeError: 'request' argument is not type of Request Tried: result = openapi.unmarshal_request(Request(environ)) Same error: I'm literally putting in the exact same Type and it just doesn't work

p1c2u commented 3 months ago

The problem is that the system (Odoo) has a wrapper around the original werkzeug request

Because it's made for werkzeug request You can try to extract original request from it.

Alternatively Nnthing stopping you from creating your own class for the wrapper based on WerkzeugOpenAPIRequest just override __init__ method.