Skip to content

Corrected OpenAPI schema type for DecimalField #7254

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

Merged
merged 4 commits into from
Apr 9, 2020
Merged

Corrected OpenAPI schema type for DecimalField #7254

merged 4 commits into from
Apr 9, 2020

Conversation

clintonb
Copy link
Contributor

@clintonb clintonb commented Apr 4, 2020

Description

The API renders DecimalFields as strings. The generated schema now renders decimal fields correctly as strings.

Fixes #7253

The API renders DecimalFields as strings. The generated schema now renders decimal fields correctly as strings.

Fixes #7253
'type': 'number'
return {
'type': 'string',
'format': 'decimal'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OpenAPI spec allows for specifying a format despite it not being "official". See OAI/OpenAPI-Specification#845 (comment) for additional discussion.

assert properties['decimal1']['multipleOf'] == .01
assert properties['decimal1']['maximum'] == 10000
assert properties['decimal1']['minimum'] == -10000
assert properties['decimal1'] == {'type': 'string', 'format': 'decimal'}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for this style of validation to ensure there are no other properties being erroneously added to the spec, and breaking validation.

@dhaval-mehta
Copy link
Contributor

dhaval-mehta commented Apr 5, 2020

Hi @clintonb,

coerce_decimal_to_string setting determines whether field will be serialized as number or string.

My suggestion: Do not change to string for all cases. Based on coerce_decimal_to_string value you should set datatype.

@clintonb
Copy link
Contributor Author

clintonb commented Apr 5, 2020

Good catch @dhaval-mehta. Code updated.

@clintonb
Copy link
Contributor Author

clintonb commented Apr 5, 2020

It's worth noting that I am reasonably certain the rendering of ChoiceField is incorrect. For example, when passed Decimal choices, the enums array is rendered with the word Decimal surrounding the values. I don't have the time to fix this, but I imagine someone will/has encountered this error.

@clintonb
Copy link
Contributor Author

clintonb commented Apr 5, 2020

Please review, @carltongibson.

@carltongibson carltongibson self-requested a review April 5, 2020 18:39
@carltongibson carltongibson added this to the 3.12 Release milestone Apr 5, 2020
@dhaval-mehta
Copy link
Contributor

dhaval-mehta commented Apr 5, 2020

It's worth noting that I am reasonably certain the rendering of ChoiceField is incorrect. For example, when passed Decimal choices, the enums array is rendered with the word Decimal surrounding the values. I don't have the time to fix this, but I imagine someone will/has encountered this error.

@clintonb In #7161, I added code for type generation for enum. I will work on this.

My question is:
@carltongibson, It is still unofficial. Should we include this?

@carltongibson
Copy link
Collaborator

Hi @dhaval-mehta: If you have time to prepare a PR for that, super! 🥇

@dhaval-mehta
Copy link
Contributor

Hi @dhaval-mehta: If you have time to prepare a PR for that, super! 🥇

Working on it :)

@clintonb
Copy link
Contributor Author

clintonb commented Apr 8, 2020

@dhaval-mehta @carltongibson please review

if getattr(field, 'coerce_to_string', api_settings.COERCE_DECIMAL_TO_STRING):
content = {
'type': 'string',
'format': 'decimal',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through OAI/OpenAPI-Specification#845 (comment).

I found that the decimal format is supported for number datatype as well.

The rest of the code looks good to me!

Copy link
Collaborator

@carltongibson carltongibson Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the decimal format is supported for number datatype as well.

I constantly struggle to find answers spelled out clearly and canonically. Do we want to add this?
OAI/OpenAPI-Specification#845 is still WIP, is it actually part of the spec yet?

Inclined to take this PR as it is now, and add more later... Opinions?

Copy link
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one comment.

if getattr(field, 'coerce_to_string', api_settings.COERCE_DECIMAL_TO_STRING):
content = {
'type': 'string',
'format': 'decimal',
Copy link
Collaborator

@carltongibson carltongibson Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the decimal format is supported for number datatype as well.

I constantly struggle to find answers spelled out clearly and canonically. Do we want to add this?
OAI/OpenAPI-Specification#845 is still WIP, is it actually part of the spec yet?

Inclined to take this PR as it is now, and add more later... Opinions?

Copy link
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's have it. We can take further tweaks to decimal handling later if there's call for it. Thanks @clintonb. 🥇

@carltongibson carltongibson merged commit 603aac7 into encode:master Apr 9, 2020
@clintonb clintonb deleted the fix-openapi-decimal branch April 23, 2022 23:32
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Decimal fields rendered as number instead of string in OpenAPI schema
4 participants