astrosat / django-astrosat-core

Common backend library for Astrosat projects' core functionality
GNU General Public License v3.0
0 stars 0 forks source link

fix swagger issue w/ CurrentUserDefault #41

Closed allynt closed 3 years ago

allynt commented 3 years ago

Describe the feature

Swagger (that is, drf-yasg2) tries to invoke every API view when generating a schema to render. When doing this, obviously, it cannot be "logged in" as a real user. Instead it uses Django's built-in AnonymousUser. This is a very stripped-down version of a User and it certainly doesn't have any of the custom fields of the User model defined in django-astrosat-users.

Some of our views rely on those extra fields, though, to do clever things. The recommended way to get around this issue is to protect the view by checking if it is being called in the context of swagger's schema generation and then just returning immediately if so [as per https://github.com/axnsan12/drf-yasg/issues/333#issuecomment-474883875].

However, this issue also crops up when using CurrentUserDefault w/ SlugRelatedFields on the serializer (since AnonymousUser is very unlikely to have the slug_field referred to in the serializer definition). Therefore, I should add a similar check to my custom SwaggerCurrentUserDefault class.

Additional context

This is not required, it doesn't affect functionality. But it does spam the server logs w/ crap that makes it hard for me to isolate real errors.