django-ninja-jwt
django-ninja-jwt copied to clipboard
Resolver gets called twice
I am adding a 'user' key to the token claims with the user data including its permissions. Following exactly this I get the tokens with no problem. But resolving the permissions raises a ValidationError, and I realized that the resolver gets called twice: -first time the obj is a User instance and context is None, so no problem for getting the permission list -the second time obj is not a User but a ModelAuthReadSchema instance and context is not None but its user is a AnonymousUser, and here comes the errors
this is my schema for User where AuthModel is just get_user_model()
class ModelAuthReadSchema(ModelSchema):
permissions: List[str] | None
class Meta:
model = AuthModel
fields = ['id', 'username', 'first_name', 'last_name', 'email']
@staticmethod
def resolve_permissions(obj, context):
return get_permissions(obj)
get_permissions is a custom function for getting just business-related permissions
this is the error:
pydantic_core._pydantic_core.ValidationError: 3 validation errors for NinjaResponseSchema
response.refresh
Field required [type=missing, input_value=<DjangoGetter: LoginInput...', username='cccccc')>, input_type=DjangoGetter]
For further information visit https://errors.pydantic.dev/2.6/v/missing
response.access
Field required [type=missing, input_value=<DjangoGetter: LoginInput...', username='cccccc')>, input_type=DjangoGetter]
For further information visit https://errors.pydantic.dev/2.6/v/missing
response.user
Field required [type=missing, input_value=<DjangoGetter: LoginInput...', username='cccccc')>, input_type=DjangoGetter]
For further information visit https://errors.pydantic.dev/2.6/v/missing
this would be the desired output:
{
"refresh": "xxxxxxxxxxx",
"access": "yyyyyyyyyyy",
"user": {
"permissions": [
"app_label1.add_model",
"app_label2.delete_model"
],
"id": 1,
"username": "ccccccc",
"first_name": "",
"last_name": "",
"email": "[email protected]"
}
}