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 Writable Serializers #12256

Closed
amhn opened this issue Apr 14, 2023 · 1 comment
Closed

Warnings on schema generation for Writable Serializers #12256

amhn opened this issue Apr 14, 2023 · 1 comment
Assignees
Labels
beta Concerns a bug/feature in a beta release status: accepted This issue has been accepted for implementation 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 dynamically generated writable serializers:

Warning [InterfaceViewSet > WritableInterfaceSerializer]: unable to resolve type hint for function "l2vpn_termination". Consider using a type hint or @extend_schema_field. 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

As far as I can see this is because the fields are overwritten in NetboxAutoSchema. Because of that the type information and the read_only-Property seems to gets lost.

One possibility to get rid of the warning, is to drop read_only fields in the writable serializers:

diff --git a/netbox/core/api/schema.py b/netbox/core/api/schema.py
index 0b44a3d52..b5b21d67f 100644
--- a/netbox/core/api/schema.py
+++ b/netbox/core/api/schema.py
@@ -149,9 +149,12 @@ class NetBoxAutoSchema(AutoSchema):
 
     def get_writable_class(self, serializer):
         properties = {}
+        remove_fields = []
         fields = {} if hasattr(serializer, 'child') else serializer.fields
 
         for child_name, child in fields.items():
+            if 'read_only' in dir(child) and child.read_only:
+                remove_fields.append(child_name)
             if isinstance(child, (ChoiceField, WritableNestedSerializer)):
                 properties[child_name] = None
             elif isinstance(child, ManyRelatedField) and isinstance(child.child_relation, SerializedPKRelatedField):
@@ -165,7 +168,10 @@ class NetBoxAutoSchema(AutoSchema):
             meta_class = getattr(type(serializer), 'Meta', None)
             if meta_class:
                 ref_name = 'Writable' + self.get_serializer_ref_name(serializer)
-                writable_meta = type('Meta', (meta_class,), {'ref_name': ref_name})
+                fields = list(meta_class.fields)
+                for p in remove_fields:
+                    fields.remove(p)
+                writable_meta = type('Meta', (meta_class,), {'ref_name': ref_name, 'fields': fields})
                 properties['Meta'] = writable_meta
 
             self.writable_serializers[type(serializer)] = type(writable_name, (type(serializer),), properties)

This drops 2 fields from Writable*-components in the generated schema, but they were read_only anyway and should be in the model anyway

  • data_file inPatched WritableConfigContextRequest, WritableConfigContextRequest, WritableConfigTemplateRequest, PatchedWritableConfigTemplateRequest, PatchedWritableExportTemplateRequest, WritableExportTemplateRequest
  • wireless_link in PatchedWritableInterfaceRequest, WritableInterfaceRequest

Diff:

--- a/schema.yml
+++ b/schema.yml
@@ -93569,9 +93569,6 @@ components:
           type: integer
           nullable: true
           description: Remote data source
-        data_file:
-          type: integer
-          nullable: true
         data:
           type: object
           additionalProperties: {}
@@ -93602,9 +93599,6 @@ components:
           type: integer
           nullable: true
           description: Remote data source
-        data_file:
-          type: integer
-          nullable: true
         tags:
           type: array
           items:
@@ -94337,9 +94331,6 @@ components:
           type: integer
           nullable: true
           description: Remote data source
-        data_file:
-          type: integer
-          nullable: true
     PatchedWritableFHRPGroupAssignmentRequest:
       type: object
       description: Adds support for custom fields and tags.
@@ -94683,9 +94674,6 @@ components:
         mark_connected:
           type: boolean
           description: Treat as if a cable is connected
-        wireless_link:
-          type: integer
-          nullable: true
         wireless_lans:
           type: array
           items:
@@ -103563,9 +103551,6 @@ components:
           type: integer
           nullable: true
           description: Remote data source
-        data_file:
-          type: integer
-          nullable: true
         data:
           type: object
           additionalProperties: {}
@@ -103599,9 +103584,6 @@ components:
           type: integer
           nullable: true
           description: Remote data source
-        data_file:
-          type: integer
-          nullable: true
         tags:
           type: array
           items:
@@ -104438,9 +104420,6 @@ components:
           type: integer
           nullable: true
           description: Remote data source
-        data_file:
-          type: integer
-          nullable: true
       required:
       - content_types
       - name
 -104821,9 +104800,6 @@ components:
         mark_connected:
           type: boolean
           description: Treat as if a cable is connected
-        wireless_link:
-          type: integer
-          nullable: true
         wireless_lans:
           type: array
           items:

@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
@jeremystretch jeremystretch added the status: accepted This issue has been accepted for implementation label Apr 20, 2023
jeremystretch added a commit that referenced this issue Apr 21, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
beta Concerns a bug/feature in a beta release status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
Development

No branches or pull requests

3 participants