Skip to content

Commit 58823be

Browse files
committed
Merge branch 'define-content-type-with-request-body-annotation' of github.com:Mattias-Sehlstedt/springdoc-openapi into Mattias-Sehlstedt-define-content-type-with-request-body-annotation
2 parents 4c182de + 66c1d8c commit 58823be

File tree

4 files changed

+261
-4
lines changed

4 files changed

+261
-4
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/RequestBodyService.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,11 @@ public void calculateRequestBodyInfo(Components components, MethodAttributes met
285285
* @param requestBodyInfo the request body info
286286
* @return the request body
287287
*/
288-
private RequestBody buildRequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody requestBodyDoc, Components components,
289-
MethodAttributes methodAttributes,
290-
ParameterInfo parameterInfo, RequestBodyInfo requestBodyInfo) {
288+
private RequestBody buildRequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody requestBodyDoc,
289+
Components components,
290+
MethodAttributes methodAttributes,
291+
ParameterInfo parameterInfo,
292+
RequestBodyInfo requestBodyInfo) {
291293
RequestBody requestBody = requestBodyInfo.getRequestBody();
292294
if (requestBody == null) {
293295
requestBody = new RequestBody();
@@ -300,7 +302,9 @@ private RequestBody buildRequestBody(io.swagger.v3.oas.annotations.parameters.Re
300302
Schema<?> schema = parameterBuilder.calculateSchema(components, parameterInfo, requestBodyInfo,
301303
methodAttributes.getJsonViewAnnotationForRequestBody());
302304
Map<String, Encoding> parameterEncoding = getParameterEncoding(parameterInfo);
303-
buildContent(requestBody, methodAttributes, schema, parameterEncoding);
305+
// If a content type is explicitly stated with a @RequestBody annotation, yield to that content type
306+
if (!methodAttributes.isWithResponseBodySchemaDoc())
307+
buildContent(requestBody, methodAttributes, schema, parameterEncoding);
304308

305309
// Add requestBody javadoc
306310
if (StringUtils.isBlank(requestBody.getDescription()) && parameterBuilder.getJavadocProvider() != null
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * *
7+
* * * * * * Copyright 2019-2025 the original author or authors.
8+
* * * * * *
9+
* * * * * * Licensed under the Apache License, Version 2.0 (the "License");
10+
* * * * * * you may not use this file except in compliance with the License.
11+
* * * * * * You may obtain a copy of the License at
12+
* * * * * *
13+
* * * * * * https://www.apache.org/licenses/LICENSE-2.0
14+
* * * * * *
15+
* * * * * * Unless required by applicable law or agreed to in writing, software
16+
* * * * * * distributed under the License is distributed on an "AS IS" BASIS,
17+
* * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* * * * * * See the License for the specific language governing permissions and
19+
* * * * * * limitations under the License.
20+
* * * * *
21+
* * * *
22+
* * *
23+
* *
24+
*
25+
*/
26+
27+
package test.org.springdoc.api.v30.app244;
28+
29+
import io.swagger.v3.oas.annotations.Operation;
30+
import io.swagger.v3.oas.annotations.media.Content;
31+
import io.swagger.v3.oas.annotations.media.Schema;
32+
import io.swagger.v3.oas.annotations.parameters.RequestBody;
33+
import org.springdoc.core.annotations.ParameterObject;
34+
import org.springframework.http.MediaType;
35+
import org.springframework.web.bind.annotation.ModelAttribute;
36+
import org.springframework.web.bind.annotation.PostMapping;
37+
import org.springframework.web.bind.annotation.RestController;
38+
39+
@RestController
40+
public class HelloController {
41+
42+
public record Greeting(String hi, String bye) {
43+
}
44+
45+
@PostMapping(value = "v1/greet", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
46+
public void endpoint(@RequestBody @ModelAttribute Greeting greeting) {
47+
48+
}
49+
50+
@PostMapping(value = "v2/greet")
51+
public void endpoint2(@ParameterObject @ModelAttribute Greeting greeting) {
52+
53+
}
54+
55+
@PostMapping(value = "v3/greet")
56+
@RequestBody(
57+
content = @Content(
58+
mediaType = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
59+
schema = @Schema(implementation = Greeting.class)
60+
))
61+
public void endpoint3(Greeting greeting) {
62+
63+
}
64+
65+
@PostMapping(value = "v4/greet")
66+
public void endpoint4(
67+
@RequestBody(content = @Content(
68+
mediaType = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
69+
schema = @Schema(implementation = Greeting.class)))
70+
@ModelAttribute Greeting greeting) {
71+
72+
}
73+
74+
@PostMapping(value = "v5/greet")
75+
@Operation(
76+
requestBody = @RequestBody(content = @Content(
77+
mediaType = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
78+
schema = @Schema(implementation = Greeting.class))
79+
))
80+
public void endpoint5(@ModelAttribute Greeting greeting) {
81+
82+
}
83+
84+
}
85+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2025 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app244;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
29+
30+
public class SpringDocApp244Test extends AbstractSpringDocV30Test {
31+
32+
@SpringBootApplication
33+
static class SpringDocTestApp {}
34+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
"openapi" : "3.0.1",
3+
"info" : {
4+
"title" : "OpenAPI definition",
5+
"version" : "v0"
6+
},
7+
"servers" : [ {
8+
"url" : "http://localhost",
9+
"description" : "Generated server url"
10+
} ],
11+
"paths" : {
12+
"/v5/greet" : {
13+
"post" : {
14+
"tags" : [ "hello-controller" ],
15+
"operationId" : "endpoint5",
16+
"requestBody" : {
17+
"content" : {
18+
"application/x-www-form-urlencoded" : {
19+
"schema" : {
20+
"$ref" : "#/components/schemas/Greeting"
21+
}
22+
}
23+
}
24+
},
25+
"responses" : {
26+
"200" : {
27+
"description" : "OK"
28+
}
29+
}
30+
}
31+
},
32+
"/v4/greet" : {
33+
"post" : {
34+
"tags" : [ "hello-controller" ],
35+
"operationId" : "endpoint4",
36+
"requestBody" : {
37+
"content" : {
38+
"application/x-www-form-urlencoded" : {
39+
"schema" : {
40+
"$ref" : "#/components/schemas/Greeting"
41+
}
42+
}
43+
}
44+
},
45+
"responses" : {
46+
"200" : {
47+
"description" : "OK"
48+
}
49+
}
50+
}
51+
},
52+
"/v3/greet" : {
53+
"post" : {
54+
"tags" : [ "hello-controller" ],
55+
"operationId" : "endpoint3",
56+
"requestBody" : {
57+
"content" : {
58+
"application/x-www-form-urlencoded" : {
59+
"schema" : {
60+
"$ref" : "#/components/schemas/Greeting"
61+
}
62+
}
63+
}
64+
},
65+
"responses" : {
66+
"200" : {
67+
"description" : "OK"
68+
}
69+
}
70+
}
71+
},
72+
"/v2/greet" : {
73+
"post" : {
74+
"tags" : [ "hello-controller" ],
75+
"operationId" : "endpoint2",
76+
"parameters" : [ {
77+
"name" : "hi",
78+
"in" : "query",
79+
"required" : false,
80+
"schema" : {
81+
"type" : "string"
82+
}
83+
}, {
84+
"name" : "bye",
85+
"in" : "query",
86+
"required" : false,
87+
"schema" : {
88+
"type" : "string"
89+
}
90+
} ],
91+
"responses" : {
92+
"200" : {
93+
"description" : "OK"
94+
}
95+
}
96+
}
97+
},
98+
"/v1/greet" : {
99+
"post" : {
100+
"tags" : [ "hello-controller" ],
101+
"operationId" : "endpoint",
102+
"requestBody" : {
103+
"content" : {
104+
"application/x-www-form-urlencoded" : {
105+
"schema" : {
106+
"$ref" : "#/components/schemas/Greeting"
107+
}
108+
}
109+
}
110+
},
111+
"responses" : {
112+
"200" : {
113+
"description" : "OK"
114+
}
115+
}
116+
}
117+
}
118+
},
119+
"components" : {
120+
"schemas" : {
121+
"Greeting" : {
122+
"type" : "object",
123+
"properties" : {
124+
"hi" : {
125+
"type" : "string"
126+
},
127+
"bye" : {
128+
"type" : "string"
129+
}
130+
}
131+
}
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)