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

many=True inconsistent behavior for non-200 status_code #402

Closed
jerinpetergeorge opened this issue May 22, 2021 · 6 comments
Closed

many=True inconsistent behavior for non-200 status_code #402

jerinpetergeorge opened this issue May 22, 2021 · 6 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@jerinpetergeorge
Copy link

Consider I have a view as below,

# serializers.py
class EmptyPayloadResponseSerializer(serializers.Serializer):
    detail = **serializers.CharField()


# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema
from .serializers import EmptyPayloadResponseSerializer


class EmptyPayloadAPI(APIView):
    @extend_schema(responses=EmptyPayloadResponseSerializer(many=True))
    def post(self, request, *args, **kwargs):
        # some actions
        return Response(data={"detail": "Success"})

Specifically, focus on the responses=EmptyPayloadResponseSerializer(many=True) statement

I have got the expected behavior

[
  {
    "detail": "string"
  }
]

So fo so good!!!

Then, I have changed the responses parameter as,

from rest_framework.views import APIView
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema
from .serializers import EmptyPayloadResponseSerializer


class EmptyPayloadAPI(APIView):
    @extend_schema(
        responses={
            200: EmptyPayloadResponseSerializer(many=True),
            400: EmptyPayloadResponseSerializer(many=True),
        }
    )
    def post(self, request, *args, **kwargs):
        # some actions
        return Response(data={"detail": "Success"})

and I have got the schema as,
image

Is this the intended behavior? 🤔

@tfranzel
Copy link
Owner

hey! yes we changed that on purpose. the other argument is in #277. the rationale was that responses in the error range (X<200 && X>= 300) are usually not paginated, filtered or listed. i figured that this is simply more common compared to your use-case.

i have to investigate if it is possible to honor that many=True there for 400.

@tfranzel tfranzel added the bug Something isn't working label May 24, 2021
@ConnorLanglois
Copy link

ConnorLanglois commented Jul 22, 2021

@tfranzel I am also having this same issue. Is there perhaps an estimation on when a fix for this will be pushed out or any guidance you could give for anyone looking to make a PR to fix this, such as where in the code we can find this behavior? Also, in the meantime, is there any workaround we can apply?

Edit: I see this line here is responsible for the behavior:

and '200' <= status_code < '300'
Could we switch this to be a setting so that those of us who enable it can have lists in our error bodies?

@tfranzel
Copy link
Owner

regarding my earlier comment:

i have to investigate if it is possible to honor that many=True there for 400.

pretty certain at this point that it is not possible make it do the right thing by design. DRF simply does not provide enough information for us at this point to make a more educated decision.

@ConnorLanglois : i'll look into it again, but a setting is the most likely outcome. can't see any other way to serve both use-cases.

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Jul 23, 2021
@tfranzel
Copy link
Owner

@ConnorLanglois @jerinpetergeorge introduced a new setting to choose the behavior. default is the current behavior.

@ConnorLanglois
Copy link

@tfranzel Nice work man! Really appreciate the hard work and fast response time. Yeah that setting looks perfect, any clue on when next release is?

@tfranzel
Copy link
Owner

thanks! got one more issue to look at after which i will do a release.

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

3 participants