add @Valid

This commit is contained in:
Anthony Stirling 2025-07-21 16:17:49 +01:00
parent 40d2a9015c
commit af4e20c971
3 changed files with 16 additions and 12 deletions

View File

@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils; import org.springframework.web.util.HtmlUtils;
import jakarta.validation.Valid;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.responses.ApiResponses;
@ -76,12 +78,9 @@ public class AdminSettingsController {
responseCode = "500", responseCode = "500",
description = "Failed to save settings to configuration file") description = "Failed to save settings to configuration file")
}) })
public ResponseEntity<String> updateSettings(@RequestBody UpdateSettingsRequest request) { public ResponseEntity<String> updateSettings(@Valid @RequestBody UpdateSettingsRequest request) {
try { try {
Map<String, Object> settings = request.getSettings(); Map<String, Object> settings = request.getSettings();
if (settings == null || settings.isEmpty()) {
return ResponseEntity.badRequest().body("No settings provided to update");
}
int updatedCount = 0; int updatedCount = 0;
for (Map.Entry<String, Object> entry : settings.entrySet()) { for (Map.Entry<String, Object> entry : settings.entrySet()) {
@ -160,7 +159,7 @@ public class AdminSettingsController {
@ApiResponse(responseCode = "500", description = "Failed to save settings") @ApiResponse(responseCode = "500", description = "Failed to save settings")
}) })
public ResponseEntity<String> updateSettingsSection( public ResponseEntity<String> updateSettingsSection(
@PathVariable String sectionName, @RequestBody Map<String, Object> sectionData) { @PathVariable String sectionName, @Valid @RequestBody Map<String, Object> sectionData) {
try { try {
if (sectionData == null || sectionData.isEmpty()) { if (sectionData == null || sectionData.isEmpty()) {
return ResponseEntity.badRequest().body("No section data provided to update"); return ResponseEntity.badRequest().body("No section data provided to update");
@ -246,12 +245,8 @@ public class AdminSettingsController {
@ApiResponse(responseCode = "500", description = "Failed to save setting") @ApiResponse(responseCode = "500", description = "Failed to save setting")
}) })
public ResponseEntity<String> updateSettingValue( public ResponseEntity<String> updateSettingValue(
@PathVariable String key, @RequestBody UpdateSettingValueRequest request) { @PathVariable String key, @Valid @RequestBody UpdateSettingValueRequest request) {
try { try {
if (request.getValue() == null) {
return ResponseEntity.badRequest().body("Request body must contain 'value' field");
}
Object value = request.getValue(); Object value = request.getValue();
log.info("Admin updating single setting: {} = {}", key, value); log.info("Admin updating single setting: {} = {}", key, value);
GeneralUtils.saveKeyToSettings(key, value); GeneralUtils.saveKeyToSettings(key, value);

View File

@ -1,5 +1,7 @@
package stirling.software.proprietary.security.model.api.admin; package stirling.software.proprietary.security.model.api.admin;
import jakarta.validation.constraints.NotNull;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@ -8,6 +10,7 @@ import lombok.Data;
@Schema(description = "Request object for updating a single setting value") @Schema(description = "Request object for updating a single setting value")
public class UpdateSettingValueRequest { public class UpdateSettingValueRequest {
@Schema(description = "The new value for the setting", example = "true") @NotNull(message = "Setting value cannot be null")
@Schema(description = "The new value for the setting", example = "true", required = true)
private Object value; private Object value;
} }

View File

@ -2,6 +2,9 @@ package stirling.software.proprietary.security.model.api.admin;
import java.util.Map; import java.util.Map;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@ -12,6 +15,8 @@ import lombok.Data;
"Request object for delta updates to application settings. Only include the settings you want to change. Uses dot notation for nested properties (e.g., 'system.enableAnalytics', 'ui.appName')") "Request object for delta updates to application settings. Only include the settings you want to change. Uses dot notation for nested properties (e.g., 'system.enableAnalytics', 'ui.appName')")
public class UpdateSettingsRequest { public class UpdateSettingsRequest {
@NotNull(message = "Settings map cannot be null")
@NotEmpty(message = "Settings map cannot be empty")
@Schema( @Schema(
description = description =
"Map of setting keys to their new values. Only include changed settings (delta updates). Keys use dot notation for nested properties.", "Map of setting keys to their new values. Only include changed settings (delta updates). Keys use dot notation for nested properties.",
@ -20,6 +25,7 @@ public class UpdateSettingsRequest {
+ " \"system.enableAnalytics\": true,\n" + " \"system.enableAnalytics\": true,\n"
+ " \"ui.appName\": \"My Custom PDF Tool\",\n" + " \"ui.appName\": \"My Custom PDF Tool\",\n"
+ " \"security.enableLogin\": false\n" + " \"security.enableLogin\": false\n"
+ "}") + "}",
required = true)
private Map<String, Object> settings; private Map<String, Object> settings;
} }