Adding Example RequestBody in swagger micronaut

Viewed 678

I generated swagger for micronaut using the instructions provided in https://micronaut-projects.github.io/micronaut-openapi/latest/guide/index.html

So I have a controller method like:

    @Consumes("application/vnd.api+json")
    @Produces("application/vnd.api+json")
    @Post("/{id}/users")
    @RequestBody
    public HttpResponse addAndAssignTarget(@PathVariable("id") Long projectId, @Body @Parameter() JsonNode user) {

I am not using a POJO for adding users for another reason which is out of context for this question. Thus, the generated swagger ui shows {} as example for request body. I would like to change this to something like. How can I do this?

{
    "data" : {
            "type": "projects",
            "attributes": {
                "name": "some-name1",
                "description": "some-description",
                "partner_company": "some-compnay"
            }
    }
}
1 Answers

It looks much better if you use POJO. However, you can add an example as string, but the problem with that if you have any nested objects the quotation marks won't look nice (will be escaped "):

@Consumes("application/vnd.api+json")
@Produces("application/vnd.api+json")
@Post("/{id}/users")
@RequestBody
public HttpResponse addAndAssignTarget(@PathVariable("id") Long projectId,
@Body @RequestBody(content = @Content(schema = @Schema(example = "[0, 2, 3]"))) JsonNode user){

}

Also it is not a Paramater, but a @Body and @RequestBody for annotation purposes.

Output will be:

"[0, 2, 3]"
Related