@@ -85,20 +85,43 @@ void write(boolean requestScoped) {
85
85
}
86
86
87
87
private void writeContextReturn () {
88
+ // Support for CompletableFuture's.
89
+ final UType type = UType .parse (method .returnType ());
90
+ if (type .isGeneric () && type .mainType ().equals ("java.util.concurrent.CompletableFuture" )) {
91
+ final String returnVariableName = "futureResult" ;
92
+
93
+ writer .append (" ctx.future(() -> {" ).eol ();
94
+ writer .append (" return result.thenAccept(%s -> {" , returnVariableName ).eol ();
95
+ writer .append (" " );
96
+ this .writeContextReturn (returnVariableName );
97
+ writer .eol ().append (" });" ).eol ();
98
+ writer .append (" });" ).eol ();
99
+ return ;
100
+ }
101
+
102
+ // Everything else
103
+ this .writeContextReturn ("result" );
104
+ }
105
+
106
+ private void writeContextReturn (final String returnVariableName ) {
88
107
final var produces = method .produces ();
89
108
if (produces == null || MediaType .APPLICATION_JSON .equalsIgnoreCase (produces )) {
90
109
if (useJsonB ) {
91
- final var uType = UType .parse (method .returnType ());
92
- writer .append (" %sJsonType.toJson(result, ctx.contentType(\" application/json\" ).outputStream());" , uType .shortName ());
110
+ var uType = UType .parse (method .returnType ());
111
+ if (uType .isGeneric () && uType .mainType ().equals ("java.util.concurrent.CompletableFuture" )) {
112
+ uType = uType .paramRaw ();
113
+ }
114
+
115
+ writer .append (" %sJsonType.toJson(%s, ctx.contentType(\" application/json\" ).outputStream());" , uType .shortName (), returnVariableName );
93
116
} else {
94
- writer .append (" ctx.json(result );" );
117
+ writer .append (" ctx.json(%s );" , returnVariableName );
95
118
}
96
119
} else if (MediaType .TEXT_HTML .equalsIgnoreCase (produces )) {
97
- writer .append (" ctx.html(result );" );
120
+ writer .append (" ctx.html(%s );" , returnVariableName );
98
121
} else if (MediaType .TEXT_PLAIN .equalsIgnoreCase (produces )) {
99
- writer .append (" ctx.contentType(\" text/plain\" ).result(result );" );
122
+ writer .append (" ctx.contentType(\" text/plain\" ).result(%s );" , returnVariableName );
100
123
} else {
101
- writer .append (" ctx.contentType(\" %s\" ).result(result );" , produces );
124
+ writer .append (" ctx.contentType(\" %s\" ).result(%s );" , produces , returnVariableName );
102
125
}
103
126
}
104
127
}
0 commit comments