Andrew A
Andrew A

Reputation: 21

How to generate RequestBody of type Map<String, Object> using springdoc for swagger open api 3.0?

For the following code, only Parameter id is getting generated and its completely missing the "Request body" section.

I added type="object" based on https://swagger.io/docs/specification/data-models/dictionaries/ .

public Item addProperties(
    @Parameter(description = "identifier of the item")
        @PathVariable("id") String id,
    @Parameter(
        description = "map of property names and values ", 
        content = @Content(
            schema = @Schema(
                type = "object",
                implementation = Map.class)))
        @RequestBody Map<String, Object> properties) 

Upvotes: 2

Views: 4483

Answers (2)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48610

The Map type appears to be ignored by Open API (although I do not have a solid reference for this claim).

Refer to: GitHub ~ Issues ~ #597 ~ Request Body for Maps not available in Swagger-UI

Here is the workaround that is described from the GitHub issue above.

static {
    SpringDocUtils.getConfig().removeRequestWrapperToIgnore(java.util.Map.class);
}

Place that static block inside your @SpringBootApplication class. That should fix the issue.

@RequestBody Map<String, Object> properties // Should now generate correctly

See also

Upvotes: 2

Andrius
Andrius

Reputation: 41

The @RequestBody type has to be a class. So any of Map's implementations would work (i.e. HashMap).

@RequestBody HashMap<String, Object> properties

Upvotes: 4

Related Questions