23
23
24
24
package org .fao .geonet .api .site ;
25
25
26
+ import com .fasterxml .jackson .core .type .TypeReference ;
27
+ import com .fasterxml .jackson .databind .ObjectMapper ;
26
28
import io .swagger .v3 .oas .annotations .Parameter ;
29
+ import io .swagger .v3 .oas .annotations .media .Content ;
30
+ import io .swagger .v3 .oas .annotations .media .ExampleObject ;
31
+ import io .swagger .v3 .oas .annotations .media .Schema ;
27
32
import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
28
33
import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
29
34
import io .swagger .v3 .oas .annotations .tags .Tag ;
90
95
import javax .servlet .http .HttpServletRequest ;
91
96
import javax .servlet .http .HttpSession ;
92
97
import java .awt .image .BufferedImage ;
93
- import java .io .File ;
94
- import java .io .IOException ;
95
- import java .io .InputStream ;
96
- import java .io .OutputStream ;
98
+ import java .io .*;
97
99
import java .nio .file .DirectoryStream ;
98
100
import java .nio .file .Files ;
99
101
import java .nio .file .Path ;
@@ -391,11 +393,45 @@ public List<Setting> getSettingsDetails(
391
393
392
394
@ io .swagger .v3 .oas .annotations .Operation (
393
395
summary = "Save settings" ,
394
- description = "" )
395
- @ RequestMapping (
396
+ description = "Save the provided settings." ,
397
+ requestBody = @ io .swagger .v3 .oas .annotations .parameters .RequestBody (
398
+ description = "Map of settings to be saved" ,
399
+ required = true ,
400
+ content = {
401
+ @ Content (
402
+ mediaType = MediaType .APPLICATION_FORM_URLENCODED_VALUE ,
403
+ schema = @ Schema (implementation = Map .class ),
404
+ examples = {
405
+ @ ExampleObject (
406
+ name = "Example setting (application/x-www-form-urlencoded)" ,
407
+ value = "{\n \" additionalProp1\" : \" string\" ,\n \" additionalProp2\" : \" string\" ,\n \" additionalProp3\" : \" string\" \n }"
408
+ ),
409
+ @ ExampleObject (
410
+ name = "Example setting selection manager max records to 1000 (application/x-www-form-urlencoded)" ,
411
+ value = "{\n \" system/selectionmanager/maxrecords\" : \" 1000\" \n }"
412
+ )
413
+ }
414
+ ),
415
+ @ Content (
416
+ mediaType = MediaType .APPLICATION_JSON_VALUE ,
417
+ schema = @ Schema (implementation = Map .class ),
418
+ examples = {
419
+ @ ExampleObject (
420
+ name = "Example setting (application/json)" ,
421
+ value = "{\n \" additionalProp1\" : \" string\" ,\n \" additionalProp2\" : \" string\" ,\n \" additionalProp3\" : \" string\" \n }"
422
+ ),
423
+ @ ExampleObject (
424
+ name = "Example setting selection manager max records to 1000 (application/json)" ,
425
+ value = "{\n \" system/selectionmanager/maxrecords\" : \" 1000\" \n }"
426
+ )
427
+ }
428
+ )
429
+ }
430
+ )
431
+ )
432
+ @ PostMapping (
396
433
path = "/settings" ,
397
- produces = MediaType .APPLICATION_JSON_VALUE ,
398
- method = RequestMethod .POST
434
+ consumes = {MediaType .APPLICATION_FORM_URLENCODED_VALUE , MediaType .APPLICATION_JSON_VALUE }
399
435
)
400
436
@ PreAuthorize ("hasAuthority('Administrator')" )
401
437
@ ResponseStatus (HttpStatus .NO_CONTENT )
@@ -404,11 +440,20 @@ public List<Setting> getSettingsDetails(
404
440
@ ApiResponse (responseCode = "403" , description = ApiParams .API_RESPONSE_NOT_ALLOWED_ONLY_ADMIN )
405
441
})
406
442
public void saveSettings (
407
- @ Parameter (hidden = false )
408
- @ RequestParam
409
- Map <String , String > allRequestParams ,
443
+ // Mark parameter as hidden in open api specification as the Operation requestBody(above) will describe the format to be supplied
444
+ // Without this fix, the swagger ui will fail to work correctly.
445
+ @ Parameter (description = "Map of settings to be saved" ,
446
+ required = true , hidden = true )
447
+ @ RequestParam Map <String , String > allRequestParams ,
410
448
HttpServletRequest request
411
449
) throws Exception {
450
+ //If sent as JSON then the allRequestParams will be empty, and we need to manually load it from the request body
451
+ if (MediaType .APPLICATION_JSON_VALUE .equals (request .getContentType ()) && allRequestParams .isEmpty ()) {
452
+ BufferedReader reader = request .getReader ();
453
+ ObjectMapper mapper = new ObjectMapper ();
454
+ allRequestParams = mapper .readValue (reader , new TypeReference <Map <String , String >>() {});
455
+ }
456
+
412
457
ApplicationContext applicationContext = ApplicationContextHolder .get ();
413
458
String currentUuid = settingManager .getSiteId ();
414
459
String oldSiteName = settingManager .getSiteName ();
0 commit comments