swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

swagger-ui does not remove java string escape char "\" from @ExampleObject value if name and summary are missing

Open MOUDDENEHamza opened this issue 1 year ago • 0 comments

I am using version 2.2.19 of swagger-ui and spring-boot 3.0.5.

I have defined a value in @ExampleObject of @io.swagger.v3.oas.annotations.parameters.RequestBody like this:

@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(examples = {
    @ExampleObject(
        value = "[\n" +
                    "\t{\n" +
                    "\t\t\"date\": \"2023-11-17\",\n" +
                    "\t\t\"identifier\": \"identifier\"" +
                    "\t}\n" +
                    "]"
        )}
    )
)

The escape chars \ are kept in swagger-ui's Example Value, so that when clicking on it to paste the Example Value into body param value for testing the REST endpoint, the escape chars \ are still included - thus transmitting invalid JSON to the endpoint.

Same goes for things like \n or \t, which are quite common in Java Strings.

The generated swagger.json file contains the following:

"example value": "[\n\t{\n\t\t\"date\": \"2023-11-17\",\n\t\t\"identifier\": \"identifier\"\t}\n]"

But when I edit @ExampleObject like this:

@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(examples = {
    @ExampleObject(
        name = "name sample",
        summary = "summary example",
        value = "[\n" +
                    "\t{\n" +
                    "\t\t\"date\": \"2023-11-17\",\n" +
                    "\t\t\"identifier\": \"identifier\"" +
                    "\t}\n" +
                    "]"
        )}
    )
)

The problem is resolved!!!

MOUDDENEHamza avatar Jan 04 '24 12:01 MOUDDENEHamza