-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 #25597 from geoand/jackson-mixin
Introduce declarative support for Jackson's mixin feature
- Loading branch information
Showing
10 changed files
with
274 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
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
80 changes: 80 additions & 0 deletions
80
...ployment/src/test/java/io/quarkus/jackson/deployment/JacksonMixinsWithCustomizerTest.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,80 @@ | ||
package io.quarkus.jackson.deployment; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.jackson.JacksonMixin; | ||
import io.quarkus.jackson.ObjectMapperCustomizer; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class JacksonMixinsWithCustomizerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest(); | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Test | ||
public void test() throws JsonProcessingException { | ||
assertThat(objectMapper.writeValueAsString(new Fruit("test"))).isEqualTo("{\"manual\":\"test\"}"); | ||
assertThat(objectMapper.writeValueAsString(new Message("hello"))).isEqualTo("{}"); | ||
} | ||
|
||
@Singleton | ||
static class TestCustomizer implements ObjectMapperCustomizer { | ||
|
||
@Override | ||
public void customize(ObjectMapper objectMapper) { | ||
objectMapper.addMixIn(Fruit.class, ManualFruitMixin.class); | ||
} | ||
} | ||
|
||
public static class Fruit { | ||
public String name; | ||
|
||
public Fruit(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@JacksonMixin(Fruit.class) | ||
public abstract static class AutoFruitMixin { | ||
@JsonProperty("auto") | ||
public String name; | ||
} | ||
|
||
// this mixin will override the AutoFruitMixin because it will be explicitly registered by the user with a customizer | ||
public abstract static class ManualFruitMixin { | ||
@JsonProperty("manual") | ||
public String name; | ||
} | ||
|
||
public static class Message { | ||
private final String description; | ||
|
||
public Message(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
} | ||
|
||
@JacksonMixin(Message.class) | ||
public interface MessageMixin { | ||
@JsonIgnore | ||
String getDescription(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...yment/src/test/java/io/quarkus/jackson/deployment/JacksonMixinsWithoutCustomizerTest.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,53 @@ | ||
package io.quarkus.jackson.deployment; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.jackson.JacksonMixin; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class JacksonMixinsWithoutCustomizerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest(); | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Test | ||
public void test() throws JsonProcessingException { | ||
assertThat(objectMapper.writeValueAsString(new Fruit("test"))).isEqualTo("{\"nm\":\"test\"}"); | ||
assertThat(objectMapper.writeValueAsString(new Fruit2("test"))).isEqualTo("{\"nm\":\"test\"}"); | ||
} | ||
|
||
public static class Fruit { | ||
public String name; | ||
|
||
public Fruit(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
public static class Fruit2 { | ||
public String name; | ||
|
||
public Fruit2(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@JacksonMixin({ Fruit.class, Fruit2.class }) | ||
public abstract static class FruitMixin { | ||
@JsonProperty("nm") | ||
public String name; | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
extensions/jackson/runtime/src/main/java/io/quarkus/jackson/JacksonMixin.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,19 @@ | ||
package io.quarkus.jackson; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used on classes that are meant to be used as Jackson mixins. | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface JacksonMixin { | ||
|
||
/** | ||
* The types for which the mixin should apply to. | ||
*/ | ||
Class<?>[] value(); | ||
} |
34 changes: 34 additions & 0 deletions
34
extensions/jackson/runtime/src/main/java/io/quarkus/jackson/runtime/MixinsRecorder.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,34 @@ | ||
package io.quarkus.jackson.runtime; | ||
|
||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.jackson.ObjectMapperCustomizer; | ||
import io.quarkus.runtime.annotations.Recorder; | ||
|
||
@Recorder | ||
public class MixinsRecorder { | ||
|
||
public Supplier<ObjectMapperCustomizer> customizerSupplier(Map<Class<?>, Class<?>> mixinsMap) { | ||
return new Supplier<>() { | ||
@Override | ||
public ObjectMapperCustomizer get() { | ||
return new ObjectMapperCustomizer() { | ||
@Override | ||
public void customize(ObjectMapper objectMapper) { | ||
for (var entry : mixinsMap.entrySet()) { | ||
objectMapper.addMixIn(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return DEFAULT_PRIORITY + 1; | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
integration-tests/jackson/src/main/java/io/quarkus/it/jackson/model/SampleResponseMixin.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,12 @@ | ||
package io.quarkus.it.jackson.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import io.quarkus.jackson.JacksonMixin; | ||
|
||
@JacksonMixin(SampleResponse.class) | ||
public abstract class SampleResponseMixin { | ||
|
||
@JsonProperty("nm") | ||
public abstract String getName(); | ||
} |
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