-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collisions of enum fields in openapi spec #12149
Comments
To resolve this this the enums shoud be added to ENUM_NAME_OVERRIDES in SPECTACULAR_SETTINGS. Preferably before the 3.5 release to prevent a breaking change after the release. The following settings should be set. Names are of course only suggestions. For the 3 enums with XX in the name I am not sure what they should be named.
I suspect resolving some of the other current warnings will result in further enums that need to be added here. |
Resolving this issue as mentioned may not be ideal for plugin developers. This will add unnecessary burden of maintaining enum types by both core and plugin. Probably there is a better way of resolving this. |
Agreed with @abhi1693. I also worry about the long-term maintainability of this approach. Can we look deeper into how enums are intended to be discovered dynamically? |
Note: These are all (?) from the Writable serializers, these are created In core.api.schema.NetBoxAutoSchema.get_writable_class. There is already a check for the ChoiceField so potentially it might be possible to auto-manage the name there to prevent a collision. |
Most (or all?) of the collisions are from writable serializers because most enums are created from ChoiceField. ChoiceField is rendered as string because of ChoiceFieldFix[1]. This fix is not applied to ChoiceFields from writable serializers. The easiest way to fix this is disabling Any other way seems more complicated, since [1] ChoiceFieldFix could be improved to provide the allowed choices by using build_choice_field instead of build_generic_type: diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py
index 0b44a3d52..50d7b4ff8 100644
--- a/netbox/core/api/schema.py
+++ b/netbox/core/api/schema.py
@@ -12,6 +12,7 @@ from drf_spectacular.plumbing import (
build_basic_type,
build_media_type_object,
build_object_type,
+ build_choice_field,
is_serializer,
)
from drf_spectacular.types import OpenApiTypes
@@ -38,7 +39,7 @@ class ChoiceFieldFix(OpenApiSerializerFieldExtension):
def map_serializer_field(self, auto_schema, direction):
if direction == 'request':
- return build_basic_type(OpenApiTypes.STR)
+ return build_choice_field(self.target)
elif direction == "response":
return build_object_type( At least for requests, responses are more complicated, at least the label part. |
NetBox version
v3.5-beta
Python version
3.10
Steps to Reproduce
Run
./manage.py spectacular --file ../../terraform/go-netbox-openapi/schema.json --validate --format openapi-json
with the beta version.Expected Behavior
Names for enums are predictable and meaningful.
Observed Behavior
Names like Type666Enum, Type2fcEnum, Type6e5Enum, Type653Enum and Type209Enum are chosen for some of the ChoiceFields named Type.
The text was updated successfully, but these errors were encountered: