Skip to content

Commit adc73ff

Browse files
committed
Fix set site settings in swagger_ui so it function correctly.
Prior to this fix, the swagger ui for applying site settings was not working.
1 parent 490e6b9 commit adc73ff

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

services/src/main/java/org/fao/geonet/api/site/SiteApi.java

+55-12
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525

2626
import co.elastic.clients.elasticsearch.core.CountRequest;
2727
import co.elastic.clients.elasticsearch.core.CountResponse;
28+
import com.fasterxml.jackson.core.type.TypeReference;
29+
import com.fasterxml.jackson.databind.ObjectMapper;
2830
import io.swagger.v3.oas.annotations.Parameter;
2931
import io.swagger.v3.oas.annotations.media.Content;
32+
import io.swagger.v3.oas.annotations.media.ExampleObject;
3033
import io.swagger.v3.oas.annotations.media.Schema;
3134
import io.swagger.v3.oas.annotations.responses.ApiResponse;
3235
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@@ -88,10 +91,7 @@
8891
import javax.servlet.http.HttpServletRequest;
8992
import javax.servlet.http.HttpSession;
9093
import java.awt.image.BufferedImage;
91-
import java.io.File;
92-
import java.io.IOException;
93-
import java.io.InputStream;
94-
import java.io.OutputStream;
94+
import java.io.*;
9595
import java.nio.file.DirectoryStream;
9696
import java.nio.file.Files;
9797
import java.nio.file.Path;
@@ -387,24 +387,67 @@ public List<Setting> getSettingsDetails(
387387

388388
@io.swagger.v3.oas.annotations.Operation(
389389
summary = "Save settings",
390-
description = "")
391-
@RequestMapping(
390+
description = "Save the provided settings.",
391+
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
392+
description = "Map of settings to be saved",
393+
required = true,
394+
content = {
395+
@Content(
396+
mediaType = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
397+
schema = @Schema(implementation = Map.class),
398+
examples = {
399+
@ExampleObject(
400+
name = "Example setting (application/x-www-form-urlencoded)",
401+
value = "{\n \"additionalProp1\": \"string\",\n \"additionalProp2\": \"string\",\n \"additionalProp3\": \"string\"\n}"
402+
),
403+
@ExampleObject(
404+
name = "Example setting selection manager max records to 1000 (application/x-www-form-urlencoded)",
405+
value = "{\n \"system/selectionmanager/maxrecords\": \"1000\"\n}"
406+
)
407+
}
408+
),
409+
@Content(
410+
mediaType = MediaType.APPLICATION_JSON_VALUE,
411+
schema = @Schema(implementation = Map.class),
412+
examples = {
413+
@ExampleObject(
414+
name = "Example setting (application/json)",
415+
value = "{\n \"additionalProp1\": \"string\",\n \"additionalProp2\": \"string\",\n \"additionalProp3\": \"string\"\n}"
416+
),
417+
@ExampleObject(
418+
name = "Example setting selection manager max records to 1000 (application/json)",
419+
value = "{\n \"system/selectionmanager/maxrecords\": \"1000\"\n}"
420+
)
421+
}
422+
)
423+
}
424+
)
425+
)
426+
@PostMapping(
392427
path = "/settings",
393-
produces = MediaType.APPLICATION_JSON_VALUE,
394-
method = RequestMethod.POST
428+
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE, MediaType.APPLICATION_JSON_VALUE}
395429
)
396430
@PreAuthorize("hasAuthority('Administrator')")
397431
@ResponseStatus(HttpStatus.NO_CONTENT)
398432
@ApiResponses(value = {
399-
@ApiResponse(responseCode = "204", description = "Settings saved.", content = {@Content(schema = @Schema(hidden = true))}),
433+
@ApiResponse(responseCode = "204", description = "Settings saved."),
400434
@ApiResponse(responseCode = "403", description = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_ADMIN)
401435
})
402436
public void saveSettings(
403-
@Parameter(hidden = false)
404-
@RequestParam
405-
Map<String, String> allRequestParams,
437+
// Mark parameter as hidden in open api specification as the Operation requestBody(above) will describe the format to be supplied
438+
// Without this fix, the swagger ui will fail to work correctly.
439+
@Parameter(description = "Map of settings to be saved",
440+
required = true, hidden = true)
441+
@RequestParam Map<String, String> allRequestParams,
406442
HttpServletRequest request
407443
) throws Exception {
444+
//If sent as JSON then the allRequestParams will be empty, and we need to manually load it from the request body
445+
if (MediaType.APPLICATION_JSON_VALUE.equals(request.getContentType()) && allRequestParams.isEmpty()) {
446+
BufferedReader reader = request.getReader();
447+
ObjectMapper mapper = new ObjectMapper();
448+
allRequestParams = mapper.readValue(reader, new TypeReference<Map<String, String>>() {});
449+
}
450+
408451
ApplicationContext applicationContext = ApplicationContextHolder.get();
409452
String currentUuid = settingManager.getSiteId();
410453
String oldSiteName = settingManager.getSiteName();

0 commit comments

Comments
 (0)