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

Minor DRF traceback #9878

Closed
ryanmerolle opened this issue Jul 29, 2022 · 5 comments
Closed

Minor DRF traceback #9878

ryanmerolle opened this issue Jul 29, 2022 · 5 comments
Assignees
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@ryanmerolle
Copy link
Contributor

NetBox version

v3.2.7

Python version

3.10

Steps to Reproduce

  1. Setup a fresh install of NetBox
  2. Setup logging to debug (may not be required)
  3. Open Django Swagger page
  4. Observe below error in the logs:
default for PrimaryKeyRelatedField(allow_null=True, default=CurrentUserDefault(), queryset=<QuerySet [<User: admin>]>, required=False) is callable but it raised an exception when called; 'default' will not be set on schema
Traceback (most recent call last):
File "/opt/netbox/venv/lib/python3.10/site-packages/drf_yasg/utils.py", line 487, in get_field_default
default = default(field)
File "/opt/netbox/venv/lib/python3.10/site-packages/rest_framework/fields.py", line 290, in __call__
return serializer_field.context['request'].user
KeyError: 'request'
172.24.0.1 - - [28/Jul/2022:16:54:33 +0000] "GET /api/docs/?format=openapi HTTP/1.1" 200 1074260 "http://localhost:8000/api/docs/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"

Expected Behavior

No Error

Observed Behavior

Nothing major, just noise in the logs and possibly a delay in loading the swagger page.

@ryanmerolle ryanmerolle added the type: bug A confirmed report of unexpected behavior in the application label Jul 29, 2022
@jeremystretch jeremystretch added the status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation label Aug 1, 2022
@arthanson
Copy link
Collaborator

arthanson commented Aug 26, 2022

Looks like it is caused by CurrentUserDefault in the following code:

class JournalEntrySerializer(NetBoxModelSerializer):
    ...
    created_by = serializers.PrimaryKeyRelatedField(
        allow_null=True,
        queryset=User.objects.all(),
        required=False,
        default=serializers.CurrentUserDefault()
    )

@ryanmerolle
Copy link
Contributor Author

Great find @arthanson !

@amhn
Copy link
Contributor

amhn commented Aug 31, 2022

Did look into it. The default function is called for JournalEntrySerializer and WritableJournalEntrySerializer. On the call for WritableJournalEntry the context is empty.

The following diff fixes the traceback, but I am not yet sure if it has other implications or if more variables should be added:

diff --git a/netbox/utilities/custom_inspectors.py b/netbox/utilities/custom_inspectors.py
index 258399e86..3a3ae3f71 100644
--- a/netbox/utilities/custom_inspectors.py
+++ b/netbox/utilities/custom_inspectors.py
@@ -32,9 +32,9 @@ class NetBoxSwaggerAutoSchema(SwaggerAutoSchema):
             if writable_class is not None:
                 if hasattr(serializer, 'child'):
                     child_serializer = self.get_writable_class(serializer.child)
-                    serializer = writable_class(child=child_serializer)
+                    serializer = writable_class(context=serializer.context, child=child_serializer)
                 else:
-                    serializer = writable_class()
+                    serializer = writable_class(context=serializer.context)
         return serializer
 
     def get_writable_class(self, serializer):

@arthanson arthanson self-assigned this Aug 31, 2022
@arthanson arthanson removed the status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation label Aug 31, 2022
@DanSheps DanSheps added the status: accepted This issue has been accepted for implementation label Sep 6, 2022
@arthanson arthanson added the status: blocked Another issue or external requirement is preventing implementation label Sep 7, 2022
@arthanson
Copy link
Collaborator

Marking as blocked as we should figure out #9608 first.

@arthanson arthanson removed their assignment Oct 12, 2022
@arthanson arthanson added the status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation label Oct 12, 2022
@jeremystretch
Copy link
Member

I tested @amhn's proposed change and the only difference in the schema output was adding a default value of 1 for WritableJournalEntry. Seems like a valid fix to me.

@jeremystretch jeremystretch removed status: blocked Another issue or external requirement is preventing implementation status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Nov 17, 2022
@jeremystretch jeremystretch self-assigned this Nov 17, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

5 participants