-
-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Failing to override Pydantic alias_gnerator config, to convert fields to camelCase in response body #469
Comments
Hi @eldardamari Pydantic's class AddResult(Schema):
total_sum: int
class Config:
alias_generator = to_camel
allow_population_by_field_name = True
@api.get("/add", response=AddResult, by_alias=True) # <--- !!! by_alias=True
def add(request, a: int, b: int):
return {'total_sum': a + b} |
Thanks @vitalik! app_router = Router()
@app_router.get('/data', by_alias=True)
def get_data():
// code
@app_router.get('/')
def get_index():
// code
|
I guess you can extend Router class: class MyRouter(Router):
def api_operation(self, *a, **kw):
kw['by_alias'] = True
return super().api_operation(*a, **kw)
router = MyRouter() |
Is there a similar solution that would change the default value of |
I guess you can try the following - in urls.py(!!!) before include api to urls def set_all_by_alias(api):
for _pth, router in api._routers:
for view in router.path_operations.values():
for op in view.operations:
op.by_alias = True
set_all_by_alias(api)
urlpatterns = [
...
path("api/", api.urls), # <---------- !
] |
@vitalik Thank you for the prompt response, it worked without any changes to your suggestion 🎉 |
@vitalik Thank you for this! Are we able to set a universal alias_generator function as well in the urls.py? Would be great to avoid adding these to Config entries on every Schema: class ProjectSiteSchema(ModelSchema):
class Meta:
model = ProjectSite
fields = "__all__"
class Config:
alias_generator = to_camel
populate_by_name = True |
Hi @eldardamari I assume you use ninja 1.x (pydantic 2.x) The new pydantic changes the place to configure to class ProjectSiteSchema(ModelSchema):
model_config = dict(alias_generator=to_camel, populate_by_name=True)
class Meta:
model = ProjectSite
fields = "__all__" |
@vitalik weirdly, this works for almost everything. Here's the example value from the auto-generated docs: "sites": [
{
"id": 0,
"created_by_id": 0,
"name": "string",
"addressCity": "string",
"addressState": "string",
"addressZip": "string",
"addressStreet1": "string",
"addressStreet2": "string",
"createdAt": "2024-02-12T06:19:10.600Z"
}
], How do I make sure it includes |
It looks like setting the model_config does correctly alias any fields declared on the ModelSchema subclass but it isn't correctly aliasing any fields autogenerated from the Django model.
This is because in This actually presents a small problem, the serialization alias needs to match the attribute's name on the Django model, which is why this alias is generated in the first place. We need two different serialization aliases, which doesn't seem possible so I'm not sure what the alternative is here. Any ideas? EDIT: Idea here |
Actually there's another issue here for this exact problem #828 |
Describe the bug
Failing to apply Pydantic
alias_gnerator
config, to convert fields to camelCase in response body.Expected response:
Docs
Versions (please complete the following information):
The text was updated successfully, but these errors were encountered: