Skip to content
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

Apply decorator to validate request on route functions. #5947

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Aadesh-Baral
Copy link
Contributor

@Aadesh-Baral Aadesh-Baral commented Jun 27, 2023

Part 2 of #5910

---------------------------------------
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.
-----------------------------------------------
This commit refactors the request body handling in the validate_request decorator. Previously, the code attempted to access request.json directly, which could raise a "Failed to decode JSON object" error when the request body was empty or contained invalid JSON.

To address this issue, the code has been updated to use a try-except block. It now checks request.is_json and handles two scenarios:

- If request.is_json is True, indicating a JSON content type, it tries to access request.json to retrieve the JSON payload. If the request body contains invalid JSON, the WerkzeugBadRequest exception is caught, and the body is set to an empty dictionary.

- If request.is_json is False or request.json raises an error for any reason, body is also set to an empty dictionary.
@Aadesh-Baral Aadesh-Baral force-pushed the enhance/5910-request-validation branch from a7c85a0 to 21558f1 Compare June 28, 2023 05:29
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Aadesh-Baral Aadesh-Baral self-assigned this Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant