vitalik / django-ninja

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

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

Open POD666 opened 3 weeks ago

POD666 commented 3 weeks 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):