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

Pydantic computed fields are not appearing in the schema #1354

Closed
lubiah opened this issue Dec 14, 2024 · 2 comments
Closed

Pydantic computed fields are not appearing in the schema #1354

lubiah opened this issue Dec 14, 2024 · 2 comments

Comments

@lubiah
Copy link

lubiah commented Dec 14, 2024

Describe the bug
If your create a pydantic basemodel which has computed fields as fields, those fields don't appear when you pass it to drf-spectacular as a serializer.

To Reproduce
Create any Pydantic model with computer fields and pass it as a serializer to the extend_schema decorator or get_serializer method.

Expected behavior
The computed fields should apppear.

I did find that, in the pydantic extension. Setting the model_dump_schema(mode='serialization') fixes the issue

@tfranzel
Copy link
Owner

Hey, I just added a computed property to the tests and it works as expected. Are you sure you are using spectacular 0.28.0?

Also we generate the schema slightly differently with

model_json_schema(self.target, ref_template="#/components/schemas/{model}", mode="serialization")

but we also set to mode to serialization beginning with our latest release.

Please see the PR, which is passing. Don't mind the broken build, it is due to another lib changing currently.

@lubiah
Copy link
Author

lubiah commented Dec 18, 2024

@tfranzel . I'm using 0.27.2. I think I need to upgrade. I'll close the issue now.
With the current version I'm using schema was generated with schema = model_json_schema(self.target, ref_template="#/components/schemas/{model}"). and manually adding the serialization mode fixed the issue.
Since the new version includes it, then I think my issue is resolved.

@lubiah lubiah closed this as completed Dec 18, 2024
tfranzel added a commit that referenced this issue Dec 19, 2024
add pydantic computed field to tests #1354
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