-
Notifications
You must be signed in to change notification settings - Fork 264
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
fix: set pydantic json mode to serialization #1156
Conversation
Hi! So I skimmed over the other issue and I'm not sure this is an improvement here. What exactly do we fix here and what do we gain from this narrowing? |
The The schemas generated by drf-spectacular describe language-agnostic serialization (rather than python-specific validation) and should require decimals be encoded as strings. |
I get your point now, but I'm not entirely convinced that everybody wants to have it that way. Actually DRF has a dedicated feature flag for this question: https://www.django-rest-framework.org/api-guide/settings/#coerce_decimal_to_string Maybe we should use it for this also? We build the |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1156 +/- ##
=======================================
Coverage 98.58% 98.58%
=======================================
Files 72 72
Lines 8766 8768 +2
=======================================
+ Hits 8642 8644 +2
Misses 124 124 ☔ View full report in Codecov by Sentry. |
Related to this: having some kind of option for passing a class MyModel(BaseModel):
model_config = ConfigDict(alias_generator=AliasGenerator(serialization_alias=to_camel))
my_property: str In current mode the drf-spectacular will always generate a schema with I can open a PR if you agree this would be helpful. If you have some tips how to best implement this, lmk! |
Thanks for chiming in @microHoffman. That is another good point for this. Another point was also just raised in #1258. I think now with those 3 points, the benefits outweigh the potential issues this may bring. In general it seems that Let's merge it and see what happens 😄 |
Hello @tfranzel , just a note that this is possibly unexpected schema breaking change for some users - just because someone has a Second case is e.g. if you use In my opinion ideal would be to be able to somehow explicitly pass to the drf-spectacular what mode of serialization you use for each endpoint (and ideally for request and response separately), e.g. via What do you think? |
@microHoffman thank you for your thorough analysis. As we already noted, this should be the better solution and not necessarily the best one. I still think it is an improvement.
Do you mean that is impossible currently to generate a request/response schema currently? Maybe injecting a custom generator would be an idea for this?
Can't really apply this here and changing the decorator would be too big of an impact. Apart from that you would want to patch the fields and that would be If anything I think this is an improvement over before and pydantic support is by design never going to be perfect as it is simply a convenience without any conventions (wrt. DRF). |
See: pydantic/pydantic#7457 (comment)
This fixes the schema for Decimal types.
Before:
After: