Forum Discussion

thomasgigout's avatar
11 months ago

Try it out with a String

Hi there,

 

How to reproduce the issue:

 

The annotations come from the package "io.swagger.v3.*"

I have an Dropwizard API written in Java and I use the Maven package "dropwizard-swagger" from "com.smoketurner". So I have a simple POST request which takes a String as an option. The values are only "start" or "stop". Here is the function with its annotations :

 

@POST
@Timed
@Operation(
summary = "Turn off/on processing",
requestBody = @RequestBody(
description = "Processing state (off / on)",
required = true,
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = String.class
),
examples = {
@ExampleObject(
name = "start",
value = "start"
),
@ExampleObject(
name = "stop",
value = "stop"
)
}
)
)
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "Success response",
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = Boolean.class)
)
),
@ApiResponse(
responseCode = "400",
description = "Bad request",
content = @Content(mediaType = MediaType.APPLICATION_JSON)
)
}
)
public boolean switchProcessing(String state) {
boolean ret;
switch (state) {
case "start" -> {
// do something
ret = true;
}
case "stop" -> {
// do something else
ret = true;
}
default -> ret = false;
}
return ret;
}

It produces this try it out :

and this cURL :

 

curl -X 'POST' \ 'http://localhost:15000/api/procedure-history' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '"start"'

 

As you can see, there is quotes around the value. With this, the program don't recognize the String "start" so it doesn't enter in the correct switch case. If I remove the double quotes in the try it out, it produces this cURL and works as expected.

 

curl -X 'POST' \ 'http://localhost:15000/api/procedure-history' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d 'start'​

 

Do you have any idea on how I can fix it?

 

No RepliesBe the first to reply