Skip to content

Commit 49af70a

Browse files
abhi1693jeremystretch
authored andcommitted
fixes choice field schema for response #12960
1 parent 7f96c7f commit 49af70a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

netbox/core/api/schema.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import typing
3+
from collections import OrderedDict
34

45
from drf_spectacular.extensions import OpenApiSerializerFieldExtension
56
from drf_spectacular.openapi import AutoSchema
@@ -28,14 +29,19 @@ class ChoiceFieldFix(OpenApiSerializerFieldExtension):
2829
target_class = 'netbox.api.fields.ChoiceField'
2930

3031
def map_serializer_field(self, auto_schema, direction):
32+
build_cf = build_choice_field(self.target)
33+
3134
if direction == 'request':
32-
return build_choice_field(self.target)
35+
return build_cf
3336

3437
elif direction == "response":
38+
value = build_cf
39+
label = {**build_basic_type(OpenApiTypes.STR), "enum": list(OrderedDict.fromkeys(self.target.choices.values()))}
40+
3541
return build_object_type(
3642
properties={
37-
"value": build_basic_type(OpenApiTypes.STR),
38-
"label": build_basic_type(OpenApiTypes.STR),
43+
"value": value,
44+
"label": label
3945
}
4046
)
4147

0 commit comments

Comments
 (0)