Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down Expand Up @@ -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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading