2

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) 
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Andrew A
  • 21
  • 1
  • 2
  • Using `Map` should be fine from a Spring/Jackson standpoint. Making a e.g. [fetch (PATCH)](https://postsrc.com/code-snippets/how-to-make-patch-request-with-fetch-api) request should be legal if the `@RequestBody` is a `Map`. I think it's either a bug in Swagger/Open API 3, or a design choice. Although I cannot find any references to back up these claims. – Mr. Polywhirl Jan 31 '23 at 20:15

2 Answers2

4

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

@RequestBody HashMap<String, Object> properties
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Andrius
  • 41
  • 2
  • Is this a Swagger/Open API limitation? A Spring REST controller that takes a `Map` works just fine from a cURL/XHR standpoint. See: ["The @RequestBody Annotation" ~ _Spring Framework Guru_](https://springframework.guru/requestbody-annotation/) – Mr. Polywhirl Jan 31 '23 at 19:56
1

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

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132