code-specialist / fastapi-keycloak

Keycloak integration for Python FastAPI
https://fastapi-keycloak.code-specialist.com/
Apache License 2.0
193 stars 50 forks source link

Error on "get_user" when user doesn't exists #53

Open fabiothz opened 2 years ago

fabiothz commented 2 years ago

Wen i try to search for a user that doesn't exist, an error occurs:

{
  "detail": "list index out of range"
}

Occurs in the line below, as the API returns a empty array.

https://github.com/code-specialist/fastapi-keycloak/blob/28f5f7bf18c25c8677202ad2f1d3c2437763a58d/fastapi_keycloak/api.py#L833

A suggestion would be to check before instantiating the object:

response_json = response.json()
return KeycloakUser(**response_json[0]) if len(response_json) else None

This way the API will allow me to check if a user exists in keycloak. What do you think?

yannicschroeer commented 2 years ago

Hey @fabiothz. Nice catch, this really shouldn't happen.

Personally, I would expect an error to be thrown here. Something like a UserNotFound exception. This would be the most explicit and customizable behavior I can think of. I would wait for @JonasScholl opinion on this

stratosgear commented 2 years ago

I believe this can now be closed! @fabiothz Are you still experiencing the issue with the latest release?