Skip to content

Commit 2a1ab0c

Browse files
committed
Fix serialized name handling in validate_request decorator
--------------------------------------- The previous implementation of the validate_request decorator did not correctly handle the serialized names of fields in the DTO class when populating attribute values from the request. This commit addresses the issue by updating the decorator to access the serialized names directly from the serialized_name attribute of each field. The updated implementation iterates over the fields in the DTO class and checks for the presence of the serialized names in the request body. If a matching serialized name is found, the corresponding value is assigned to the DTO attribute. This ensures that fields with serialized names, specified using the serialized_name parameter in the field definitions, are properly set.
1 parent 281023a commit 2a1ab0c

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

backend/models/dtos/__init__.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ def wrapper(*args, **kwargs):
3333
try:
3434
dto = dto_class()
3535

36-
# Set attribute values from request body, query parameters, and path parameters
3736
for attr in dto.__class__._fields:
38-
if request.is_json and attr in request.json:
39-
setattr(dto, attr, request.json[attr])
40-
elif attr in request.args:
41-
setattr(dto, attr, request.args.get(attr))
42-
elif attr in kwargs:
43-
setattr(dto, attr, kwargs[attr])
37+
# Get serialized name of attr if exists otherwise use attr name
38+
field = dto.__class__._fields[attr]
39+
attr_name = field.serialized_name if field.serialized_name else attr
40+
41+
# Set attribute value from request body, query parameters, or path parameters
42+
if request.is_json and attr_name in request.json:
43+
setattr(dto, attr, request.json[attr_name])
44+
elif attr_name in request.args:
45+
setattr(dto, attr, request.args.get(attr_name))
46+
elif attr_name in kwargs:
47+
setattr(dto, attr, kwargs[attr_name])
4448

4549
# Set authenticated user id if user_id is a field in the DTO
4650
if "user_id" in dto.__class__._fields:
4751
dto.user_id = token_auth.current_user()
4852

49-
# Get accepted languages from request header
53+
# Get accepted language from request header
5054
if "preferred_locale" in dto.__class__._fields:
5155
dto.preferred_locale = request.environ.get(
5256
"HTTP_ACCEPT_LANGUAGE", "en"

0 commit comments

Comments
 (0)