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

[Bug]: `DjangoOpenAPIResponse` no longer type compatible with `unmarshal_response`

Open jceipek opened this issue 1 year ago • 2 comments

Actual Behavior

There is a type error here when using werkzeug Version 3.1.2 or newer:

from django.conf import settings
from openapi_core import unmarshal_response
from openapi_core.contrib.django import DjangoOpenAPIRequest, DjangoOpenAPIResponse

def test(openapi_request: DjangoOpenAPIRequest, openapi_response: DjangoOpenAPIResponse):
        return unmarshal_response(
            request=openapi_request,
            response=openapi_response, # type error on this line
            spec=settings.OPENAPI_SPEC,
        )

Expected Behavior

No type errors in the provided sample code.

Steps to Reproduce

Typecheck the provided sample code using mypy.

OpenAPI Core Version

3.0.0

OpenAPI Core Integration

django

Affected Area(s)

casting, dependencies, unmarshalling

References

  • https://github.com/pallets/werkzeug/blob/main/CHANGES.rst#version-312
  • https://github.com/pallets/werkzeug/issues/2989

Anything else we need to know?

werkzeug Version 3.1.2 changed the definition of Headers because it isn't technically a MutableMapping.

The Response protocol in openapi_core here has Mapping[str, Any]: https://github.com/python-openapi/openapi-core/blob/b8f0644bde3afb4e0273c4cfa8f2ac3eee9f76b3/openapi_core/protocols.py#L122-L123

DjangoOpenAPIResponse has headers typed as Headers from werkzeug: https://github.com/python-openapi/openapi-core/blob/b8f0644bde3afb4e0273c4cfa8f2ac3eee9f76b3/openapi_core/contrib/django/responses.py#L33-L35

Since these are no longer compatible, unmarshal_response can no longer accept DjangoOpenAPIResponse as a response parameter without failing typechecking.

Would you like to implement a fix?

None

jceipek avatar Nov 12 '24 18:11 jceipek

MockResponse is also incompatible with Response for the same reason.

https://github.com/python-openapi/openapi-core/blob/b8f0644bde3afb4e0273c4cfa8f2ac3eee9f76b3/openapi_core/testing/responses.py#L20

andersk avatar Nov 19 '24 03:11 andersk

What if I were to use a protocol with a generic? Could we have that just wrap the werkzeug nonsense?

LewisCowlesMotive avatar Apr 25 '25 13:04 LewisCowlesMotive

Any progress with this? I'm having issues with azure-functions >=1.23.0

poetry show werkzeug name: werkzeug version: 3.1.3 description: The comprehensive WSGI web application library.

dependencies

  • MarkupSafe >=2.1.1

required by

  • azure-functions >=3.1.3,<3.2.0
  • moto >=0.5,<2.2.0 || >2.2.0,<2.2.1 || >2.2.1

sergioave avatar Oct 27 '25 16:10 sergioave

I Cannot even remember why I was looking at this 😄

LewisCowlesMotive avatar Oct 28 '25 12:10 LewisCowlesMotive

I think the title of this is wrong, it should be

[Bug]: Protocols.headers => Mutable is no longer compatible with werkzeug.Headers

Rationale: DjangoOpenAPIResponse.headers => Headers is compatible with the definition in werkzeug.

miraculixx avatar Nov 28 '25 01:11 miraculixx