Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Example RequestBody in swagger micronaut

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"
            }
    }
}
like image 785
Rajshree Rai Avatar asked Jun 23 '26 17:06

Rajshree Rai


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]"
like image 86
saganas Avatar answered Jun 25 '26 22:06

saganas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!