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

paginate decorator causes validation to fail #1011

Open
johnbridstrup opened this issue Dec 16, 2023 · 3 comments
Open

paginate decorator causes validation to fail #1011

johnbridstrup opened this issue Dec 16, 2023 · 3 comments

Comments

@johnbridstrup
Copy link

johnbridstrup commented Dec 16, 2023

I have used the @paginate decorator successfully multiple times now, but for some reason it is causing validation to fail on a new endpoint (that works without pagination). I have a feeling this is because of the schema, which has a nested schema and a resolved value, because that is the only real difference between it and the working ones. Am I missing something from the documentation or is there a known reason this is happening?

Schema

class MyModelResponseSchema(ModelSchema):
    related_model: RelatedModelResponseSchema
    calculated_property: int
    creator: UserLimitedSchema

    @staticmethod
    def resolve_calculated_property(obj):
        return obj.calculated_property

    class Meta:
        model = MyModel
        fields = [
            "related_model",
            "max_amount",
            "max_increment",
            "creator",
        ]

Endpoint

@router.get(
    "myEndpoint",
    auth=JWTAuth(),
    response={200: List[MyModelResponseSchema], codes_4xx: DefaultError},
    url_name="service_my_endpoint",
)
@paginate  # <--- Works fine without this decorator
def my_view(request):
    return 200, MyModel.objects.all()

Output

pydantic_core._pydantic_core.ValidationError: 8 validation errors for NinjaResponseSchema
response.items.0.related_model
  Field required [type=missing, input_value=<ninja.schema.DjangoGette...bject at 0x7f8bbb8852c0>, input_type=DjangoGetter]
response.items.0.calculated_property
  Field required [type=missing, input_value=<ninja.schema.DjangoGette...bject at 0x7f8bbb8852c0>, input_type=DjangoGetter]
response.items.0.creator
  Field required [type=missing, input_value=<ninja.schema.DjangoGette...bject at 0x7f8bbb8852c0>, input_type=DjangoGetter]
response.items.0.max_amount
  Field required [type=missing, input_value=<ninja.schema.DjangoGette...bject at 0x7f8bbb8852c0>, input_type=DjangoGetter]
...

Any help would be nice, thanks!

@johnbridstrup
Copy link
Author

Okay, I've removed the nested schemas and resolver and its still happening. I am now very confused why its specifically happening for this model/endpoint and not the others, which appear to be exactly the same (return .all() for a simple query)

@johnbridstrup
Copy link
Author

I figured out why this is happening. It seems you cannot include an explicit response code when returning from the method. I think this should be probably allowed. I'll look into making a PR at some point

@johnbridstrup
Copy link
Author

from the apply pagination method

{'items': <QuerySet [<Model1: 1>, <Model1: 2>]>, 'count': 2}
{'items': (200, <QuerySet [<Model2: 1>]>), 'count': 2}  # <--- It is trying to paginate the tuple

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