From 523240554f29068c2b70a1738951e735ecb031a4 Mon Sep 17 00:00:00 2001 From: Ludy Date: Tue, 20 May 2025 08:58:27 +0200 Subject: [PATCH] Fix empty-parameter issue in `updateUserSettings` by using `@RequestBody` map (#3536) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description of Changes Please provide a summary of the changes, including: - **What was changed:** - Refactored the `updateUserSettings` method in `UserController` to accept a `@RequestBody Map` named `updates` instead of pulling parameters from `HttpServletRequest`. - Removed the now-unused `HashMap` import and the manual parameter-extraction loop. - **Why the change was made:** - **Bug Fix:** The previous implementation relied on `request.getParameterMap()`, which was consistently empty, so no settings were ever applied. - Simplifies controller logic by leveraging Spring’s request-body binding. - Improves readability and maintainability, removing boilerplate and error-prone code. --- ## Checklist ### General - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [x] I have performed a self-review of my own code - [x] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../SPDF/controller/api/UserController.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/api/UserController.java b/src/main/java/stirling/software/SPDF/controller/api/UserController.java index ce4770499..aa4ae9a00 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/UserController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/UserController.java @@ -3,7 +3,6 @@ package stirling.software.SPDF.controller.api; import java.io.IOException; import java.security.Principal; import java.sql.SQLException; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -168,13 +167,23 @@ public class UserController { @PreAuthorize("!hasAuthority('ROLE_DEMO_USER')") @PostMapping("/updateUserSettings") - public String updateUserSettings(HttpServletRequest request, Principal principal) + /** + * Updates the user settings based on the provided JSON payload. + * + * @param updates A map containing the settings to update. The expected structure is: + * + * Keys not listed above will be ignored. + * @param principal The currently authenticated user. + * @return A redirect string to the account page after updating the settings. + * @throws SQLException If a database error occurs. + * @throws UnsupportedProviderException If the operation is not supported for the user's provider. + */ + public String updateUserSettings(@RequestBody Map updates, Principal principal) throws SQLException, UnsupportedProviderException { - Map paramMap = request.getParameterMap(); - Map updates = new HashMap<>(); - for (Map.Entry entry : paramMap.entrySet()) { - updates.put(entry.getKey(), entry.getValue()[0]); - } log.debug("Processed updates: {}", updates); // Assuming you have a method in userService to update the settings for a user userService.updateUserSettings(principal.getName(), updates);