forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
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 quarkusio#4757 from geoand/quarkusio#4719
Make Jackson + JSON-B @JsonSerializer/Deserializer for fields work in native
- Loading branch information
Showing
13 changed files
with
544 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,6 +284,7 @@ stages: | |
timeoutInMinutes: 25 | ||
modules: | ||
- jackson | ||
- jsonb | ||
- jgit | ||
- kogito | ||
- kubernetes-client | ||
|
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
89 changes: 89 additions & 0 deletions
89
...io/quarkus/reproducer/jacksonbuilder/model/ModelWithSerializerAndDeserializerOnField.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,89 @@ | ||
package io.quarkus.reproducer.jacksonbuilder.model; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
|
||
@RegisterForReflection | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class ModelWithSerializerAndDeserializerOnField { | ||
|
||
private String name; | ||
|
||
@JsonDeserialize(using = InnerDeserializer.class) | ||
@JsonSerialize(using = InnerSerializer.class) | ||
private Inner inner; | ||
|
||
public ModelWithSerializerAndDeserializerOnField() { | ||
} | ||
|
||
public ModelWithSerializerAndDeserializerOnField(String name, Inner inner) { | ||
this.name = name; | ||
this.inner = inner; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Inner getInner() { | ||
return inner; | ||
} | ||
|
||
public void setInner(Inner inner) { | ||
this.inner = inner; | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class Inner { | ||
private String someValue; | ||
|
||
public Inner() { | ||
} | ||
|
||
public Inner(String someValue) { | ||
this.someValue = someValue; | ||
} | ||
|
||
public String getSomeValue() { | ||
return someValue; | ||
} | ||
|
||
public void setSomeValue(String someValue) { | ||
this.someValue = someValue; | ||
} | ||
} | ||
|
||
public static class InnerDeserializer extends JsonDeserializer<Inner> { | ||
|
||
@Override | ||
public Inner deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { | ||
return new Inner("immutable"); | ||
} | ||
} | ||
|
||
public static class InnerSerializer extends JsonSerializer<Inner> { | ||
@Override | ||
public void serialize(Inner value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
gen.writeStartObject(); | ||
gen.writeStringField("someValue", "unchangeable"); | ||
gen.writeEndObject(); | ||
} | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...e/src/main/java/io/quarkus/reproducer/ModelWithSerializerDeserializerOnFieldResource.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,43 @@ | ||
package io.quarkus.reproducer; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.reproducer.jacksonbuilder.model.ModelWithSerializerAndDeserializerOnField; | ||
|
||
@Path("fieldserder") | ||
public class ModelWithSerializerDeserializerOnFieldResource { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public ModelWithSerializerDeserializerOnFieldResource(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@POST | ||
@Produces(MediaType.TEXT_PLAIN) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public String post(String body) throws IOException { | ||
ModelWithSerializerAndDeserializerOnField input = objectMapper.readValue(body, | ||
ModelWithSerializerAndDeserializerOnField.class); | ||
return input.getName() + "/" + input.getInner().getSomeValue(); | ||
} | ||
|
||
@GET | ||
@Path("/{name}/{someValue}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public String get(@PathParam("name") String name, @PathParam("someValue") String someValue) throws IOException { | ||
ModelWithSerializerAndDeserializerOnField input = new ModelWithSerializerAndDeserializerOnField(name, | ||
new ModelWithSerializerAndDeserializerOnField.Inner(someValue)); | ||
return objectMapper.writeValueAsString(input); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../test/java/io/quarkus/reproducer/ModelWithSerializerAndDeserializerOnFieldResourceIT.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,8 @@ | ||
package io.quarkus.reproducer; | ||
|
||
import io.quarkus.test.junit.SubstrateTest; | ||
|
||
@SubstrateTest | ||
public class ModelWithSerializerAndDeserializerOnFieldResourceIT extends ModelWithSerializerAndDeserializerOnFieldResourceTest { | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...est/java/io/quarkus/reproducer/ModelWithSerializerAndDeserializerOnFieldResourceTest.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,44 @@ | ||
package io.quarkus.reproducer; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.reproducer.jacksonbuilder.model.ModelWithSerializerAndDeserializerOnField; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class ModelWithSerializerAndDeserializerOnFieldResourceTest { | ||
|
||
@Test | ||
public void testSerializer() throws IOException { | ||
given() | ||
.contentType("application/json") | ||
.when().get("/fieldserder/tester/whatever") | ||
.then() | ||
.statusCode(200) | ||
.body("name", equalTo("tester")) | ||
.body("inner.someValue", equalTo("unchangeable")); | ||
} | ||
|
||
@Test | ||
public void testDeserializer() throws IOException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
given() | ||
.contentType("application/json") | ||
.body(objectMapper.writeValueAsString( | ||
new ModelWithSerializerAndDeserializerOnField("tester", | ||
new ModelWithSerializerAndDeserializerOnField.Inner()))) | ||
.when().post("/fieldserder") | ||
.then() | ||
.statusCode(200) | ||
.body(is("tester/immutable")); | ||
} | ||
} |
Oops, something went wrong.