Skip to content

Commit

Permalink
fix: map JSONSchema spec naming convention to snake_case when names f…
Browse files Browse the repository at this point in the history
…rom schema_extra are not found
  • Loading branch information
charles-dyfis-net committed Sep 30, 2024
1 parent b187749 commit 0141782
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions litestar/_openapi/schema_generation/schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
from collections import deque
from copy import copy
from datetime import date, datetime, time, timedelta
Expand Down Expand Up @@ -139,6 +140,11 @@
}


def _jsonschema_to_propname(s: str) -> str:
"""Given a string that matches the mixedCase convention used by the JSONSchema spec, convert to snake case as used here"""
return re.sub(r"([a-z])([A-Z])", lambda match: f"{match[1]}_{match[2].lower()}", s)


def _types_in_list(lst: list[Any]) -> list[OpenAPIType] | OpenAPIType:
"""Extract unique OpenAPITypes present in the values of a list.
Expand Down Expand Up @@ -593,9 +599,12 @@ def process_schema_result(self, field: FieldDefinition, schema: Schema) -> Schem
if isinstance(field.kwarg_definition, KwargDefinition) and (extra := field.kwarg_definition.schema_extra):
for schema_key, value in extra.items():
if not hasattr(schema, schema_key):
raise ValueError(
f"`schema_extra` declares key `{schema_key}` which does not exist in `Schema` object"
)
if hasattr(schema, (amended_key := _jsonschema_to_propname(schema_key))):
schema_key = amended_key
else:
raise ValueError(
f"`schema_extra` declares key `{schema_key}` which does not exist in `Schema` object"
)
setattr(schema, schema_key, value)

if schema.default is None and field.default is not Empty:
Expand Down

0 comments on commit 0141782

Please sign in to comment.