vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.34k stars 437 forks source link

[BUG] computed_field is not present in openapi spec #1292

Open POD666 opened 2 months ago

POD666 commented 2 months ago

Describe the bug

computed_field is missing in openapi spec.

I have defined a schema to be used for responses like this:

class MySchema(Schema):
    id: int
    slug: str

    @computed_field
    def name(self) -> str:
        return f"{self.id}-{self.slug}"

    def model_json_schema(self, *args, **kwargs):
        # This is a workaround for the issue with the generated schema to include computed_field
        return super().model_json_schema(*args, mode="serialization", **kwargs)

The overwritten model_json_schema is an attempt to fix the issue according to pydantic issue here.

Anyway the fix doesn't work, looks like NinjaGenerateJsonSchema doesn't handle mode="serialization".

I decided not to investigate it farther and opened the issue.

Versions (please complete the following information):

tu-pm commented 1 month ago

We'd need to override on the Schema model itself, since it is the base class of a NinjaResponseSchema class that wraps MySchema when rendering the swagger UI.