Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generic type handling of body parameter in RESTEasy Reactive #16907

Merged
merged 1 commit into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.resteasy.reactive.jackson.deployment.test;

public class DataItem<T> {

private T content;

public T getContent() {
return content;
}

public void setContent(T content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.resteasy.reactive.jackson.deployment.test;

public class Item {

private String name;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ public Multi<Person> getMulti0() {
return Multi.createFrom().empty();
}

@POST
@Path("/genericInput")
public String genericInputTest(DataItem<Item> item) {
return item.getContent().getName();
}

public static class UnquotedFieldsPersonBiFunction implements BiFunction<ObjectMapper, Type, ObjectWriter> {

public static final AtomicInteger count = new AtomicInteger();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.resteasy.reactive.jackson.deployment.test;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
Expand All @@ -26,6 +27,7 @@ public class SimpleJsonTest {
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Person.class, SimpleJsonResource.class, User.class, Views.class, SuperClass.class,
DataItem.class, Item.class,
NoopReaderInterceptor.class);
}
});
Expand Down Expand Up @@ -293,4 +295,17 @@ public void testCustomSerialization() {
// a new instance should have been created
assertEquals(3, SimpleJsonResource.UnquotedFieldsPersonBiFunction.count.intValue());
}

@Test
public void testGenericInput() {
RestAssured
.with()
.body("{\"content\": {\"name\":\"foo\", \"email\":\"bar\"}}")
.contentType("application/json; charset=utf-8")
.post("/simple/genericInput")
.then()
.statusCode(200)
.contentType("text/plain")
.body(is("foo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.resteasy.reactive.jsonb.deployment.test;

public class DataItem<T> {

private T content;

public T getContent() {
return content;
}

public void setContent(T content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.resteasy.reactive.jsonb.deployment.test;

public class Item {

private String name;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,10 @@ public Multi<Person> getMulti2() {
public Multi<Person> getMulti0() {
return Multi.createFrom().empty();
}

@POST
@Path("/genericInput")
public String genericInputTest(DataItem<Item> item) {
return item.getContent().getName();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.resteasy.reactive.jsonb.deployment.test;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

Expand All @@ -22,7 +23,7 @@ public class SimpleJsonTest {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Person.class, SimpleJsonResource.class, SuperClass.class);
.addClasses(Person.class, SimpleJsonResource.class, SuperClass.class, DataItem.class, Item.class);
}
});

Expand Down Expand Up @@ -200,4 +201,17 @@ public void testJsonMulti() {
.contentType("application/json")
.body(Matchers.equalTo("[]"));
}

@Test
public void testGenericInput() {
RestAssured
.with()
.body("{\"content\": {\"name\":\"foo\", \"email\":\"bar\"}}")
.contentType("application/json; charset=utf-8")
.post("/simple/genericInput")
.then()
.statusCode(200)
.contentType("text/plain")
.body(is("foo"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.COOKIE_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.DEFAULT_VALUE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.DOUBLE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.DUMMY_ELEMENT_TYPE;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.FLOAT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.FORM_PARAM;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.HEADER_PARAM;
Expand Down Expand Up @@ -945,6 +946,11 @@ && isContextType(paramType.asClassType())) {
builder.setOptional(true);
} else if (convertible) {
throw new RuntimeException("Invalid parameter type '" + pt + "' used on method " + errorLocation);
} else {
// the "element" type is not of importance as in this case the signature is used at runtime to determine the proper types
elementType = DUMMY_ELEMENT_TYPE.toString();
addReaderForType(additionalReaders, pt);
typeHandled = true;
}
} else if ((paramType.name().equals(PATH_SEGMENT)) && (type == ParameterType.PATH)) {
elementType = paramType.name().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import javax.ws.rs.sse.Sse;
import javax.ws.rs.sse.SseEventSink;
import org.jboss.jandex.DotName;
import org.jboss.resteasy.reactive.DummyElementType;
import org.jboss.resteasy.reactive.MultipartForm;
import org.jboss.resteasy.reactive.RestCookie;
import org.jboss.resteasy.reactive.RestForm;
Expand Down Expand Up @@ -163,6 +164,7 @@ public final class ResteasyReactiveDotNames {
public static final DotName SET = DotName.createSimple(Set.class.getName());
public static final DotName SORTED_SET = DotName.createSimple(SortedSet.class.getName());
public static final DotName MAP = DotName.createSimple(Map.class.getName());
public static final DotName DUMMY_ELEMENT_TYPE = DotName.createSimple(DummyElementType.class.getName());
public static final DotName MULTI_VALUED_MAP = DotName.createSimple(MultivaluedMap.class.getName());
public static final DotName PATH_SEGMENT = DotName.createSimple(PathSegment.class.getName());
public static final DotName LOCAL_DATE = DotName.createSimple(LocalDate.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jboss.resteasy.reactive;

/**
* Used only to aid with generic types of endpoint parameters
*/
public final class DummyElementType {
}