diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/SwaggerSerializerTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/SwaggerSerializerTest.java index 491c40f380..81095f4e6d 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/SwaggerSerializerTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/SwaggerSerializerTest.java @@ -5,6 +5,7 @@ import io.swagger.v3.core.matchers.SerializationMatchers; import io.swagger.v3.core.oas.models.Person; import io.swagger.v3.core.util.Json; +import io.swagger.v3.core.util.JsonAssert; import io.swagger.v3.core.util.OutputReplacer; import io.swagger.v3.core.util.ResourceUtils; import io.swagger.v3.oas.models.Components; @@ -172,7 +173,7 @@ public void writeSpecWithParameterReferences() throws IOException { final String swaggerJson = Json.mapper().writeValueAsString(swagger); final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class); - assertEquals(Json.pretty(rebuilt), Json.pretty(swagger)); + JsonAssert.assertJsonEquals(Json.mapper(), Json.pretty(rebuilt), Json.pretty(swagger)); } @Test diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/roundtrip/ComprehensiveRoundTripTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/roundtrip/ComprehensiveRoundTripTest.java index 36c2b3c770..e2ae0ea6a6 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/roundtrip/ComprehensiveRoundTripTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/roundtrip/ComprehensiveRoundTripTest.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.core.util.Json; +import io.swagger.v3.core.util.JsonAssert; import io.swagger.v3.core.util.Json31; import io.swagger.v3.core.util.ResourceUtils; import io.swagger.v3.core.util.Yaml; @@ -70,7 +71,7 @@ public void testRoundTrip30Json() throws IOException { String jsonAgain = Json.pretty(deserializedOpenAPI); // Compare JSON strings - assertEquals(json, jsonAgain, "JSON round-trip failed"); + JsonAssert.assertJsonEquals(Json.mapper(), json, jsonAgain, "JSON round-trip failed"); } /** @@ -106,7 +107,7 @@ public void testRoundTrip30Yaml() throws IOException { String yamlAgain = Yaml.pretty(deserializedOpenAPI); // Compare YAML strings - assertEquals(yaml, yamlAgain, "YAML round-trip failed"); + JsonAssert.assertJsonEquals(Yaml.mapper(), yaml, yamlAgain, "YAML round-trip failed"); } /** @@ -148,7 +149,7 @@ public void testRoundTrip31Json() throws IOException { String jsonAgain = Json31.pretty(deserializedOpenAPI); // Compare JSON strings - assertEquals(json, jsonAgain, "JSON round-trip failed"); + JsonAssert.assertJsonEquals(Json31.mapper(), json, jsonAgain, "JSON round-trip failed"); } /** @@ -190,7 +191,7 @@ public void testRoundTrip31Yaml() throws IOException { String yamlAgain = Yaml31.pretty(deserializedOpenAPI); // Compare YAML strings - assertEquals(yaml, yamlAgain, "YAML round-trip failed"); + JsonAssert.assertJsonEquals(Yaml31.mapper(), yaml, yamlAgain, "YAML round-trip failed"); } /** @@ -242,7 +243,7 @@ public void testComplexRoundTrip31() throws IOException { String jsonAgain = Json31.pretty(deserializedOpenAPI); // Compare JSON strings - assertEquals(json, jsonAgain, "JSON round-trip failed"); + JsonAssert.assertJsonEquals(Json31.mapper(), json, jsonAgain, "JSON round-trip failed"); } /** @@ -276,7 +277,7 @@ public void testBooleanSchemaRoundTrip() throws IOException { String jsonAgain = Json31.pretty(deserializedOpenAPI); // Compare JSON strings - assertEquals(json, jsonAgain, "JSON round-trip failed"); + JsonAssert.assertJsonEquals(Json.mapper(), json, jsonAgain, "JSON round-trip failed"); } /** @@ -307,7 +308,7 @@ public void testNullValuesRoundTrip() throws IOException { String jsonAgain = Json31.pretty(deserializedOpenAPI); // Compare JSON strings - assertEquals(json, jsonAgain, "JSON round-trip failed"); + JsonAssert.assertJsonEquals(Json.mapper(), json, jsonAgain, "JSON round-trip failed"); } /** @@ -350,14 +351,7 @@ public void testRealWorldRoundTrip31() throws IOException { String yamlAgain = Yaml31.pretty(deserializedOpenAPI); // Compare YAML strings (normalize whitespace) - assertEquals(normalizeWhitespace(serializedYaml), normalizeWhitespace(yamlAgain), "YAML round-trip failed"); - } - - /** - * Helper method to normalize whitespace in YAML strings for comparison - */ - private String normalizeWhitespace(String yaml) { - return yaml.replaceAll("\\s+", " ").trim(); + JsonAssert.assertJsonEquals(Yaml31.mapper(), serializedYaml, yamlAgain, "YAML round-trip failed"); } /** diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java index 3025b23a4a..3cd5ea4022 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/properties/PropertySerializationTest.java @@ -317,6 +317,6 @@ public void deserializeObjectPropertyWithRequiredProperties() throws IOException .addProperties("stringProperty", new StringSchema()); p.required(Arrays.asList("stringProperty")); final String json = "{\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}},\"required\":[\"stringProperty\"]}"; - assertEquals(p, m.readValue(json, Schema.class)); + JsonAssert.assertJsonEquals(m, m.writeValueAsString(p), json); } } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/util/JsonAssert.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/util/JsonAssert.java index 72a1870ced..f9ff7c2047 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/util/JsonAssert.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/util/JsonAssert.java @@ -8,11 +8,31 @@ public final class JsonAssert { private JsonAssert() { } + /** + * Asserts that two JSON strings are semantically equal, ignoring key order. + * This version uses a default failure message. + * + * @param mapper The ObjectMapper to use for parsing. + * @param expectedJson The expected JSON/YAML string. + * @param actualJson The actual JSON/YAML string. + */ public static void assertJsonEquals(ObjectMapper mapper, String expectedJson, String actualJson) { + assertJsonEquals(mapper, expectedJson, actualJson, "The JSON structures are not equal."); + } + + /** + * This version allows for a custom failure message. + * + * @param mapper The ObjectMapper to use for parsing. + * @param expectedJson The expected JSON/YAML string. + * @param actualJson The actual JSON/YAML string. + * @param message The custom message to display if the assertion fails. + */ + public static void assertJsonEquals(ObjectMapper mapper, String expectedJson, String actualJson, String message) { try { JsonNode expectedNode = mapper.readTree(expectedJson); JsonNode actualNode = mapper.readTree(actualJson); - assertTrue(expectedNode.equals(actualNode)); + assertTrue(expectedNode.equals(actualNode), message); } catch (Exception e) { throw new RuntimeException(e); }