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

extend_schema with OpenApiResponse results in description: null in generated schema #591

Closed
eviltwin opened this issue Oct 31, 2021 · 2 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@eviltwin
Copy link

eviltwin commented Oct 31, 2021

Describe the bug
When using extend_schema to add examples to an endpoint, if you do not give a value for the OpenApiResponse's description parameter then it will produce a schema with description: null rather than description: ''. This is invalid according to the specification.

This prevents the schema from being used by openapi-python-client to generate client libraries.

I think this is because the default value in OpenApiResponse.__init__ is None when it should be ''.

Obviously, there is a workaround of simply providing description='' to OpenApiResponse (or, you know, an actual description).

To Reproduce

class WidgetViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows widgets to be viewed or edited.
    """

    queryset = Widget.objects.all()
    serializer_class = WidgetSerializer
    permission_classes = [permissions.IsAuthenticated]

    @extend_schema(
        responses={
            200: OpenApiResponse(
                response=WidgetSerializer,
                examples=[
                    OpenApiExample(
                        "Undamaged Widget",
                        value={
                            "is_damaged": False,
                            "damage_reason": "",
                        },
                    ),
                    OpenApiExample(
                        "Damaged Widget",
                        value={
                            "is_damaged": True,
                            "damage_reason": "Someone broke it.",
                        },
                    ),
                ],
            )
        }
    )
    def retrieve(self, request, *args, **kwargs):
        return super().retrieve(request, *args, **kwargs)
        

Expected behavior
The schema should contain description: '' when no description parameter is passed to OpenApiResponse, just like it does by default when not using extend_schema.

@tfranzel tfranzel added bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Oct 31, 2021
@tfranzel
Copy link
Owner

@eviltwin thx! yes, that was a momentary lapse in judgement that resulted in a regression. should be fixed now.

@eviltwin
Copy link
Author

eviltwin commented Nov 1, 2021

thanks! change looks like it will do the right thing so I'll close this :)

@eviltwin eviltwin closed this as completed Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants