-
Notifications
You must be signed in to change notification settings - Fork 80
add support of Jackson 3 #736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package org.approvaltests; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class JsonJackson3ApprovalsTest | ||
| { | ||
| @Test | ||
| void testObjectMapperOverride() | ||
| { | ||
| MyClass o = new MyClass(); | ||
| JsonJackson3Approvals.verifyAsJson(o, | ||
| jm -> jm.changeDefaultPropertyInclusion(incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL))); | ||
| } | ||
|
|
||
| @Test | ||
| void testDuplicateFields() | ||
| { | ||
| JsonJackson3Approvals.verifyAsJson(new MyOtherClass()); | ||
| } | ||
| private static class MyClass | ||
| { | ||
| @JsonIgnore | ||
| private String name = "MyClass"; | ||
| public String lastName = "MyClass"; | ||
| public String middleName = null; | ||
| public String getName() | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) | ||
| { | ||
| this.name = name; | ||
| } | ||
| } | ||
| private static class MyOtherClass extends MyClass | ||
| { | ||
| @JsonIgnore | ||
| private String name = "MyOtherClass"; | ||
| @Override | ||
| public String getName() | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public void setName(String name) | ||
| { | ||
| this.name = name; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "lastName" : "MyClass", | ||
| "middleName" : null | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "lastName" : "MyClass" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,6 +37,18 @@ | |||||||
| <url>https://github.com/approvals/ApprovalTests.Java</url> | ||||||||
| </scm> | ||||||||
|
|
||||||||
| <dependencyManagement> | ||||||||
| <dependencies> | ||||||||
| <dependency> | ||||||||
| <groupId>tools.jackson</groupId> | ||||||||
| <artifactId>jackson-bom</artifactId> | ||||||||
| <version>3.0.2</version> | ||||||||
| <scope>import</scope> | ||||||||
| <type>pom</type> | ||||||||
| </dependency> | ||||||||
| </dependencies> | ||||||||
| </dependencyManagement> | ||||||||
|
|
||||||||
| <dependencies> | ||||||||
| <dependency> | ||||||||
| <groupId>com.approvaltests</groupId> | ||||||||
|
|
@@ -80,6 +92,11 @@ | |||||||
| <version>2.20.1</version> | ||||||||
| <optional>true</optional> | ||||||||
| </dependency> | ||||||||
| <dependency> | ||||||||
| <groupId>tools.jackson.core</groupId> | ||||||||
| <artifactId>jackson-databind</artifactId> | ||||||||
|
||||||||
| <artifactId>jackson-databind</artifactId> | |
| <artifactId>jackson-databind</artifactId> | |
| <optional>true</optional> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package org.approvaltests; | ||
|
|
||
| import com.spun.util.ObjectUtils; | ||
| import org.approvaltests.core.Options; | ||
| import org.lambda.functions.Function1; | ||
| import tools.jackson.core.JacksonException; | ||
| import tools.jackson.databind.ObjectMapper; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
|
|
||
| public class JsonJackson3Approvals | ||
| { | ||
| private JsonJackson3Approvals() | ||
| { | ||
| } | ||
|
|
||
| public static void verifyAsJson(Object o) | ||
| { | ||
| verifyAsJson(o, new Options()); | ||
| } | ||
|
|
||
| public static void verifyAsJson(Object o, Options options) | ||
| { | ||
| verifyAsJson(o, a -> a, options); | ||
| } | ||
|
|
||
| public static void verifyAsJson(Object o, Function1<JsonMapper.Builder, JsonMapper.Builder> objectMapperBuilder) | ||
| { | ||
| verifyAsJson(o, objectMapperBuilder, new Options()); | ||
| } | ||
|
|
||
| public static void verifyAsJson(Object o, Function1<JsonMapper.Builder, JsonMapper.Builder> objectMapperBuilder, | ||
| Options options) | ||
| { | ||
| Approvals.verify(asJson(o, objectMapperBuilder), options.forFile().withExtension(".json")); | ||
| } | ||
|
|
||
| public static String asJson(Object o) | ||
| { | ||
| return asJson(o, a -> a); | ||
| } | ||
|
|
||
| public static String asJson(Object o, Function1<JsonMapper.Builder, JsonMapper.Builder> objectMapperBuilder) | ||
| { | ||
| try | ||
| { | ||
| ObjectMapper objectMapper = objectMapperBuilder.call(JsonMapper.builder()).build(); | ||
| return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o); | ||
| } | ||
| catch (JacksonException e) | ||
| { | ||
| throw ObjectUtils.throwAsError(e); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is importing Jackson 2.x annotations (
com.fasterxml.jackson.*) instead of Jackson 3.x annotations (tools.jackson.*). For Jackson 3.x, the imports should be:import tools.jackson.annotation.JsonIgnore;import tools.jackson.annotation.JsonInclude;This will cause compilation errors or runtime issues when trying to use Jackson 3.x specific features.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no jackson-annotations dep for jackson 3.
Please read the note :
(https://github.com/FasterXML/jackson/wiki/Jackson-Release-3.0)