FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Verified Commit 417a7c84 authored by Sebastiaan ten Pas's avatar Sebastiaan ten Pas
Browse files

fix: keep snake case in openapi schema token authentication

parent 53667662
No related branches found
No related tags found
1 merge request!107fix: keep snake case in openapi schema token authentication
Pipeline #705103 passed
......@@ -224,11 +224,16 @@ SPECTACULAR_SETTINGS = {
],
"CAMELIZE_NAMES": True,
"POSTPROCESSING_HOOKS": [
"drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields",
"activate_account_project.spectacular.camelize_serializer_fields",
"drf_spectacular.hooks.postprocess_schema_enums",
],
}
SPECTACULAR_COMPONENTS_KEEP_SNAKE_CASE = [
"TokenRequest",
"TokenResponse",
]
# Allow all origins to access API.
CORS_URLS_REGEX = r"^.*$"
CORS_ORIGIN_ALLOW_ALL = True
......
from copy import deepcopy
from django.conf import settings
from drf_spectacular.contrib.djangorestframework_camel_case import (
camelize_serializer_fields as _camelize_serializer_fields,
)
def camelize_serializer_fields(result, generator, request, public):
"""
By default, camelize_serializer_fields will camelize all serializer fields. This function does
not know about any parser_classes and renderer_classes that are set on the view, so it will
camelize all serializer fields. This function is a wrapper around the original function that
will camelize all serializer fields, but will keep the schemas of the components in
SPECTACULAR_COMPONENTS_KEEP_SNAKE_CASE in snake case. This is necessary because the OpenAPI
schema definitions are used to generate the client SDKs, and the client SDKs expect the schema
definitions to be in snake case.
"""
snake_case_schemas = []
# Find all schemas that should keep their snake case
for (component_name, component_type), component in generator.registry._components.items():
if (
component_type == "schemas"
and component_name in settings.SPECTACULAR_COMPONENTS_KEEP_SNAKE_CASE
):
snake_case_schemas.append((component, deepcopy(component.schema)))
# Camelize all serializer fields
result = _camelize_serializer_fields(result, generator, request, public)
# Restore the schemas that should keep their snake case
for component, schema in snake_case_schemas:
component.schema = schema
return result
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment