Open
Description
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 bothapplication/json
andapplication/xml
as possible response media types. I expected it to only listapplication/xml
, since it's explicitly declared at the method level.
Additional context
- Behavior was probably introduced by Fix consumes and produces calculation. Fixes #2596 #2600
Metadata
Metadata
Assignees
Labels
No labels