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

Properly support @JsonView on Resource class #35651

Merged
merged 1 commit into from
Aug 31, 2023
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
Expand Up @@ -237,7 +237,7 @@ void handleJsonAnnotations(Optional<ResourceScanningResultBuildItem> resourceSca
if ((jsonViews == null) || (jsonViews.length == 0)) {
continue;
}
recorder.recordJsonView(getMethodId(instance.target().asMethod()), jsonViews[0].name().toString());
recorder.recordJsonView(getTargetId(instance.target()), jsonViews[0].name().toString());
}
}
if (resourceClass.annotationsMap().containsKey(CUSTOM_SERIALIZATION)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.quarkus.resteasy.reactive.jackson.deployment.test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

import java.util.function.Supplier;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.annotation.JsonView;

import io.quarkus.test.QuarkusUnitTest;

class JsonViewOnClassTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(User.class, Views.class, Public.class, Private.class, Mixed.class);
}
});

@Test
void test() {
given().accept("application/json").get("public")
.then()
.statusCode(200)
.body(not(containsString("1")), containsString("test"));

given().accept("application/json").get("mixed")
.then()
.statusCode(200)
.body(containsString("1"), containsString("test"));

given().accept("application/json").get("private")
.then()
.statusCode(200)
.body(containsString("1"), containsString("test"));
}

@JsonView(Views.Public.class)
@Path("public")
public static class Public {

@GET
public User get() {
return User.testUser();
}
}

@JsonView(Views.Private.class)
@Path("private")
public static class Private {

@GET
public User get() {
return User.testUser();
}
}

@JsonView(Views.Public.class)
@Path("mixed")
public static class Mixed {

@GET
@JsonView(Views.Private.class)
public User get() {
return User.testUser();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,35 +255,28 @@ public void run() {
@GET
@Path("/user-without-view")
public User userWithoutView() {
return testUser();
return User.testUser();
}

@JsonView(Views.Public.class)
@GET
@Path("/user-with-public-view")
public User userWithPublicView() {
return testUser();
return User.testUser();
}

@JsonView(Views.Private.class)
@GET
@Path("/user-with-private-view")
public User userWithPrivateView() {
return testUser();
return User.testUser();
}

@CustomSerialization(UnquotedFieldsPersonSerialization.class)
@GET
@Path("/invalid-use-of-custom-serializer")
public User invalidUseOfCustomSerializer() {
return testUser();
}

private User testUser() {
User user = new User();
user.id = 1;
user.name = "test";
return user;
return User.testUser();
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ public class User {

@JsonView(Views.Public.class)
public String name;

public static User testUser() {
User user = new User();
user.id = 1;
user.name = "test";
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class ResteasyReactiveServerJacksonRecorder {
private static final Map<String, Class<?>> customSerializationMap = new HashMap<>();
private static final Map<String, Class<?>> customDeserializationMap = new HashMap<>();

public void recordJsonView(String methodId, String className) {
jsonViewMap.put(methodId, loadClass(className));
public void recordJsonView(String targetId, String className) {
jsonViewMap.put(targetId, loadClass(className));
}

public void recordCustomSerialization(String target, String className) {
Expand All @@ -42,6 +42,10 @@ public void run() {
});
}

public static Class<?> jsonViewForClass(Class<?> clazz) {
return jsonViewMap.get(clazz.getName());
}

public static Class<?> jsonViewForMethod(String methodId) {
return jsonViewMap.get(methodId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ public ObjectReader apply(ObjectMapper objectMapper) {
Class<?> jsonViewValue = ResteasyReactiveServerJacksonRecorder.jsonViewForMethod(resourceInfo.getMethodId());
if (jsonViewValue != null) {
return effectiveReader.withView(jsonViewValue);
} else {
jsonViewValue = ResteasyReactiveServerJacksonRecorder
.jsonViewForClass(resourceInfo.getResourceClass());
if (jsonViewValue != null) {
return effectiveReader.withView(jsonViewValue);
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public void writeResponse(Object o, Type genericType, ServerRequestContext conte
Class<?> jsonViewValue = ResteasyReactiveServerJacksonRecorder.jsonViewForMethod(resourceInfo.getMethodId());
if (jsonViewValue != null) {
effectiveWriter = effectiveWriter.withView(jsonViewValue);
} else {
jsonViewValue = ResteasyReactiveServerJacksonRecorder
.jsonViewForClass(resourceInfo.getResourceClass());
if (jsonViewValue != null) {
effectiveWriter = effectiveWriter.withView(jsonViewValue);
}

}
}
effectiveWriter.writeValue(stream, o);
Expand Down