Skip to content

Commit

Permalink
- F allow customization of ObjectMapper used in JsonJacksonApprovals -
Browse files Browse the repository at this point in the history
…closes #478

Co-authored-by: Jay Bazuzi <[email protected]>
Co-authored-by: Llewellyn Falco <[email protected]>
  • Loading branch information
3 people committed Sep 30, 2024
1 parent 449b5c8 commit 257151a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L17-L20) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L37-L40) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L36) (String, boolean, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L14-L17) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L17-L20) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L25-L28) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonXstreamApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonXstreamApprovals.java#L18-L21) (Object, $\color{#AAA}{\textsf{Options}}$)
* VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L24) (ContextAware, $\color{#AAA}{\textsf{Options}}$)
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package org.approvaltests;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.Test;

public class JsonJacksonApprovalsTest
{
@Test
void testObjectMapperOverride()
{
MyClass o = new MyClass();

JsonJacksonApprovals.verifyAsJson(o, (om)-> om
.setSerializationInclusion(JsonInclude.Include.NON_NULL));
}
@Test
void testDuplicateFields()
{
Expand All @@ -15,6 +26,7 @@ private static class MyClass
@JsonIgnore
private String name = "MyClass";
public String lastName = "MyClass";
public String middleName = null;
public String getName()
{
return name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"lastName" : "MyClass"
"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
@@ -0,0 +1,3 @@
{
"lastName" : "MyClass"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spun.util.ObjectUtils;
import org.approvaltests.core.Options;
import org.lambda.functions.Function1;

import java.util.function.Function;

public class JsonJacksonApprovals
{
Expand All @@ -13,13 +16,24 @@ public static void verifyAsJson(Object o)
}
public static void verifyAsJson(Object o, Options options)
{
Approvals.verify(asJson(o), options.forFile().withExtension(".json"));
verifyAsJson(o, a -> a, options);
}
public static void verifyAsJson(Object o, Function1<ObjectMapper, ObjectMapper> objectMapperBuilder)
{
verifyAsJson(o, objectMapperBuilder, new Options());
}
public static void verifyAsJson(Object o, Function1<ObjectMapper, ObjectMapper> 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)
public static String asJson(Object o, Function1<ObjectMapper, ObjectMapper> objectMapperBuilder)
{
try
{
ObjectMapper objectMapper = new ObjectMapper();
ObjectMapper objectMapper = objectMapperBuilder.call(new ObjectMapper());
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o);
}
catch (JsonProcessingException e)
Expand Down

0 comments on commit 257151a

Please sign in to comment.