Skip to content

Commit a0c9724

Browse files
author
bnasslahsen
committed
Added support: Example value can be specified without having to specify a schema. Fixes #352
1 parent f93a1fd commit a0c9724

File tree

4 files changed

+106
-19
lines changed

4 files changed

+106
-19
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/RequestBodyBuilder.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.Map;
1414
import java.util.Optional;
1515

16-
@SuppressWarnings("rawtypes")
1716
public class RequestBodyBuilder {
1817

1918
private final AbstractParameterBuilder parameterBuilder;
@@ -109,35 +108,39 @@ private RequestBody buildRequestBody(RequestBody requestBody, Components compone
109108
requestBody.setRequired(parameter.getRequired());
110109
}
111110

112-
if (requestBody.getContent() == null
113-
|| (requestBody.getContent() != null && methodAttributes.isMethodOverloaded())) {
111+
if (requestBody.getContent() == null) {
114112
Schema<?> schema = parameterBuilder.calculateSchema(components, parameterInfo.getParameter(),
115113
parameterInfo.getpName(), requestBodyInfo,
116114
methodAttributes.getJsonViewAnnotationForRequestBody());
117115
buildContent(requestBody, methodAttributes, schema);
116+
} else if (requestBody.getContent() != null && (methodAttributes.isMethodOverloaded() ||
117+
requestBody.getContent().values().stream().anyMatch(mediaType -> mediaType.getSchema() == null))) {
118+
Schema<?> schema = parameterBuilder.calculateSchema(components, parameterInfo.getParameter(),
119+
parameterInfo.getpName(), requestBodyInfo,
120+
methodAttributes.getJsonViewAnnotationForRequestBody());
121+
mergeContent(requestBody, methodAttributes, schema);
118122
}
119123
return requestBody;
120124
}
121125

122-
private void buildContent(RequestBody requestBody, MethodAttributes methodAttributes, Schema<?> schema) {
126+
private void mergeContent(RequestBody requestBody, MethodAttributes methodAttributes, Schema<?> schema) {
123127
Content content = requestBody.getContent();
124-
if (methodAttributes.isMethodOverloaded() && content != null) {
125-
for (String value : methodAttributes.getAllConsumes()) {
126-
setMediaTypeToContent(schema, content, value);
127-
}
128-
} else {
129-
content = new Content();
130-
for (String value : methodAttributes.getAllConsumes()) {
131-
setMediaTypeToContent(schema, content, value);
132-
}
133-
}
134-
requestBody.setContent(content);
128+
buildContent(requestBody, methodAttributes, schema, content);
135129
}
136130

131+
private void buildContent(RequestBody requestBody, MethodAttributes methodAttributes, Schema<?> schema) {
132+
Content content = new Content();
133+
buildContent(requestBody, methodAttributes, schema, content);
134+
}
137135

138-
private void setMediaTypeToContent(Schema schema, Content content, String value) {
139-
io.swagger.v3.oas.models.media.MediaType mediaTypeObject = new io.swagger.v3.oas.models.media.MediaType();
140-
mediaTypeObject.setSchema(schema);
141-
content.addMediaType(value, mediaTypeObject);
136+
private void buildContent(RequestBody requestBody, MethodAttributes methodAttributes, Schema<?> schema, Content content) {
137+
for (String value : methodAttributes.getAllConsumes()) {
138+
io.swagger.v3.oas.models.media.MediaType mediaTypeObject = new io.swagger.v3.oas.models.media.MediaType();
139+
mediaTypeObject.setSchema(schema);
140+
if (content.get(value) != null)
141+
mediaTypeObject.setExample(content.get(value).getExample());
142+
content.addMediaType(value, mediaTypeObject);
143+
}
144+
requestBody.setContent(content);
142145
}
143146
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.org.springdoc.api.app74;
2+
3+
import io.swagger.v3.oas.annotations.Parameter;
4+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
5+
import io.swagger.v3.oas.annotations.media.Content;
6+
import io.swagger.v3.oas.annotations.media.ExampleObject;
7+
import io.swagger.v3.oas.annotations.parameters.RequestBody;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
12+
@RestController
13+
public class HelloController {
14+
15+
@PostMapping("/test")
16+
@RequestBody(
17+
content = @Content(
18+
examples = @ExampleObject(
19+
value = "sample"
20+
)
21+
)
22+
)
23+
public String postMyRequestBody(
24+
String myRequestBody) {
25+
return null;
26+
}
27+
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package test.org.springdoc.api.app74;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import test.org.springdoc.api.AbstractSpringDocTest;
5+
6+
public class SpringDocApp74Test extends AbstractSpringDocTest {
7+
8+
@SpringBootApplication
9+
static class SpringDocTestApp { }
10+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/test": {
15+
"post": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "postMyRequestBody",
20+
"requestBody": {
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"type": "string"
25+
},
26+
"example": "sample"
27+
}
28+
}
29+
},
30+
"responses": {
31+
"200": {
32+
"description": "default response",
33+
"content": {
34+
"*/*": {
35+
"schema": {
36+
"type": "string"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
},
45+
"components": {}
46+
}

0 commit comments

Comments
 (0)