Skip to content

Commit b0fa9b4

Browse files
committed
OpenAPI: requestbody from MVC
1 parent 902bc7e commit b0fa9b4

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

modules/jooby-openapi/src/main/java/io/jooby/internal/openapi/AnnotationParser.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.Objects;
3535
import java.util.Optional;
3636
import java.util.Set;
37+
import java.util.concurrent.atomic.AtomicReference;
38+
import java.util.function.Consumer;
3739
import java.util.stream.Collectors;
3840
import java.util.stream.Stream;
3941

@@ -201,16 +203,23 @@ private static List<OperationExt> routerMethod(ParserContext ctx, String prefix,
201203
ClassNode classNode, MethodNode method) {
202204
List<OperationExt> result = new ArrayList<>();
203205

204-
List<ParameterExt> arguments = routerArguments(method);
206+
AtomicReference<RequestBodyExt> requestBody = new AtomicReference<>();
207+
List<ParameterExt> arguments = routerArguments(method, requestBody::set);
205208
List<ResponseExt> returnTypes = returnTypes(method);
206209

207210
for (String httpMethod : httpMethod(method.visibleAnnotations)) {
208211
for (String pattern : httpPattern(classNode, method, httpMethod)) {
209-
OperationExt operation = new OperationExt(method, httpMethod, RoutePath.path(prefix, pattern),
212+
OperationExt operation = new OperationExt(
213+
method,
214+
httpMethod,
215+
RoutePath.path(prefix, pattern),
210216
arguments,
211-
returnTypes);
217+
returnTypes
218+
);
212219
operation.setOperationId(method.name);
213220
operation.setDeprecated(isDeprecated(method.visibleAnnotations));
221+
Optional.ofNullable(requestBody.get()).ifPresent(operation::setRequestBody);
222+
214223
result.add(operation);
215224
}
216225
}
@@ -246,7 +255,8 @@ private static List<ResponseExt> returnTypes(MethodNode method) {
246255
return result;
247256
}
248257

249-
private static List<ParameterExt> routerArguments(MethodNode method) {
258+
private static List<ParameterExt> routerArguments(MethodNode method,
259+
Consumer<RequestBodyExt> requestBody) {
250260
List<ParameterExt> result = new ArrayList<>();
251261
if (method.parameters != null) {
252262
for (int i = 0; i < method.parameters.size(); i++) {
@@ -288,13 +298,19 @@ private static List<ParameterExt> routerArguments(MethodNode method) {
288298
? true
289299
: !isNullable(method, i);//!javaType.startsWith("java.util.Optional");
290300

291-
ParameterExt argument = new ParameterExt();
292-
argument.setName(paramType.getHttpName(annotations).orElse(parameter.name));
293-
argument.setJavaType(javaType);
294-
paramType.setIn(argument);
295-
argument.setRequired(required);
296-
297-
result.add(argument);
301+
if (paramType == ParamType.BODY) {
302+
RequestBodyExt body = new RequestBodyExt();
303+
body.setRequired(required);
304+
body.setJavaType(javaType);
305+
requestBody.accept(body);
306+
} else {
307+
ParameterExt argument = new ParameterExt();
308+
argument.setName(paramType.getHttpName(annotations).orElse(parameter.name));
309+
argument.setJavaType(javaType);
310+
paramType.setIn(argument);
311+
argument.setRequired(required);
312+
result.add(argument);
313+
}
298314
}
299315
}
300316
return result;

modules/jooby-openapi/src/test/java/examples/ControllerExample.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public String ifSession(Optional<Session> ifSession) {
4444
public List<String> returnList() {
4545
return null;
4646
}
47+
48+
@POST("/bean")
49+
public ABean save(ABean bean) {
50+
return bean;
51+
}
4752
}

modules/jooby-openapi/src/test/java/io/jooby/openapi/OpenApiToolTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,11 @@ public void routeMvc(RouteIterator iterator) {
11101110
route.getResponse().toString());
11111111
assertTrue(route.getDeprecated());
11121112
})
1113+
.next(route -> {
1114+
assertEquals("POST /api/bean", route.toString());
1115+
assertEquals(ABean.class.getName(), route.getResponse().toString());
1116+
assertEquals(ABean.class.getName(), route.getRequestBody().getJavaType());
1117+
})
11131118
.verify();
11141119
}
11151120

0 commit comments

Comments
 (0)