Skip to content

Commit 2bdfb14

Browse files
committed
refactor: drops dependency for camel case conversion
note that in pyndatic2 `populate_by_name` is what makes the `alias_generator` behave, if we use the `from_attributes` it makes pydantic look up the aliased key in the source object resulting in a validation error
1 parent 5e9a4f9 commit 2bdfb14

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/labs/schema/utils.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
from datetime import datetime
66

77
from pydantic import BaseModel, ConfigDict
8-
from humps import camelize
8+
9+
10+
def to_lower_camel(name: str) -> str:
11+
"""
12+
Converts a snake_case string to lowerCamelCase
13+
"""
14+
upper = "".join(word.capitalize() for word in name.split("_"))
15+
return upper[:1].lower() + upper[1:]
916

1017

1118
class AppBaseModel(BaseModel):
@@ -16,15 +23,16 @@ class AppBaseModel(BaseModel):
1623
translate between camcelCase and snake_case for the JSON
1724
amongst other default settings.
1825
19-
from_attributes will allow pydantic to translate SQLAlchemy results
20-
into serializable models.
26+
populate_by_name will allow pydantic to translate SQLAlchemy
27+
results into serializable models, while being able to translate
28+
the aliases back to the original names when serializing to JSON.
2129
2230
For a full set of options, see:
2331
https://pydantic-docs.helpmanual.io/usage/model_config/
2432
"""
2533
model_config = ConfigDict(
26-
from_attributes=True,
27-
alias_generator=camelize,
34+
populate_by_name=True,
35+
alias_generator=to_lower_camel,
2836
)
2937

3038

0 commit comments

Comments
 (0)