Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
POD666 opened this issue Sep 5, 2024 · 1 comment
Open

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

POD666 opened this issue Sep 5, 2024 · 1 comment

Comments

@POD666
Copy link

POD666 commented Sep 5, 2024

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):

  • Python version: Python 3.10.3
  • Django version: 3.2.13
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.7.4
@tu-pm
Copy link

tu-pm commented Oct 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants