-
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 #43443 from mariofusco/json-include
Support JsonInclude.Include in reflection free Jackson serializer
- Loading branch information
Showing
9 changed files
with
379 additions
and
29 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
30 changes: 30 additions & 0 deletions
30
...st/java/io/quarkus/resteasy/reactive/jackson/deployment/test/JsonIncludeTestResource.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,30 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import io.quarkus.resteasy.reactive.jackson.DisableSecureSerialization; | ||
import io.smallrye.common.annotation.NonBlocking; | ||
|
||
@Path("/json-include") | ||
@NonBlocking | ||
@DisableSecureSerialization | ||
public class JsonIncludeTestResource { | ||
|
||
@GET | ||
@Path("/my-object-empty") | ||
public MyObject getEmptyObject() { | ||
return new MyObject(); | ||
} | ||
|
||
@GET | ||
@Path("/my-object") | ||
public MyObject getObject() { | ||
MyObject myObject = new MyObject(); | ||
myObject.setName("name"); | ||
myObject.setDescription("description"); | ||
myObject.setStrings("test"); | ||
myObject.getMap().put("test", 1); | ||
return myObject; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ployment/src/test/java/io/quarkus/resteasy/reactive/jackson/deployment/test/MyObject.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,48 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class MyObject { | ||
|
||
private String name; | ||
|
||
private String description; | ||
|
||
private Map<String, Integer> map = new HashMap<>(); | ||
|
||
private String[] strings = new String[0]; | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Optional<String> getDescription() { | ||
return Optional.ofNullable(description); | ||
} | ||
|
||
public Map<String, Integer> getMap() { | ||
return map; | ||
} | ||
|
||
public void setMap(Map<String, Integer> map) { | ||
this.map = map; | ||
} | ||
|
||
public String[] getStrings() { | ||
return strings; | ||
} | ||
|
||
public void setStrings(String... strings) { | ||
this.strings = strings; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...s/resteasy/reactive/jackson/deployment/test/NonAbsentReflectionFreeSerializationTest.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,27 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonAbsentReflectionFreeSerializationTest extends NonAbsentSerializationTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(JsonIncludeTestResource.class, MyObject.class, NonAbsentObjectMapperCustomizer.class) | ||
.addAsResource( | ||
new StringAsset( | ||
"quarkus.rest.jackson.optimization.enable-reflection-free-serializers=true\n"), | ||
"application.properties"); | ||
} | ||
}); | ||
} |
70 changes: 70 additions & 0 deletions
70
...java/io/quarkus/resteasy/reactive/jackson/deployment/test/NonAbsentSerializationTest.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,70 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import jakarta.inject.Singleton; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
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.JsonInclude; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.jackson.ObjectMapperCustomizer; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class NonAbsentSerializationTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(JsonIncludeTestResource.class, MyObject.class, NonAbsentObjectMapperCustomizer.class) | ||
.addAsResource(new StringAsset(""), "application.properties"); | ||
} | ||
}); | ||
|
||
@Singleton | ||
public static class NonAbsentObjectMapperCustomizer implements ObjectMapperCustomizer { | ||
|
||
@Override | ||
public void customize(ObjectMapper objectMapper) { | ||
objectMapper | ||
.enable(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES) | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.setSerializationInclusion(JsonInclude.Include.NON_ABSENT); | ||
} | ||
} | ||
|
||
@Test | ||
public void testObject() { | ||
RestAssured.get("/json-include/my-object") | ||
.then() | ||
.statusCode(200) | ||
.contentType("application/json") | ||
.body("name", Matchers.equalTo("name")) | ||
.body("description", Matchers.equalTo("description")) | ||
.body("map.test", Matchers.equalTo(1)) | ||
.body("strings[0]", Matchers.equalTo("test")); | ||
} | ||
|
||
@Test | ||
public void testEmptyObject() { | ||
RestAssured.get("/json-include/my-object-empty") | ||
.then() | ||
.statusCode(200) | ||
.contentType("application/json") | ||
.body("name", Matchers.nullValue()) | ||
.body("description", Matchers.nullValue()) | ||
.body("map", Matchers.anEmptyMap()) | ||
.body("strings", Matchers.hasSize(0)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...us/resteasy/reactive/jackson/deployment/test/NonEmptyReflectionFreeSerializationTest.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,27 @@ | ||
package io.quarkus.resteasy.reactive.jackson.deployment.test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class NonEmptyReflectionFreeSerializationTest extends NonEmptySerializationTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(JsonIncludeTestResource.class, MyObject.class, NonEmptyObjectMapperCustomizer.class) | ||
.addAsResource( | ||
new StringAsset( | ||
"quarkus.rest.jackson.optimization.enable-reflection-free-serializers=true\n"), | ||
"application.properties"); | ||
} | ||
}); | ||
} |
Oops, something went wrong.