Skip to content
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

Warnings on schema generation for FilterSets #12257

Closed
amhn opened this issue Apr 14, 2023 · 5 comments · Fixed by #12987
Closed

Warnings on schema generation for FilterSets #12257

amhn opened this issue Apr 14, 2023 · 5 comments · Fixed by #12987
Labels
pending closure Requires immediate attention to avoid being closed for inactivity status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation topic: OpenAPI type: housekeeping Changes to the application which do not directly impact the end user

Comments

@amhn
Copy link
Contributor

amhn commented Apr 14, 2023

NetBox version

v3.5-beta

Python version

3.10

Steps to Reproduce

Generate schema using, e.g.

./netbox/manage.py spectacular --color --file schema.json --validate --format openapi-json

Expected Behavior

No warnings are displayed.

Observed Behavior

Currently several warnings are displayed for filtersets:

/home/andy/python/netbox/netbox/ipam/filtersets.py:706: Warning [FHRPGroupViewSet > FHRPGroupFilterSet]: Unable to guess choice types from values, filter method's type hint or find "related_ip" in model. Defaulting to string.
/home/andy/python/netbox/netbox/ipam/filtersets.py:502: Warning [IPAddressViewSet > IPAddressFilterSet]: Unable to guess choice types from values, filter method's type hint or find "present_in_vrf_id" in model. Defaulting to string.
/home/andy/python/netbox/netbox/ipam/filtersets.py:502: Warning [IPAddressViewSet > IPAddressFilterSet]: Unable to guess choice types from values, filter method's type hint or find "present_in_vrf" in model. Defaulting to string.
@amhn amhn added the type: bug A confirmed report of unexpected behavior in the application label Apr 14, 2023
@amhn
Copy link
Contributor Author

amhn commented Apr 14, 2023

drf-spectacular can not infer the parameter type for django_filters.ModelChoiceFilter fields. They should be added via extend_schema_field.

For present_in_vrf_id and present_in_vrf the annotation is added to the field directly to preserve the field description in the schema. The description is duplicated because once there is an override, no further introspection is done.

Possible diff:

diff --git a/netbox/ipam/filtersets.py b/netbox/ipam/filtersets.py
index a128b6acc..459f847d0 100644
--- a/netbox/ipam/filtersets.py
+++ b/netbox/ipam/filtersets.py
@@ -6,6 +6,9 @@ from django.db.models import Q
 from django.utils.translation import gettext as _
 from netaddr.core import AddrFormatError
 
+from drf_spectacular.utils import extend_schema_field
+from drf_spectacular.types import OpenApiTypes
+
 from dcim.models import Device, Interface, Region, Site, SiteGroup
 from netbox.filtersets import ChangeLoggedModelFilterSet, OrganizationalModelFilterSet, NetBoxModelFilterSet
 from tenancy.filtersets import TenancyFilterSet
@@ -284,17 +287,17 @@ class PrefixFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
         to_field_name='rd',
         label=_('VRF (RD)'),
     )
-    present_in_vrf_id = django_filters.ModelChoiceFilter(
+    present_in_vrf_id = extend_schema_field({'type': 'string', 'description': 'VRF'})(django_filters.ModelChoiceFilter(
         queryset=VRF.objects.all(),
         method='filter_present_in_vrf',
         label=_('VRF')
-    )
-    present_in_vrf = django_filters.ModelChoiceFilter(
+    ))
+    present_in_vrf = extend_schema_field({'type': 'string', 'description': 'VRF (RD)'})(django_filters.ModelChoiceFilter(
         queryset=VRF.objects.all(),
         method='filter_present_in_vrf',
         to_field_name='rd',
         label=_('VRF (RD)'),
-    )
+    ))
     region_id = TreeNodeMultipleChoiceFilter(
         queryset=Region.objects.all(),
         field_name='site__region',
@@ -526,17 +529,17 @@ class IPAddressFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
         to_field_name='rd',
         label=_('VRF (RD)'),
     )
-    present_in_vrf_id = django_filters.ModelChoiceFilter(
+    present_in_vrf_id = extend_schema_field({'type': 'string', 'description': 'VRF'})(django_filters.ModelChoiceFilter(
         queryset=VRF.objects.all(),
         method='filter_present_in_vrf',
         label=_('VRF')
-    )
-    present_in_vrf = django_filters.ModelChoiceFilter(
+    ))
+    present_in_vrf = extend_schema_field({'type': 'string', 'description': 'VRF (RD)'})(django_filters.ModelChoiceFilter(
         queryset=VRF.objects.all(),
         method='filter_present_in_vrf',
         to_field_name='rd',
         label=_('VRF (RD)'),
-    )
+    ))
     device = MultiValueCharFilter(
         method='filter_device',
         field_name='name',
@@ -727,6 +730,7 @@ class FHRPGroupFilterSet(NetBoxModelFilterSet):
             Q(name__icontains=value)
         )
 
+    @extend_schema_field(OpenApiTypes.STR)
     def filter_related_ip(self, queryset, name, value):
         """
         Filter by VRF & prefix of assigned IP addresses.
@@ -941,9 +945,11 @@ class VLANFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
             pass
         return queryset.filter(qs_filter)
 
+    @extend_schema_field(OpenApiTypes.STR)
     def get_for_device(self, queryset, name, value):
         return queryset.get_for_device(value)
 
+    @extend_schema_field(OpenApiTypes.STR)
     def get_for_virtualmachine(self, queryset, name, value):
         return queryset.get_for_virtualmachine(value)

@jeremystretch jeremystretch added type: housekeeping Changes to the application which do not directly impact the end user beta Concerns a bug/feature in a beta release and removed type: bug A confirmed report of unexpected behavior in the application labels Apr 14, 2023
@arthanson arthanson self-assigned this Apr 17, 2023
@arthanson arthanson added status: accepted This issue has been accepted for implementation topic: OpenAPI labels Apr 21, 2023
@jeremystretch jeremystretch removed the beta Concerns a bug/feature in a beta release label May 2, 2023
abhi1693 added a commit that referenced this issue Jun 23, 2023
@abhi1693 abhi1693 assigned abhi1693 and unassigned arthanson Jun 23, 2023
@jeremystretch
Copy link
Member

Looks like @abhi1693 addressed most of these in #12987 but there's still at least one warning to address.

@jeremystretch jeremystretch reopened this Jun 23, 2023
@jeremystretch jeremystretch added status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation and removed status: accepted This issue has been accepted for implementation labels Jun 23, 2023
@abhi1693
Copy link
Member

abhi1693 commented Aug 4, 2023

The remaining warning is Warning [ServiceViewSet > ServiceFilterSet]: model field "IPAddressField" has no mapping in ModelSerializer. It may be a deprecated field. Defaulting to "string"

Here is a potential solution but I have not been able to get this working: tfranzel/drf-spectacular#873 (comment)

Copy link
Contributor

github-actions bot commented Nov 3, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions github-actions bot added the pending closure Requires immediate attention to avoid being closed for inactivity label Nov 3, 2023
Copy link
Contributor

github-actions bot commented Dec 4, 2023

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pending closure Requires immediate attention to avoid being closed for inactivity status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation topic: OpenAPI type: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants