Skip to content

Commit

Permalink
Properly support @JSONVIEW on Resource class
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Aug 31, 2023
1 parent c352f20 commit 3b3f0f0
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 14 deletions.
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

0 comments on commit 3b3f0f0

Please sign in to comment.