Skip to content

Commit 75af075

Browse files
authored
fix: Refactor JsonAssert and apply to json/yaml assertions (#5002)
1 parent 0a5556a commit 75af075

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

modules/swagger-core/src/test/java/io/swagger/v3/core/converting/SwaggerSerializerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.v3.core.matchers.SerializationMatchers;
66
import io.swagger.v3.core.oas.models.Person;
77
import io.swagger.v3.core.util.Json;
8+
import io.swagger.v3.core.util.JsonAssert;
89
import io.swagger.v3.core.util.OutputReplacer;
910
import io.swagger.v3.core.util.ResourceUtils;
1011
import io.swagger.v3.oas.models.Components;
@@ -172,7 +173,7 @@ public void writeSpecWithParameterReferences() throws IOException {
172173

173174
final String swaggerJson = Json.mapper().writeValueAsString(swagger);
174175
final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
175-
assertEquals(Json.pretty(rebuilt), Json.pretty(swagger));
176+
JsonAssert.assertJsonEquals(Json.mapper(), Json.pretty(rebuilt), Json.pretty(swagger));
176177
}
177178

178179
@Test

modules/swagger-core/src/test/java/io/swagger/v3/core/roundtrip/ComprehensiveRoundTripTest.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import io.swagger.v3.core.util.Json;
6+
import io.swagger.v3.core.util.JsonAssert;
67
import io.swagger.v3.core.util.Json31;
78
import io.swagger.v3.core.util.ResourceUtils;
89
import io.swagger.v3.core.util.Yaml;
@@ -70,7 +71,7 @@ public void testRoundTrip30Json() throws IOException {
7071
String jsonAgain = Json.pretty(deserializedOpenAPI);
7172

7273
// Compare JSON strings
73-
assertEquals(json, jsonAgain, "JSON round-trip failed");
74+
JsonAssert.assertJsonEquals(Json.mapper(), json, jsonAgain, "JSON round-trip failed");
7475
}
7576

7677
/**
@@ -106,7 +107,7 @@ public void testRoundTrip30Yaml() throws IOException {
106107
String yamlAgain = Yaml.pretty(deserializedOpenAPI);
107108

108109
// Compare YAML strings
109-
assertEquals(yaml, yamlAgain, "YAML round-trip failed");
110+
JsonAssert.assertJsonEquals(Yaml.mapper(), yaml, yamlAgain, "YAML round-trip failed");
110111
}
111112

112113
/**
@@ -148,7 +149,7 @@ public void testRoundTrip31Json() throws IOException {
148149
String jsonAgain = Json31.pretty(deserializedOpenAPI);
149150

150151
// Compare JSON strings
151-
assertEquals(json, jsonAgain, "JSON round-trip failed");
152+
JsonAssert.assertJsonEquals(Json31.mapper(), json, jsonAgain, "JSON round-trip failed");
152153
}
153154

154155
/**
@@ -190,7 +191,7 @@ public void testRoundTrip31Yaml() throws IOException {
190191
String yamlAgain = Yaml31.pretty(deserializedOpenAPI);
191192

192193
// Compare YAML strings
193-
assertEquals(yaml, yamlAgain, "YAML round-trip failed");
194+
JsonAssert.assertJsonEquals(Yaml31.mapper(), yaml, yamlAgain, "YAML round-trip failed");
194195
}
195196

196197
/**
@@ -242,7 +243,7 @@ public void testComplexRoundTrip31() throws IOException {
242243
String jsonAgain = Json31.pretty(deserializedOpenAPI);
243244

244245
// Compare JSON strings
245-
assertEquals(json, jsonAgain, "JSON round-trip failed");
246+
JsonAssert.assertJsonEquals(Json31.mapper(), json, jsonAgain, "JSON round-trip failed");
246247
}
247248

248249
/**
@@ -276,7 +277,7 @@ public void testBooleanSchemaRoundTrip() throws IOException {
276277
String jsonAgain = Json31.pretty(deserializedOpenAPI);
277278

278279
// Compare JSON strings
279-
assertEquals(json, jsonAgain, "JSON round-trip failed");
280+
JsonAssert.assertJsonEquals(Json.mapper(), json, jsonAgain, "JSON round-trip failed");
280281
}
281282

282283
/**
@@ -307,7 +308,7 @@ public void testNullValuesRoundTrip() throws IOException {
307308
String jsonAgain = Json31.pretty(deserializedOpenAPI);
308309

309310
// Compare JSON strings
310-
assertEquals(json, jsonAgain, "JSON round-trip failed");
311+
JsonAssert.assertJsonEquals(Json.mapper(), json, jsonAgain, "JSON round-trip failed");
311312
}
312313

313314
/**
@@ -350,14 +351,7 @@ public void testRealWorldRoundTrip31() throws IOException {
350351
String yamlAgain = Yaml31.pretty(deserializedOpenAPI);
351352

352353
// Compare YAML strings (normalize whitespace)
353-
assertEquals(normalizeWhitespace(serializedYaml), normalizeWhitespace(yamlAgain), "YAML round-trip failed");
354-
}
355-
356-
/**
357-
* Helper method to normalize whitespace in YAML strings for comparison
358-
*/
359-
private String normalizeWhitespace(String yaml) {
360-
return yaml.replaceAll("\\s+", " ").trim();
354+
JsonAssert.assertJsonEquals(Yaml31.mapper(), serializedYaml, yamlAgain, "YAML round-trip failed");
361355
}
362356

363357
/**

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,6 @@ public void deserializeObjectPropertyWithRequiredProperties() throws IOException
317317
.addProperties("stringProperty", new StringSchema());
318318
p.required(Arrays.asList("stringProperty"));
319319
final String json = "{\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}},\"required\":[\"stringProperty\"]}";
320-
assertEquals(p, m.readValue(json, Schema.class));
320+
JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json);
321321
}
322322
}

modules/swagger-core/src/test/java/io/swagger/v3/core/util/JsonAssert.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,31 @@ public final class JsonAssert {
88
private JsonAssert() {
99
}
1010

11+
/**
12+
* Asserts that two JSON strings are semantically equal, ignoring key order.
13+
* This version uses a default failure message.
14+
*
15+
* @param mapper The ObjectMapper to use for parsing.
16+
* @param expectedJson The expected JSON/YAML string.
17+
* @param actualJson The actual JSON/YAML string.
18+
*/
1119
public static void assertJsonEquals(ObjectMapper mapper, String expectedJson, String actualJson) {
20+
assertJsonEquals(mapper, expectedJson, actualJson, "The JSON structures are not equal.");
21+
}
22+
23+
/**
24+
* This version allows for a custom failure message.
25+
*
26+
* @param mapper The ObjectMapper to use for parsing.
27+
* @param expectedJson The expected JSON/YAML string.
28+
* @param actualJson The actual JSON/YAML string.
29+
* @param message The custom message to display if the assertion fails.
30+
*/
31+
public static void assertJsonEquals(ObjectMapper mapper, String expectedJson, String actualJson, String message) {
1232
try {
1333
JsonNode expectedNode = mapper.readTree(expectedJson);
1434
JsonNode actualNode = mapper.readTree(actualJson);
15-
assertTrue(expectedNode.equals(actualNode));
35+
assertTrue(expectedNode.equals(actualNode), message);
1636
} catch (Exception e) {
1737
throw new RuntimeException(e);
1838
}

0 commit comments

Comments
 (0)