Skip to content

Commit 6f48550

Browse files
committed
Setting media type (json for eg) manually not working for certain cases. fix jooby-project#482
1 parent 5efa94a commit 6f48550

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.jooby.issues;
2+
3+
import java.util.concurrent.Executor;
4+
import java.util.concurrent.Executors;
5+
6+
import org.jooby.MediaType;
7+
import org.jooby.Results;
8+
import org.jooby.json.Jackson;
9+
import org.jooby.test.ServerFeature;
10+
import org.junit.Test;
11+
12+
public class Issue482 extends ServerFeature {
13+
14+
static class Raw {
15+
16+
String value;
17+
18+
public Raw(final String value) {
19+
this.value = value;
20+
}
21+
22+
}
23+
24+
{
25+
/** Raw JSON renderer .*/
26+
renderer((value, ctx) -> {
27+
if (value instanceof String && ctx.accepts(MediaType.json)) {
28+
ctx.send(value.toString());
29+
}
30+
});
31+
32+
use(new Jackson());
33+
34+
Executor exec = Executors.newSingleThreadExecutor();
35+
36+
get("/482/raw", promise(deferred -> {
37+
exec.execute(deferred.run(() -> Results.json("{\"status\":\"success\"}")));
38+
}));
39+
40+
}
41+
42+
@Test
43+
public void shouldAcceptResultsFromDeferredAPI() throws Exception {
44+
request()
45+
.get("/482/raw")
46+
.expect("{\"status\":\"success\"}")
47+
.header("Content-Type", "application/json;charset=UTF-8");
48+
}
49+
50+
}

‎jooby/src/main/java/org/jooby/Deferred.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,14 @@ public void resolve(final Object value) {
205205
if (value == null) {
206206
handler.handle(null, null);
207207
} else {
208-
super.set(value);
209-
handler.handle(clone(), null);
208+
Result result;
209+
if (value instanceof Result) {
210+
result = (Result) value;
211+
} else {
212+
super.set(value);
213+
result = clone();
214+
}
215+
handler.handle(result, null);
210216
}
211217
}
212218

0 commit comments

Comments
 (0)