Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

TypeError: Object of type KeycloakRole is not JSON serializable #30

Closed
fherrera124 opened this issue Mar 27, 2022 · 13 comments · Fixed by #31 or #38
Closed

TypeError: Object of type KeycloakRole is not JSON serializable #30

fherrera124 opened this issue Mar 27, 2022 · 13 comments · Fixed by #31 or #38
Assignees
Labels
bug Something isn't working

Comments

@fherrera124
Copy link
Contributor

Whenever I try to create an user with an initial role, I get the error: "TypeError: Object of type KeycloakRole is not JSON serializable"

@yannicschroeer
Copy link
Collaborator

yannicschroeer commented Mar 27, 2022

Hi @fherrera124. Could you please provide the whole user you're trying to register and at which point you get the serializing error?

@fherrera124
Copy link
Contributor Author

fherrera124 commented Mar 27, 2022

Sure, this is my function:

@app.post("/users", tags=["user-management"])
def create_user(first_name: str, last_name: str, email: str, password: SecretStr, initial_roles: List[str]):
    return idp.create_user(first_name=first_name, last_name=last_name,
                           username=email, email=email, password=password.get_secret_value(),
                           initial_roles=initial_roles, send_email_verification=False)

This is the list containing a valid role:

[
  "SUBCOORDINADOR_REGIONAL"
]

The output of the error:

File "/home/server/dockers/xxxx/app/app.py", line 57, in create_user
    return idp.create_user(first_name=first_name, last_name=last_name, username=email, email=email, password=password.get_secret_value(), initial_roles=initial_roles, send_email_verification=False)
  File "/home/server/.cache/pypoetry/virtualenvs/xxxx-7DESS4RH-py3.8/lib/python3.8/site-packages/fastapi_keycloak/api.py", line 57, in wrapper
    result: Response = f(*args, **kwargs)  # The actual call
  File "/home/server/.cache/pypoetry/virtualenvs/xxxx-7DESS4RH-py3.8/lib/python3.8/site-packages/fastapi_keycloak/api.py", line 686, in create_user
    response = self._admin_request(url=self.users_uri, data=data, method=HTTPMethod.POST)
  File "/home/server/.cache/pypoetry/virtualenvs/xxxx-7DESS4RH-py3.8/lib/python3.8/site-packages/fastapi_keycloak/api.py", line 913, in _admin_request
    return requests.request(method=method.name, url=url, data=json.dumps(data), headers=headers)
  File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type KeycloakRole is not JSON serializable

@yannicschroeer
Copy link
Collaborator

yannicschroeer commented Mar 27, 2022

Seems like the response by Keycloak isn't valid JSON or mismatches the Pydantic model. Could you please debug into the Keycloak response (E.g. breakpoint at line 59 of fastapi_keycloak/api.py and the content of result.json()) and provide a (safe) version of the response provided by Keycloak?

@fherrera124
Copy link
Contributor Author

fherrera124 commented Mar 27, 2022

The object returned:

[KeycloakRole(id='xxxxxx2', name='SUBCOORDINADOR_REGIONAL', composite=False, clientRole=False, containerId='Test')]

The object is a list, if I ask for the attribute json():

AttributeError: 'list' object has no attribute 'json'

If try with json.dumps(obj):

TypeError: Object of type KeycloakRole is not JSON serializable

@yannicschroeer yannicschroeer linked a pull request Mar 27, 2022 that will close this issue
@yannicschroeer
Copy link
Collaborator

This was indeed an issue. The initial roles were directly dumped to JSON from the Pydantic model which didn't work quite well. Should be fixed in 1.0.2 I just released

@fherrera124
Copy link
Contributor Author

Thanks, great work.

@fherrera124
Copy link
Contributor Author

fherrera124 commented Mar 27, 2022

@yannicschroeer

Just tested, it always returns error 500 :/

So, for now, first I'm creating the user, and then adding the role to that user (yesterday it worked).

UPDATE: New bug introduced, if there are no initial roles, now fails with this:

File "/usr/local/lib/python3.8/site-packages/fastapi_keycloak/api.py", line 664, in create_user
    initial_roles_json = [role.dict() for role in initial_roles]
TypeError: 'NoneType' object is not iterable

@yannicschroeer
Copy link
Collaborator

Turns out I tested the wrong version 🥲 Also, the Keycloak documentation was misleading so the clientRoles field was totally wrong, the realmRoles field is the correct one. Should be fixed once and for all in v1.0.3.

@fherrera124
Copy link
Contributor Author

fherrera124 commented Mar 27, 2022

Thanks, the errors are gone now. But from the KeyCloak Admin Console I'm seeing that the new users didn't get the initial roles. It's no big deal for me, I will stick with the procedure of creating users without roles, and then add them.

@fherrera124
Copy link
Contributor Author

fherrera124 commented Mar 27, 2022

@yannicschroeer
Copy link
Collaborator

Hmm, seems like that's the case... thanks for pointing that out. For convenience, I'll change the API to make additional requests for the initial roles for now

@yannicschroeer
Copy link
Collaborator

yannicschroeer commented Mar 27, 2022

I'll not say "it's fixed" anymore today. But I think it's finally working as expected in v1.0.4. Though I had to add the initial roles in additional requests.

@yannicschroeer
Copy link
Collaborator

May also be related to keycloak/keycloak#9342. Further introspection required.

@yannicschroeer yannicschroeer added the bug Something isn't working label Mar 27, 2022
@yannicschroeer yannicschroeer self-assigned this Mar 27, 2022
@yannicschroeer yannicschroeer linked a pull request Apr 12, 2022 that will close this issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
3 participants