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

ReDoc ImageField not displayed #95

Closed
BojanKogoj opened this issue Apr 3, 2018 · 8 comments
Closed

ReDoc ImageField not displayed #95

BojanKogoj opened this issue Apr 3, 2018 · 8 comments
Labels
question Open question

Comments

@BojanKogoj
Copy link

I have the following serializer, which will show avatar field in Swagger, but not ReDoc

class AvatarUpdateSerializer(serializers.ModelSerializer):
    avatar = serializers.ImageField()

    class Meta:
        model = User
        fields = ('avatar',)

Screenshots

  • Swagger
    screen shot 2018-04-03 at 15 09 24
  • Redoc
    screen shot 2018-04-03 at 15 17 10

Is this an issue with this library or ReDoc?

@axnsan12
Copy link
Owner

axnsan12 commented Apr 3, 2018

Without the generated swagger YAML or JSON, I couldn't tell. However given that swagger-ui works I would suppose it's a bug in ReDoc.

@BojanKogoj
Copy link
Author

In definitions I could find

  AvatarUpdate:
    type: object
    properties:
      avatar:
        title: Avatar
        type: string
        readOnly: true
        format: uri

That means it's ReDoc issue I suppose.

@axnsan12
Copy link
Owner

axnsan12 commented Apr 3, 2018

Actually, no. It seems to be a problem with the swagger generation (this library) - the field is marked as readOnly, ReDoc is correct in not showing it in the request body parameter.

To get drf-yasg to properly generate request parameters of type file, you must set your parser class on the view to MultiPartParser: https://github.com/axnsan12/drf-yasg/blob/master/testproj/articles/views.py#L103

@BojanKogoj
Copy link
Author

I tried that, with no success. Could it be because of generics? I could be using this completely wrong

class UpdateAvatarView(generics.UpdateAPIView):
    """ Upload users avatar  """
    serializer_class = AvatarUpdateSerializer
    http_method_names = ['patch']
    permission_classes = (
        permissions.IsAuthenticated,
        client_permissions.IsClient)

    def get_object(self, *args, **kwargs):
        return self.request.user

    @swagger_auto_schema(responses={200: UserSerializer()})
    @detail_route(methods=['patch'], parser_classes=(MultiPartParser,))
    def patch(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

@axnsan12
Copy link
Owner

axnsan12 commented Apr 4, 2018

Yes, you are 😄 . @detail_route only applies to ViewSets, and specifically ViewSets when used with routers.

You should just set parser_classes on the view class itself (like the other _classes you have set).

@BojanKogoj
Copy link
Author

This worked:

@parser_classes((MultiPartParser,))
class UpdateAvatarView(generics.UpdateAPIView):

I don't actually use any _classes decorators otherwise, the only view decorator I have in this project is @swagger_auto_schema.

Thanks for your help

@axnsan12
Copy link
Owner

axnsan12 commented Apr 4, 2018

Decorating the class is not necessary

class UpdateAvatarView(generics.UpdateAPIView):
    serializer_class = AvatarUpdateSerializer
    http_method_names = ['patch']
    permission_classes = (
        permissions.IsAuthenticated,
        client_permissions.IsClient)
    parser_classes = (MultiPartParser,)    # <--------- add this

@BojanKogoj
Copy link
Author

Yeah, that's an even better solution. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Open question
Projects
None yet
Development

No branches or pull requests

2 participants