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

Choices and blank=True #135

Closed
jayvdb opened this issue Jul 24, 2020 · 5 comments
Closed

Choices and blank=True #135

jayvdb opened this issue Jul 24, 2020 · 5 comments

Comments

@jayvdb
Copy link
Contributor

jayvdb commented Jul 24, 2020

Describe the bug
When there is a charfield with choices, and blank=True, the empty string ('') is a valid value, unrelated to the field being required or not, or nullable or not.

However drf-spectacular creates an enum of the choices without an option for the empty string.

To Reproduce
django-oscar-api UserAddressSerializer, BillingAddressSerializer, ShippingAddressSerializer, all include a title that has a choices of Mr, Mrs, Dr, etc, and blank=True

Expected behavior
The enum needs to include the empty string, or some other schema structure that allows the empty string.

IMO it would be preferrable that the enum in components doesnt include the empty string, as this would mean two fields using the same choices but different blank values would require two enums. And having two enums would result in more clashes, component renames, and those enum names are now appearing in peoples ENUM_NAME_OVERRIDES .

@tfranzel
Copy link
Owner

oh thats a tricky one. you are absolutely right about the possible duplication. on the other hand it is a valid input. we have to investigate how to solve that in the least invasive way.

@tfranzel
Copy link
Owner

sry that commit is unrelated. typed the wrong number

@tfranzel
Copy link
Owner

@jayvdb not sure if you were notified of the mention in the PR. would be awesome if you could check out the solution proposal.

tfranzel added a commit that referenced this issue Oct 11, 2020
add enum postprocessing handling of blank and null #135
@gabn88
Copy link

gabn88 commented Jan 10, 2024

I'm not sure if this is related, but when using CharField choices and blank=True, the '' (which is valid to set it empty) does not come up in the Enum that is created.

@tfranzel Do you have any suggestion on how to circumvent this?

@tfranzel
Copy link
Owner

@gabn88 please re-check. this is heavily tested and should work, if 'ENUM_ADD_EXPLICIT_BLANK_NULL_CHOICE': True, (the default)

It will however not be part of the enum, as it belongs to the field and not the enum itself. You might use that enum with AND without the blank, and thus is has to be combined:

language:
nullable: true
oneOf:
- $ref: '#/components/schemas/LanguageEnum'
- $ref: '#/components/schemas/BlankEnum'
- $ref: '#/components/schemas/NullEnum'

this will be performed for model blank, serializer allow_blank, and explicit mention of '' in the choices. If you find a bug, please open a new issue. thx

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

3 participants