mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
fixes choice field schema for response #12960
This commit is contained in:
committed by
Jeremy Stretch
parent
7f96c7fee7
commit
49af70a77d
@ -1,5 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
import typing
|
import typing
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
from drf_spectacular.extensions import OpenApiSerializerFieldExtension
|
from drf_spectacular.extensions import OpenApiSerializerFieldExtension
|
||||||
from drf_spectacular.openapi import AutoSchema
|
from drf_spectacular.openapi import AutoSchema
|
||||||
@ -28,14 +29,19 @@ class ChoiceFieldFix(OpenApiSerializerFieldExtension):
|
|||||||
target_class = 'netbox.api.fields.ChoiceField'
|
target_class = 'netbox.api.fields.ChoiceField'
|
||||||
|
|
||||||
def map_serializer_field(self, auto_schema, direction):
|
def map_serializer_field(self, auto_schema, direction):
|
||||||
|
build_cf = build_choice_field(self.target)
|
||||||
|
|
||||||
if direction == 'request':
|
if direction == 'request':
|
||||||
return build_choice_field(self.target)
|
return build_cf
|
||||||
|
|
||||||
elif direction == "response":
|
elif direction == "response":
|
||||||
|
value = build_cf
|
||||||
|
label = {**build_basic_type(OpenApiTypes.STR), "enum": list(OrderedDict.fromkeys(self.target.choices.values()))}
|
||||||
|
|
||||||
return build_object_type(
|
return build_object_type(
|
||||||
properties={
|
properties={
|
||||||
"value": build_basic_type(OpenApiTypes.STR),
|
"value": value,
|
||||||
"label": build_basic_type(OpenApiTypes.STR),
|
"label": label
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user