Skip to content

Avoid ChoiceField duplicate enum values for allow_null, allow_blank#1085

Merged
tfranzel merged 4 commits intotfranzel:masterfrom
intgr:avoid-choicefield-duplicate-values
Oct 22, 2023
Merged

Avoid ChoiceField duplicate enum values for allow_null, allow_blank#1085
tfranzel merged 4 commits intotfranzel:masterfrom
intgr:avoid-choicefield-duplicate-values

Conversation

@intgr
Copy link
Copy Markdown
Contributor

@intgr intgr commented Oct 5, 2023

When using ChoiceField(choices=[..., ''], allow_blank=True) then drf-spectacular would add another dupllicate '' value into the schema.

Same happens with ChoiceField(choices=[..., None], allow_null=True).

@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 5, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (6e4180b) 98.57% compared to head (caf0a70) 98.57%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1085   +/-   ##
=======================================
  Coverage   98.57%   98.57%           
=======================================
  Files          68       68           
  Lines        8426     8436   +10     
=======================================
+ Hits         8306     8316   +10     
  Misses        120      120           
Files Coverage Δ
drf_spectacular/plumbing.py 97.07% <100.00%> (ø)
tests/test_plumbing.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Copy Markdown
Owner

@tfranzel tfranzel left a comment

Choose a reason for hiding this comment

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

Excellent PR. Thanks @intgr

This is definitely an improvement to prevent useless duplication. However, I think it is generally bad form to explicitly include '' or None in the choice list, since this ought to be covered by the serializer/model field parameters. I don't remember how DRF handles this internally without looking it up, but this here is most likely the right thing to do (in the schema context) in either case.

@tfranzel tfranzel merged commit 6e48a70 into tfranzel:master Oct 22, 2023
@intgr intgr deleted the avoid-choicefield-duplicate-values branch October 22, 2023 14:12
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

Successfully merging this pull request may close these issues.

2 participants