You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the authentik-client Python library to interact with the Authentik API, we encounter validation errors due to the API returning null values for certain fields that are expected to be non-null strings (StrictStr) in the client library's Pydantic models.
The problematic fields are:
assigned_application_slug
assigned_application_name
assigned_backchannel_application_slug
assigned_backchannel_application_name
These fields are defined as StrictStr in the Pydantic models, which do not accept None values. As a result, when the API returns null for these fields, deserialization fails with a ValidationError.
To Reproduce
Steps to reproduce the behavior:
Set up an Authentik instance (version 2024.10.4) using Docker Compose.
Install the authentik-client Python library and dependencies:
The pip output indicates the following versions for Pydantic and related packages:
Using cached pydantic-2.10.1-py3-none-any.whl (455 kB)
Using cached pydantic_core-2.27.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB)
Using cached annotated_types-0.7.0-py3-none-any.whl (13 kB)
Use the following script to retrieve OAuth2 providers:
importauthentik_clientfromauthentik_client.apiimportProvidersApifromauthentik_client.exceptionsimportApiException# Configure API clientconfiguration=authentik_client.Configuration(
host="https://your-authentik-instance",
access_token="your_api_token"
)
api_client=authentik_client.ApiClient(configuration)
providers_api=ProvidersApi(api_client)
# Attempt to list OAuth2 providerstry:
providers=providers_api.providers_oauth2_list()
forproviderinproviders.results:
print(f"Provider Name: {provider.name}")
exceptApiExceptionase:
print(f"An error occurred: {e}")
Run the script.
Expected behavior
The script should successfully retrieve and print the names of the OAuth2 providers without any errors (even if they are not associated with any application).
Actual behavior
The script fails with a ValidationError from Pydantic, indicating that the fields received None values, which are not valid for StrictStr. The error occurs during deserialization of the API response.
Example error message:
ValidationError: 4 validation errors for OAuth2Provider
assigned_application_slug
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
assigned_application_name
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
assigned_backchannel_application_slug
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
assigned_backchannel_application_name
Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
Logs
No additional logs are available beyond the error message provided above.
The Authentik API returns null for the fields mentioned when they are not set.
The authentik-client library's Pydantic models define these fields as StrictStr, which does not accept None values.
This issue affects multiple models, including OAuth2Provider and Provider.
According to the API documentation, these fields are not optional and thus cannot be null.
Workaround
As a temporary workaround, we created a custom ApiClient subclass that preprocesses the API responses to replace null values with empty strings before deserialization. This approach avoids modifying the client library or Pydantic models directly.
Here is the code for the custom ApiClient:
importjsonfromauthentik_client.api_clientimportApiClientimportlogginglogger=logging.getLogger(__name__)
classCustomApiClient(ApiClient):
defdeserialize(self, response_text, response_type):
"""Deserializes response into an object after preprocessing."""ifresponse_text:
try:
data=json.loads(response_text)
exceptValueError:
returnsuper().deserialize(response_text, response_type)
data=self.preprocess_data(data)
logger.debug("Data after preprocessing: %s", data)
response_text=json.dumps(data)
returnsuper().deserialize(response_text, response_type)
defpreprocess_data(self, data):
"""Recursively replace null or missing values with empty strings for specific fields."""ifisinstance(data, dict):
self.replace_null_fields(data)
forkey, valueindata.items():
ifisinstance(value, (dict, list)):
data[key] =self.preprocess_data(value)
elifisinstance(data, list):
data= [self.preprocess_data(item) foritemindata]
returndatadefreplace_null_fields(self, item):
"""Helper method to replace null or missing fields in a dictionary item."""forfieldin [
'assigned_application_slug',
'assigned_application_name',
'assigned_backchannel_application_slug',
'assigned_backchannel_application_name'
]:
ifitem.get(field) isNone:
item[field] =''returnitem
Usage of the Custom ApiClient:
# Configure API client with the custom ApiClientconfiguration=authentik_client.Configuration(
host="https://your-authentik-instance",
access_token="your_api_token"
)
api_client=CustomApiClient(configuration)
providers_api=ProvidersApi(api_client)
# Proceed with the same code as beforetry:
providers=providers_api.providers_oauth2_list()
forproviderinproviders.results:
print(f"Provider Name: {provider.name}")
exceptApiExceptionase:
print(f"An error occurred: {e}")
Impact
This issue prevents users from programmatically interacting with the Authentik API using the official client library when these fields are null.
It affects automation scripts and integrations that rely on the authentik-client library.
The workaround adds complexity to the client code and may have performance implications due to recursive data processing.
It requires users to implement and maintain additional code to handle this inconsistency.
Possible Solution
Adjust the Pydantic models in the authentik-client library to accommodate None values for these fields.
Alternatively, modify the API to return empty strings or omit the fields when they are not set.
Modify the client library generation process to handle null values appropriately for these fields.
Request
Please consider updating the authentik-client library's Pydantic models or adjusting the API responses to resolve this inconsistency.
Any guidance on a recommended workaround that doesn't involve modifying the client library code or extensive preprocessing would also be appreciated.
Thank you for your attention to this matter.
The text was updated successfully, but these errors were encountered:
Seconding, I've also run into this issue through very basic use of the API. Without the workaround, this makes the Python API essentially unusable.
Thank you for sharing your workaround so I don't have to figure it out myself ;)
Unfortunately it seems the Python API is not a huge priority for the authentik team -- see #12170. Of course, this isn't a criticism of the team at all, authentik is a great piece of software provided for free under an open-source license on the basis of a great amount of work from both paid team members and volunteers. It would be nice, though, if the API could perhaps be officially spun off into a separate repository where changes and bug reports can more easily be maintained, tracked and worked on.
An open API is one of the best things that software can provide, and it would be really nice to see Authentik's Python API get a bit more love.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Describe the bug
When using the
authentik-client
Python library to interact with the Authentik API, we encounter validation errors due to the API returningnull
values for certain fields that are expected to be non-null strings (StrictStr
) in the client library's Pydantic models.The problematic fields are:
assigned_application_slug
assigned_application_name
assigned_backchannel_application_slug
assigned_backchannel_application_name
These fields are defined as
StrictStr
in the Pydantic models, which do not acceptNone
values. As a result, when the API returnsnull
for these fields, deserialization fails with aValidationError
.To Reproduce
Steps to reproduce the behavior:
Set up an Authentik instance (version
2024.10.4
) using Docker Compose.Install the
authentik-client
Python library and dependencies:The
pip
output indicates the following versions for Pydantic and related packages:Use the following script to retrieve OAuth2 providers:
Run the script.
Expected behavior
The script should successfully retrieve and print the names of the OAuth2 providers without any errors (even if they are not associated with any application).
Actual behavior
The script fails with a
ValidationError
from Pydantic, indicating that the fields receivedNone
values, which are not valid forStrictStr
. The error occurs during deserialization of the API response.Example error message:
Logs
No additional logs are available beyond the error message provided above.
Version and Deployment
2024.10.4
2024.10.4.post1732236734
2.10.1
2.27.1
0.7.0
Additional Context
null
for the fields mentioned when they are not set.authentik-client
library's Pydantic models define these fields asStrictStr
, which does not acceptNone
values.OAuth2Provider
andProvider
.null
.Workaround
As a temporary workaround, we created a custom
ApiClient
subclass that preprocesses the API responses to replacenull
values with empty strings before deserialization. This approach avoids modifying the client library or Pydantic models directly.Here is the code for the custom
ApiClient
:Usage of the Custom
ApiClient
:Impact
null
.authentik-client
library.Possible Solution
authentik-client
library to accommodateNone
values for these fields.null
values appropriately for these fields.Request
authentik-client
library's Pydantic models or adjusting the API responses to resolve this inconsistency.Thank you for your attention to this matter.
The text was updated successfully, but these errors were encountered: