Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
<kotlin.version>2.2.21</kotlin.version>
</properties>

<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>jakarta.servlet</groupId>
Expand Down Expand Up @@ -126,6 +138,10 @@
<artifactId>jackson-databind</artifactId>
<version>2.20.1</version>
</dependency>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xstream</artifactId>
Expand Down
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;
Comment on lines +3 to +4
Copy link

Copilot AI Nov 27, 2025

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.

Suggested change
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import tools.jackson.annotation.JsonIgnore;
import tools.jackson.annotation.JsonInclude;

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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 :

NOTE: jackson-annotations not affected as Jackson 3.0 uses 2.x variant (2.20) of jackson-annotations.

(https://github.com/FasterXML/jackson/wiki/Jackson-Release-3.0)

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"
}
17 changes: 17 additions & 0 deletions approvaltests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -80,6 +92,11 @@
<version>2.20.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Copy link

Copilot AI Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Jackson 3.x dependency should be marked as <optional>true</optional> to maintain consistency with the Jackson 2.x dependency (line 93) and other similar optional dependencies like gson, xstream, etc. This ensures that users of the library aren't forced to include Jackson 3.x if they don't use this feature.

Suggested change
<artifactId>jackson-databind</artifactId>
<artifactId>jackson-databind</artifactId>
<optional>true</optional>

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
Expand Down
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);
}
}
}