Skip to content

Commit

Permalink
Fix serialized name handling in validate_request decorator
Browse files Browse the repository at this point in the history
---------------------------------------
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.
  • Loading branch information
Aadesh-Baral committed Jun 27, 2023
1 parent 53c49f2 commit a7c85a0
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions backend/models/dtos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,24 @@ def wrapper(*args, **kwargs):
try:
dto = dto_class()

# Set attribute values from request body, query parameters, and path parameters
for attr in dto.__class__._fields:
if request.is_json and attr in request.json:
setattr(dto, attr, request.json[attr])
elif attr in request.args:
setattr(dto, attr, request.args.get(attr))
elif attr in kwargs:
setattr(dto, attr, kwargs[attr])
# Get serialized name of attr if exists otherwise use attr name
field = dto.__class__._fields[attr]
attr_name = field.serialized_name if field.serialized_name else attr

# Set attribute value from request body, query parameters, or path parameters
if request.is_json and attr_name in request.json:
setattr(dto, attr, request.json[attr_name])
elif attr_name in request.args:
setattr(dto, attr, request.args.get(attr_name))
elif attr_name in kwargs:
setattr(dto, attr, kwargs[attr_name])

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

# Get accepted languages from request header
# Get accepted language from request header
if "preferred_locale" in dto.__class__._fields:
dto.preferred_locale = request.headers.get("accept-language", "en")

Expand Down

0 comments on commit a7c85a0

Please sign in to comment.