3

I have the below api for which I need to have two parameters of content type application/x-www-form-urlencoded and therefore am using @RequestBody instead of @Parameter


    @Operation(summary = "Revoke given permissions", description = "Allows admin to revoke permissions to users")
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void revokePermission(
            @RequestBody(description = "the permission id", content = @Content(mediaType = "application/x-www-form-urlencoded",
                    schema = { @Schema(type = "String", name = "permission_id",
                                    description = "id of the permission to be revoked", required = true)},
                            { @Schema(type = "String", name = "permission_type", 
                                    description = "the permission type")})) 
                    String permission_id, String permissionType) {

        do_something();
     }

I need the swagger.json to be like below example, but I do not know how to generate it using springdoc .I tried @ArraySchema also , but I am not getting the output I need. I am making some mistakes in the syntax and not able to find examples online.

"requestBody": {
    "content": {
      "application/x-www-form-urlencoded": {
        "schema": {
           "properties": {
              "permission_id": { 
                "description": "id of the permission to be revoked",
                "type": "string"
              },
              "permission_type": {
                "description": "the permission type",
                "type": "string"
             }
           },
        "required": ["permission_id"] 
        }
      }
    }
  }

Any help is highly appreciated. TIA

jacob-cheng
  • 31
  • 1
  • 2

1 Answers1

2

The The simplest way to achieve what you want is to define the permission data in simple object as follow:

@Schema(name = "permissionData")
public class PermissionData {

    @Schema(type = "String", name = "permiddionId", description = "id of the permission to be revoked", required = true)
    @JsonProperty("permiddionId")
    String permiddionId;

    @Schema(type = "String", name = "permissionType",description = "the permission type")
    @JsonProperty("permissionType")
    String permissionType;
}

And then you controller method:

@Operation(summary = "Revoke given permissions", description = "Allows admin to revoke permissions to users")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void revokePermission(@RequestBody(description = "the permission data") PermissionData permissionData) {

}
  • 1
    Thank you. But is there no way to achieve this without creating an object for permission ? I would like them to be separate variables – jacob-cheng Feb 04 '20 at 00:05