-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35158 from Sgitario/35138_followup
Allow using `@CustomSerialization` and `@CustomDeserialization` at class level
- Loading branch information
Showing
9 changed files
with
329 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...ava/io/quarkus/resteasy/reactive/jackson/deployment/test/CustomSerializationResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.BiFunction; | ||
|
||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; | ||
|
||
import com.fasterxml.jackson.core.json.JsonReadFeature; | ||
import com.fasterxml.jackson.core.json.JsonWriteFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
|
||
import io.quarkus.resteasy.reactive.jackson.CustomDeserialization; | ||
import io.quarkus.resteasy.reactive.jackson.CustomSerialization; | ||
|
||
@Path("/custom-serialization") | ||
@CustomSerialization(CustomSerializationResource.UnquotedFieldsPersonSerialization.class) | ||
@CustomDeserialization(CustomSerializationResource.UnquotedFieldsPersonDeserialization.class) | ||
public class CustomSerializationResource { | ||
|
||
@ServerExceptionMapper | ||
public Response handleParseException(WebApplicationException e) { | ||
var cause = e.getCause() == null ? e : e.getCause(); | ||
return Response.status(Response.Status.BAD_REQUEST).entity(cause.getMessage()).build(); | ||
} | ||
|
||
@GET | ||
@Path("/person") | ||
public Person getPerson() { | ||
Person person = new Person(); | ||
person.setFirst("Bob"); | ||
person.setLast("Builder"); | ||
return person; | ||
} | ||
|
||
@POST | ||
@Path("/person") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public Person getPerson(Person person) { | ||
return person; | ||
} | ||
|
||
@POST | ||
@Path("/people/list") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public List<Person> getPeople(List<Person> people) { | ||
List<Person> reversed = new ArrayList<>(people.size()); | ||
for (Person person : people) { | ||
reversed.add(0, person); | ||
} | ||
return reversed; | ||
} | ||
|
||
@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; | ||
} | ||
|
||
public static class UnquotedFieldsPersonSerialization implements BiFunction<ObjectMapper, Type, ObjectWriter> { | ||
|
||
public static final AtomicInteger count = new AtomicInteger(); | ||
|
||
public UnquotedFieldsPersonSerialization() { | ||
count.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public ObjectWriter apply(ObjectMapper objectMapper, Type type) { | ||
if (type instanceof ParameterizedType) { | ||
type = ((ParameterizedType) type).getActualTypeArguments()[0]; | ||
} | ||
if (!type.getTypeName().equals(Person.class.getName())) { | ||
throw new IllegalArgumentException("Only Person type can be handled"); | ||
} | ||
return objectMapper.writer().without(JsonWriteFeature.QUOTE_FIELD_NAMES); | ||
} | ||
} | ||
|
||
public static class UnquotedFieldsPersonDeserialization implements BiFunction<ObjectMapper, Type, ObjectReader> { | ||
|
||
public static final AtomicInteger count = new AtomicInteger(); | ||
|
||
public UnquotedFieldsPersonDeserialization() { | ||
count.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public ObjectReader apply(ObjectMapper objectMapper, Type type) { | ||
if (type instanceof ParameterizedType) { | ||
type = ((ParameterizedType) type).getActualTypeArguments()[0]; | ||
} | ||
if (!type.getTypeName().equals(Person.class.getName())) { | ||
throw new IllegalArgumentException("Only Person type can be handled"); | ||
} | ||
return objectMapper.reader().with(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES); | ||
} | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
...st/java/io/quarkus/resteasy/reactive/jackson/deployment/test/CustomSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.function.Supplier; | ||
|
||
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 io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class CustomSerializationTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(Person.class, CustomSerializationResource.class, User.class, Views.class); | ||
} | ||
}); | ||
|
||
@Test | ||
public void testCustomSerialization() { | ||
// assert that we get a proper response | ||
// we can't use json-path to assert because the returned string is not proper json as it does not have quotes around the field names | ||
RestAssured.get("/custom-serialization/person") | ||
.then() | ||
.statusCode(200) | ||
.contentType("application/json") | ||
.body(containsString("Bob")) | ||
.body(containsString("Builder")); | ||
|
||
// assert with a list of people | ||
RestAssured | ||
.with() | ||
.body("[{\"first\": \"Bob\", \"last\": \"Builder\"}, {\"first\": \"Bob2\", \"last\": \"Builder2\"}]") | ||
.contentType("application/json; charset=utf-8") | ||
.post("/custom-serialization/people/list") | ||
.then() | ||
.statusCode(200) | ||
.contentType("application/json") | ||
.body(containsString("Bob")) | ||
.body(containsString("Builder")) | ||
.body(containsString("Bob2")) | ||
.body(containsString("Builder2")); | ||
|
||
// a new instance should have been created | ||
int currentCount = CustomSerializationResource.UnquotedFieldsPersonSerialization.count.get(); | ||
RestAssured.get("/custom-serialization/invalid-use-of-custom-serializer") | ||
.then() | ||
.statusCode(500); | ||
assertEquals(currentCount + 1, CustomSerializationResource.UnquotedFieldsPersonSerialization.count.intValue()); | ||
} | ||
|
||
@Test | ||
public void testCustomDeserialization() { | ||
// assert that the reader support the unquoted fields (because we have used a custom object reader | ||
// via `@CustomDeserialization` | ||
RestAssured.given() | ||
.body("{first: \"Hello\", last: \"Deserialization\"}") | ||
.contentType("application/json; charset=utf-8") | ||
.post("/custom-serialization/person") | ||
.then() | ||
.statusCode(200) | ||
.contentType("application/json") | ||
.body(containsString("Hello")) | ||
.body(containsString("Deserialization")); | ||
|
||
// assert that the instances were re-used as we simply invoked methods that should have already created their object readers | ||
RestAssured.given() | ||
.body("{first: \"Hello\", last: \"Deserialization\"}") | ||
.contentType("application/json; charset=utf-8") | ||
.post("/custom-serialization/person") | ||
.then() | ||
.statusCode(200); | ||
|
||
// assert with a list of people | ||
RestAssured | ||
.with() | ||
.body("[{first: \"Bob\", last: \"Builder\"}, {first: \"Bob2\", last: \"Builder2\"}]") | ||
.contentType("application/json; charset=utf-8") | ||
.post("/custom-serialization/people/list") | ||
.then() | ||
.statusCode(200) | ||
.contentType("application/json") | ||
.body(containsString("Bob")) | ||
.body(containsString("Builder")) | ||
.body(containsString("Bob2")) | ||
.body(containsString("Builder2")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.