|
20 | 20 |
|
21 | 21 | from drf_spectacular.openapi import AutoSchema |
22 | 22 | from drf_spectacular.plumbing import ( |
23 | | - analyze_named_regex_pattern, build_basic_type, detype_pattern, follow_field_source, |
24 | | - force_instance, get_list_serializer, is_field, is_serializer, resolve_type_hint, |
| 23 | + analyze_named_regex_pattern, build_basic_type, build_choice_field, detype_pattern, |
| 24 | + follow_field_source, force_instance, get_list_serializer, is_field, is_serializer, |
| 25 | + resolve_type_hint, |
25 | 26 | ) |
26 | 27 | from drf_spectacular.validation import validate_schema |
27 | 28 | from tests import generate_schema |
@@ -358,3 +359,21 @@ def test_analyze_named_regex_pattern(no_warnings, pattern, output): |
358 | 359 | def test_unknown_basic_type(capsys): |
359 | 360 | build_basic_type(object) |
360 | 361 | assert 'could not resolve type for "<class \'object\'>' in capsys.readouterr().err |
| 362 | + |
| 363 | + |
| 364 | +def test_choicefield_choices_enum(): |
| 365 | + schema = build_choice_field(serializers.ChoiceField(['bluepill', 'redpill'])) |
| 366 | + assert schema['enum'] == ['bluepill', 'redpill'] |
| 367 | + assert schema['type'] == 'string' |
| 368 | + |
| 369 | + schema = build_choice_field(serializers.ChoiceField( |
| 370 | + ['bluepill', 'redpill'], allow_null=True, allow_blank=True |
| 371 | + )) |
| 372 | + assert schema['enum'] == ['bluepill', 'redpill', '', None] |
| 373 | + assert schema['type'] == 'string' |
| 374 | + |
| 375 | + schema = build_choice_field(serializers.ChoiceField( |
| 376 | + choices=['bluepill', 'redpill', '', None], allow_null=True, allow_blank=True |
| 377 | + )) |
| 378 | + assert schema['enum'] == ['bluepill', 'redpill', '', None] |
| 379 | + assert 'type' not in schema |
0 commit comments