Skip to content

Commit 3b66df8

Browse files
authored
Merge pull request #3502 from jooby-project/3500
415 unsupported media type when using mount to add routes
2 parents c034bf9 + f414c82 commit 3b66df8

File tree

7 files changed

+205
-3
lines changed

7 files changed

+205
-3
lines changed

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,8 @@ private void copy(Route src, Route it) {
911911
it.setExecutorKey(src.getExecutorKey());
912912
it.setTags(src.getTags());
913913
it.setDescription(src.getDescription());
914-
it.setDecoders(src.getDecoders());
914+
// DO NOT COPY: See https://github.com/jooby-project/jooby/issues/3500
915+
// it.setDecoders(src.getDecoders());
915916
it.setMvcMethod(src.getMvcMethod());
916917
it.setNonBlocking(src.isNonBlocking());
917918
it.setSummary(src.getSummary());
@@ -983,7 +984,7 @@ private static void override(
983984
Jooby app = (Jooby) router;
984985
override(src, app.getRouter(), consumer);
985986
} else if (router instanceof RouterImpl that) {
986-
consumer.accept((RouterImpl) src, that);
987+
consumer.accept(src, that);
987988
}
988989
}
989990

tests/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<artifactId>jooby-jackson</artifactId>
3434
<version>${jooby.version}</version>
3535
</dependency>
36+
<dependency>
37+
<groupId>io.jooby</groupId>
38+
<artifactId>jooby-gson</artifactId>
39+
<version>${jooby.version}</version>
40+
</dependency>
3641
<dependency>
3742
<groupId>io.jooby</groupId>
3843
<artifactId>jooby-avaje-jsonb</artifactId>
@@ -250,7 +255,6 @@
250255
<version>1.37</version>
251256
<scope>test</scope>
252257
</dependency>
253-
254258
</dependencies>
255259

256260
<build>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3400;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import io.jooby.Jooby;
11+
import io.jooby.jackson.JacksonModule;
12+
import io.jooby.junit.ServerTest;
13+
import io.jooby.junit.ServerTestRunner;
14+
import okhttp3.MediaType;
15+
import okhttp3.RequestBody;
16+
17+
public class Issue3400 {
18+
19+
static class AppA extends Jooby {
20+
{
21+
post("/pets", ctx -> ctx.body(Pet3400.class));
22+
}
23+
}
24+
25+
@ServerTest
26+
public void shouldShareDecodersOnMountedResources(ServerTestRunner runner) {
27+
runner
28+
.define(
29+
app -> {
30+
app.install(new JacksonModule());
31+
app.mount(new AppA());
32+
})
33+
.ready(
34+
http -> {
35+
http.post(
36+
"/pets",
37+
RequestBody.create(
38+
"{\"id\": 1, \"name\": \"Cheddar\"}", MediaType.parse("application/json")),
39+
rsp -> {
40+
assertEquals("{\"id\":1,\"name\":\"Cheddar\"}", rsp.body().string());
41+
assertEquals("application/json;charset=UTF-8", rsp.header("Content-Type"));
42+
});
43+
});
44+
}
45+
46+
@ServerTest
47+
public void shouldShareDecodersOnInstalledResources(ServerTestRunner runner) {
48+
runner
49+
.define(
50+
app -> {
51+
app.install(new JacksonModule());
52+
app.install(AppA::new);
53+
})
54+
.ready(
55+
http -> {
56+
http.post(
57+
"/pets",
58+
RequestBody.create(
59+
"{\"id\": 1, \"name\": \"Cheddar\"}", MediaType.parse("application/json")),
60+
rsp -> {
61+
assertEquals("{\"id\":1,\"name\":\"Cheddar\"}", rsp.body().string());
62+
assertEquals("application/json;charset=UTF-8", rsp.header("Content-Type"));
63+
});
64+
});
65+
}
66+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3400;
7+
8+
public class Pet3400 {
9+
private int id;
10+
private String name;
11+
12+
public Pet3400(int id, String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3500;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import io.jooby.junit.ServerTest;
11+
import io.jooby.junit.ServerTestRunner;
12+
import okhttp3.MediaType;
13+
import okhttp3.RequestBody;
14+
15+
public class Issue3500 {
16+
17+
@ServerTest
18+
public void shouldShareDecodersOnMountedResources(ServerTestRunner runner) {
19+
runner
20+
.use(WidgetService::new)
21+
.ready(
22+
http -> {
23+
http.post(
24+
"/api/widgets1",
25+
RequestBody.create("{\"id\": 1}", MediaType.get("application/json")),
26+
rsp -> {
27+
assertEquals(201, rsp.code());
28+
});
29+
http.post(
30+
"/api/widgets2",
31+
RequestBody.create("{\"id\": 1}", MediaType.get("application/json")),
32+
rsp -> {
33+
assertEquals(201, rsp.code());
34+
});
35+
});
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3500;
7+
8+
public class Widget {
9+
private int id;
10+
11+
public int getId() {
12+
return id;
13+
}
14+
15+
public void setId(int id) {
16+
this.id = id;
17+
}
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3500;
7+
8+
import io.jooby.Jooby;
9+
import io.jooby.StatusCode;
10+
import io.jooby.gson.GsonModule;
11+
12+
public class WidgetService extends Jooby {
13+
14+
public WidgetService() {
15+
install(new GsonModule());
16+
17+
post(
18+
"/api/widgets1",
19+
ctx -> {
20+
Widget widget = ctx.body().to(Widget.class);
21+
System.out.println("Created " + widget);
22+
return ctx.send(StatusCode.CREATED);
23+
});
24+
25+
mount(new WidgetRouter());
26+
}
27+
28+
public static void main(String[] args) {
29+
new WidgetService().start();
30+
}
31+
}
32+
33+
class WidgetRouter extends Jooby {
34+
35+
public WidgetRouter() {
36+
37+
post(
38+
"/api/widgets2",
39+
ctx -> {
40+
Widget widget = ctx.body().to(Widget.class);
41+
return ctx.send(StatusCode.CREATED);
42+
});
43+
}
44+
}

0 commit comments

Comments
 (0)