-
Notifications
You must be signed in to change notification settings - Fork 265
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
improve decimal handling #24
Comments
Hi, When using SerializerMethodField with TypeHiting, it generates Decimal fields as number, even with COERCE_DECIMAL_TO_STRING=True
And the generated schema:
This is also happening with TypedDicts as the Type Hinting for the SerializerMethodField function. Shouldn't it be handled the same way DecimalSerializer is handled, and the generated schema should be String? Thanks |
Hi, I think this is the correct behavior. I'm pretty certain your API will in fact return a JSON |
Actually I have overridden drf Json encoder to coerce it to string and not
float, so every decimal in Jason fields of my api are coerced to string,
and I don't have to do it manuallyi ft is is the correct approach, I'll use
str as type hint, but this way I don't have the regex for numbers on it.
Thanks anyway
…On Sat, Sep 24, 2022 at 6:26 PM T. Franzel ***@***.***> wrote:
Hi,
I think this is the correct behavior. COERCE_DECIMAL_TO_STRING is a DRF
setting and only applies to instances of DecimalField. The
SerializerMethodField is unaffected by this.
I'm pretty certain your API will in fact return a JSON number and not a
string if used like that. DRF's JSONEncoder will make it a float. You
would need to do the string conversion yourself here (and use -> List[str]
).
—
Reply to this email directly, view it on GitHub
<#24 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADDH5L3UED2AG7CGBT7PGMDV75WYXANCNFSM4MHWRRSQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I see, but your change does not reflect the default behavior of DRF and so I suppose there is nothing to fix. spectacular cannot know what you change inside your
yeah that is a tradeoff. If you are hard set on that detail your would need to subclass |
I'll have a look into this.
Thanks for the prompt replies.
And congratulations for the great job you've been doing!!
…On Sun, Sep 25, 2022 at 7:22 AM T. Franzel ***@***.***> wrote:
I see, but your change does not reflect the default behavior of DRF and so
I suppose there is nothing to fix. spectacular cannot know what you change
inside your JSONEncoder.
but this way I don't have the regex for numbers on it.
yeah that is a tradeoff. If you are hard set on that detail your would
need to subclass AutoSchema and override _map_response_type_hint.
—
Reply to this email directly, view it on GitHub
<#24 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADDH5L3YVC4DZ4UVSS5PTRLWAARXTANCNFSM4MHWRRSQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Hi,
Sorry to bother you again with this, but while looking into override
_map_response_type_hint I bumped on another method that should be affected
by this _resolve_typeddict.
Digging deeper, and as i have a custom JSONEncoder and all my Decimals are
being rendered as strings, if there was a way for me to override
OPENAPI_TYPE_MAPPING
or get_openapi_type_mapping it would do the trick, but I could not find a
way to do this. Do you think it is possible?
Thanks again!!
On Sun, Sep 25, 2022 at 7:38 AM Gustavo Jacob ***@***.***>
wrote:
… I'll have a look into this.
Thanks for the prompt replies.
And congratulations for the great job you've been doing!!
On Sun, Sep 25, 2022 at 7:22 AM T. Franzel ***@***.***>
wrote:
> I see, but your change does not reflect the default behavior of DRF and
> so I suppose there is nothing to fix. spectacular cannot know what you
> change inside your JSONEncoder.
>
> but this way I don't have the regex for numbers on it.
>
> yeah that is a tradeoff. If you are hard set on that detail your would
> need to subclass AutoSchema and override _map_response_type_hint.
>
> —
> Reply to this email directly, view it on GitHub
> <#24 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ADDH5L3YVC4DZ4UVSS5PTRLWAARXTANCNFSM4MHWRRSQ>
> .
> You are receiving this because you commented.Message ID:
> ***@***.***>
>
|
Hey @gujacob . I found this thread while googling for a solution for a similar problem. Since I didn't find anything I sketched this field class, which I think has all the correct behaviors, including the correct generated schema:
I've tried to fix the Any updates / fixes to this snippet are appreciated, although it's working fine for my use-case. Let me just finish, by apologizing to @tfranzel for putting this here, when this is not directly related to |
decimals can be both string and number. utilize
COERCE_DECIMAL_TO_STRING
to detect the caseimplement funtionality comparable to encode/django-rest-framework@603aac7
The text was updated successfully, but these errors were encountered: