Skip to content

Unexpected merging of media types #3023

Open
@nicklasholm

Description

@nicklasholm

Describe the bug

According to the Javadoc for org.springframework.web.bind.annotation.RequestMapping#produces, method-level produces should override class-level declarations.

Supported at the type level as well as at the method level! If specified at both levels, the method level produces condition overrides the type level condition.

However, when both are used, the springdoc generated OpenAPI spec merges them.

To Reproduce
Steps to reproduce the behavior:

  • Spring Boot 3.5.0
  • springdoc-openapi-starter-webmvc-ui 2.8.9

Example controller:

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public class MediaTypeDemoController {

    @GetMapping("/json")
    public ResponseEntity<Map<String, String>> getJsonResponse() {
        Map<String, String> response = Map.of("message", "This is JSON");
        return ResponseEntity.ok(response);
    }

    @GetMapping(value = "/xml", produces = MediaType.APPLICATION_XML_VALUE)
    public ResponseEntity<String> getXmlResponse() {
        String xml = "<message>This is XML</message>";
        return ResponseEntity.ok(xml);
    }
}

Generated spec:

{
  openapi: "3.1.0",
  info: {
    title: "OpenAPI definition",
    version: "v0"
  },
  servers: [
    {
      url: "http://localhost:8080/",
      description: "Generated server url"
    }
  ],
  paths: {
    /xml: {
      get: {
        tags: [
          "media-type-demo-controller"
        ],
        operationId: "getXmlResponse",
        responses: {
          200: {
            description: "OK",
            content: {
              application/xml: {
                schema: {
                  type: "string"
                }
              },
              application/json: {
                schema: {
                  type: "string"
                }
              }
            }
          }
        }
      }
    },
    /json: {
      get: {
        tags: [
          "media-type-demo-controller"
        ],
        operationId: "getJsonResponse",
        responses: {
          200: {
            description: "OK",
            content: {
              application/json: {
                schema: {
                  type: "object",
                  additionalProperties: {
                    type: "string"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  components: { }
}

Expected behavior

  • The OpenAPI spec generated for GET /xml includes both application/json and application/xml as possible response media types. I expected it to only list application/xml, since it's explicitly declared at the method level.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions