yuval9313 / FastApi-RESTful

Quicker way to develop FastApi
MIT License
180 stars 25 forks source link

[BUG] default_response_class parameter is ignored #277

Closed caffeinatedgaze closed 7 months ago

caffeinatedgaze commented 7 months ago

Describe the bug FastAPI's default_response_class parameter is ignored by class-based views. This is important when, for example, trying to override the default JSON encoder.

To Reproduce Steps to reproduce the behavior:

  1. Create a class-based view
  2. Add a custom encoder, see the example below
  3. Trigger the view
  4. Observe that the log is not printed

If you repeat these steps for an normal function-based view, the log will be printed.

Expected behavior The custom encoder is ignored in class-based views.

Environment:

- fastapi==0.109.0
- fastapi-restful==0.5.0
- Python==3.11.7
class CustomJSONEncoder(JSONEncoder):

    def default(self, o: Any) -> Any:
        print("I was here.")
        return super().default(o)

class CustomJSONResponse(JSONResponse):

    def __init__(self, *args, **kwargs):
        print("I was here.")
        super().__init__(*args, **kwargs)

    def render(self, content: Any) -> bytes:
        print("I was here.")
        super().render(content=content)

app = FastAPI(default_response_class=CustomJSONResponse)
yuval9313 commented 7 months ago

Hey, can you add the CBV used? I will copy it directly to the unit tests and fix it asap

caffeinatedgaze commented 7 months ago

Sure. I will provide a minimal reproducible example asap. :racehorse:

caffeinatedgaze commented 7 months ago

I tried to reproduce it in a clean environment, and it worked fine. I guess my issues were related to inaccurate usage of Pydantic models and lazy fields. I will re-open if found out anything new.