From 4136a5fd5e7bde6bb091a82de34d5661235bbfc8 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Fri, 31 Jan 2020 15:33:43 +0100 Subject: [PATCH 1/2] List choices for choice fields as enums Fixes #4062 Signed-off-by: Tomas Slusny --- netbox/utilities/custom_inspectors.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/netbox/utilities/custom_inspectors.py b/netbox/utilities/custom_inspectors.py index 68fe57d82..553d98982 100644 --- a/netbox/utilities/custom_inspectors.py +++ b/netbox/utilities/custom_inspectors.py @@ -76,26 +76,28 @@ class CustomChoiceFieldInspector(FieldInspector): SwaggerType, _ = self._get_partial_types(field, swagger_object_type, use_references, **kwargs) if isinstance(field, ChoiceField): - value_schema = openapi.Schema(type=openapi.TYPE_STRING) + choices = field._choices + choice_value = list(choices.keys()) + choice_label = list(choices.values()) + value_schema = openapi.Schema(type=openapi.TYPE_STRING, enum=choice_value) - choices = list(field._choices.keys()) - if set([None] + choices) == {None, True, False}: + if set([None] + choice_value) == {None, True, False}: # DeviceType.subdevice_role, Device.face and InterfaceConnection.connection_status all need to be # differentiated since they each have subtly different values in their choice keys. # - subdevice_role and connection_status are booleans, although subdevice_role includes None # - face is an integer set {0, 1} which is easily confused with {False, True} schema_type = openapi.TYPE_STRING - if all(type(x) == bool for x in [c for c in choices if c is not None]): + if all(type(x) == bool for x in [c for c in choice_value if c is not None]): schema_type = openapi.TYPE_BOOLEAN - value_schema = openapi.Schema(type=schema_type) + value_schema = openapi.Schema(type=schema_type, enum=choice_value) value_schema['x-nullable'] = True - if isinstance(choices[0], int): + if isinstance(choice_value[0], int): # Change value_schema for IPAddressFamilyChoices, RackWidthChoices - value_schema = openapi.Schema(type=openapi.TYPE_INTEGER) + value_schema = openapi.Schema(type=openapi.TYPE_INTEGER, enum=choice_value) schema = SwaggerType(type=openapi.TYPE_OBJECT, required=["label", "value"], properties={ - "label": openapi.Schema(type=openapi.TYPE_STRING), + "label": openapi.Schema(type=openapi.TYPE_STRING, enum=choice_label), "value": value_schema }) From 920078a738242a21b7c283d8b25569e1c3646743 Mon Sep 17 00:00:00 2001 From: kobayashi Date: Thu, 5 Mar 2020 23:52:33 -0500 Subject: [PATCH 2/2] set fix#4062 to release note --- docs/release-notes/version-2.7.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release-notes/version-2.7.md b/docs/release-notes/version-2.7.md index 72d88b743..581d395e1 100644 --- a/docs/release-notes/version-2.7.md +++ b/docs/release-notes/version-2.7.md @@ -5,6 +5,7 @@ ## Enhancements * [#3949](https://github.com/netbox-community/netbox/issues/3949) - Revised the installation docs and upgrade script to employ a Python virtual environment +* [#4062](https://github.com/netbox-community/netbox/issues/4062) - Enumerate ChoiceField type and value in API * [#4119](https://github.com/netbox-community/netbox/issues/4119) - Extend upgrade script to clear expired user sessions * [#4121](https://github.com/netbox-community/netbox/issues/4121) - Add dynamic lookup expressions for all filters * [#4218](https://github.com/netbox-community/netbox/issues/4218) - Allow negative voltage for DC power feeds