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

[BUG] paginate decorator with custom HTTP status response={200: ..., 201: ...} doesn't work #1147

Closed
benjaoming opened this issue May 2, 2024 · 3 comments

Comments

@benjaoming
Copy link
Contributor

benjaoming commented May 2, 2024

Describe the bug

It seems that customizing the status of a response by returning <response_code>, object_list doesn't work well for list views.

Maybe it's difficult to fix, as the tuple is already iterable and will present itself as the object list itself?

But what is the workaround then?

Details

The following produces errors:

@api.get(
    "/organizations",
    response={
        200: List[OrganizationPublicSchema],
        201: List[OrganizationPrivateSchema],
    },
)
@paginate
def list_organizations(request):
    if request.user.is_anonymous:
        return 200, models.Organization.objects.filter(public=True)
    else:
        return 201, models.Organization.objects.filter(user=request.user)

The error is as if we simply didn't supply the right object back... which kind of is true, but django-ninja is also supposed to understand the status code tuple?

4 validation errors for NinjaResponseSchema
response.items.0.title
  Field required [type=missing, input_value=<DjangoGetter: 200>, input_type=DjangoGetter]
    For further information visit https://errors.pydantic.dev/2.6/v/missing
response.items.0.country
  Field required [type=missing, input_value=<DjangoGetter: 200>, input_type=DjangoGetter]
    For further information visit https://errors.pydantic.dev/2.6/v/missing
response.items.1.title
  Field required [type=missing, input_value=<DjangoGetter: <Organizat...zation: Clark-Fisher>]>>, input_type=DjangoGetter]
    For further information visit https://errors.pydantic.dev/2.6/v/missing
response.items.1.country
  Field required [type=missing, input_value=<DjangoGetter: <Organizat...zation: Clark-Fisher>]>>, input_type=DjangoGetter]
    For further information visit https://errors.pydantic.dev/2.6/v/missing

However, this works fine:

@api.get(
    "/organizations",
    response=List[OrganizationPublicSchema],
)
@paginate
def list_organizations(request):
    return Organization.objects.all()

Versions (please complete the following information):

  • Python version: 3.11
  • Django version: 5.0.3
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.6.4
@benjaoming
Copy link
Contributor Author

There's a test case that seems to work well 🤔

@api.get("/check_list_model", response={200: List[UserModel]})
def check_list_model(request):
return 200, [User(1, "John", "Password")]

@benjaoming
Copy link
Contributor Author

Aha!

It's the @paginate decorator that's causing it. Removing it makes the issue go away.

Indeed, maybe pagination shouldn't be expected to work seemlessly on a list view with many response types?

@benjaoming
Copy link
Contributor Author

aaaaand it's a duplicate of #940 👍

@benjaoming benjaoming changed the title [BUG] ModelSchema lists and custom HTTP status response={200: ..., 201: ...} doesn't work well [BUG] paginate decorator with custom HTTP status response={200: ..., 201: ...} doesn't work May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant