Skip to content

Commit c62127a

Browse files
LarsEckartisidore
authored andcommitted
! B json array now odered when using JsonApprovals
Resolves issue #667
1 parent 927eaf5 commit c62127a

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

approvaltests-tests/src/test/java/org/approvaltests/JsonApprovalsTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.gson.GsonBuilder;
44
import org.junit.jupiter.api.Test;
5-
import org.lambda.functions.Function1;
65

76
import java.time.LocalDateTime;
87

@@ -37,4 +36,52 @@ public void setLocalDate(LocalDateTime localDate)
3736
}
3837
private LocalDateTime localDate;
3938
}
39+
@Test
40+
void verifyJsonReorderWithoutArray()
41+
{
42+
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
43+
String variant1 = """
44+
{
45+
"a" = 1,
46+
"b" = 2
47+
}
48+
""";
49+
String variant2 = """
50+
{
51+
"b" = 2,
52+
"a" = 1
53+
}
54+
""";
55+
// It does not matter on which we call the verifyJson
56+
JsonApprovals.verifyJson(variant1, true);
57+
JsonApprovals.verifyJson(variant2, true);
58+
}
59+
@Test
60+
void verifyJsonReorderWithArray()
61+
{
62+
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
63+
String variant1 = """
64+
{
65+
"array" = [
66+
{
67+
"a" = 1,
68+
"b" = 2
69+
}
70+
]
71+
}
72+
""";
73+
String variant2 = """
74+
{
75+
"array" = [
76+
{
77+
"b" = 2,
78+
"a" = 1
79+
}
80+
]
81+
}
82+
""";
83+
// Now both variants should work since objects within arrays get reordered
84+
JsonApprovals.verifyJson(variant1, true);
85+
JsonApprovals.verifyJson(variant2, true);
86+
}
4087
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"array": [
3+
{
4+
"a": 1,
5+
"b": 2
6+
}
7+
]
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"a": 1,
3+
"b": 2
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"array": [
3+
{
4+
"a": 1,
5+
"b": 2
6+
}
7+
]
8+
}

approvaltests-util/src/main/java/com/spun/util/JsonUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonArray;
56
import com.google.gson.JsonElement;
67
import com.google.gson.JsonObject;
78
import com.google.gson.JsonParser;
@@ -100,13 +101,37 @@ public static JsonObject sortJsonObjectFields(JsonObject jsonObject)
100101
{
101102
sortedJsonObject.add(entry.getKey(), sortJsonObjectFields(element.getAsJsonObject()));
102103
}
104+
else if (element.isJsonArray())
105+
{
106+
sortedJsonObject.add(entry.getKey(), sortJsonArrayFields(element.getAsJsonArray()));
107+
}
103108
else
104109
{
105110
sortedJsonObject.add(entry.getKey(), element);
106111
}
107112
}
108113
return sortedJsonObject;
109114
}
115+
public static JsonArray sortJsonArrayFields(JsonArray jsonArray)
116+
{
117+
JsonArray sortedJsonArray = new JsonArray();
118+
for (JsonElement element : jsonArray)
119+
{
120+
if (element.isJsonObject())
121+
{
122+
sortedJsonArray.add(sortJsonObjectFields(element.getAsJsonObject()));
123+
}
124+
else if (element.isJsonArray())
125+
{
126+
sortedJsonArray.add(sortJsonArrayFields(element.getAsJsonArray()));
127+
}
128+
else
129+
{
130+
sortedJsonArray.add(element);
131+
}
132+
}
133+
return sortedJsonArray;
134+
}
110135
public static class InstantAdapter extends TypeAdapter<Instant>
111136
{
112137
@Override

0 commit comments

Comments
 (0)