Skip to content

Commit

Permalink
415 unsupported media type when using mount to add routes
Browse files Browse the repository at this point in the history
- fix #3500
- fix #3400
  • Loading branch information
jknack committed Aug 13, 2024
1 parent c034bf9 commit dd618dd
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 3 deletions.
5 changes: 3 additions & 2 deletions jooby/src/main/java/io/jooby/internal/RouterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,8 @@ private void copy(Route src, Route it) {
it.setExecutorKey(src.getExecutorKey());
it.setTags(src.getTags());
it.setDescription(src.getDescription());
it.setDecoders(src.getDecoders());
// DO NOT COPY: See https://github.com/jooby-project/jooby/issues/3500
// it.setDecoders(src.getDecoders());
it.setMvcMethod(src.getMvcMethod());
it.setNonBlocking(src.isNonBlocking());
it.setSummary(src.getSummary());
Expand Down Expand Up @@ -983,7 +984,7 @@ private static void override(
Jooby app = (Jooby) router;
override(src, app.getRouter(), consumer);
} else if (router instanceof RouterImpl that) {
consumer.accept((RouterImpl) src, that);
consumer.accept(src, that);
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<artifactId>jooby-jackson</artifactId>
<version>${jooby.version}</version>
</dependency>
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-gson</artifactId>
<version>${jooby.version}</version>
</dependency>
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-avaje-jsonb</artifactId>
Expand Down Expand Up @@ -250,7 +255,6 @@
<version>1.37</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
45 changes: 45 additions & 0 deletions tests/src/test/java/io/jooby/i3400/Issue3400.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.i3400;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.jooby.Jooby;
import io.jooby.jackson.JacksonModule;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class Issue3400 {

static class AppA extends Jooby {
{
post("/pets", ctx -> ctx.body(Pet3400.class));
}
}

@ServerTest
public void shouldShareDecodersOnMountedResources(ServerTestRunner runner) {
runner
.define(
app -> {
app.install(new JacksonModule());
app.mount(new AppA());
})
.ready(
http -> {
http.post(
"/pets",
RequestBody.create(
"{\"id\": 1, \"name\": \"Cheddar\"}", MediaType.parse("application/json")),
rsp -> {
assertEquals("{\"id\":1,\"name\":\"Cheddar\"}", rsp.body().string());
assertEquals("application/json;charset=UTF-8", rsp.header("Content-Type"));
});
});
}
}
32 changes: 32 additions & 0 deletions tests/src/test/java/io/jooby/i3400/Pet3400.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.i3400;

public class Pet3400 {
private int id;
private String name;

public Pet3400(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
37 changes: 37 additions & 0 deletions tests/src/test/java/io/jooby/i3500/Issue3500.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.i3500;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class Issue3500 {

@ServerTest
public void shouldShareDecodersOnMountedResources(ServerTestRunner runner) {
runner
.use(WidgetService::new)
.ready(
http -> {
http.post(
"/api/widgets1",
RequestBody.create("{\"id\": 1}", MediaType.get("application/json")),
rsp -> {
assertEquals(201, rsp.code());
});
http.post(
"/api/widgets2",
RequestBody.create("{\"id\": 1}", MediaType.get("application/json")),
rsp -> {
assertEquals(201, rsp.code());
});
});
}
}
18 changes: 18 additions & 0 deletions tests/src/test/java/io/jooby/i3500/Widget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.i3500;

public class Widget {
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
44 changes: 44 additions & 0 deletions tests/src/test/java/io/jooby/i3500/WidgetService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.i3500;

import io.jooby.Jooby;
import io.jooby.StatusCode;
import io.jooby.gson.GsonModule;

public class WidgetService extends Jooby {

public WidgetService() {
install(new GsonModule());

post(
"/api/widgets1",
ctx -> {
Widget widget = ctx.body().to(Widget.class);
System.out.println("Created " + widget);
return ctx.send(StatusCode.CREATED);
});

mount(new WidgetRouter());
}

public static void main(String[] args) {
new WidgetService().start();
}
}

class WidgetRouter extends Jooby {

public WidgetRouter() {

post(
"/api/widgets2",
ctx -> {
Widget widget = ctx.body().to(Widget.class);
return ctx.send(StatusCode.CREATED);
});
}
}

0 comments on commit dd618dd

Please sign in to comment.