Improve null-safety of JsonContentAssert

See gh-46926
This commit is contained in:
Moritz Halbritter
2025-09-16 11:55:16 +02:00
parent 96afc3f944
commit 3bfcfaa795
@@ -1000,9 +1000,11 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
if (this.actual == null) {
return compareForNull(expectedJson);
}
if (expectedJson == null) {
return fail("Expected JSON but got null");
}
try {
return JSONCompare.compareJSON((expectedJson != null) ? expectedJson.toString() : null,
this.actual.toString(), compareMode);
return JSONCompare.compareJSON(expectedJson.toString(), this.actual.toString(), compareMode);
}
catch (Exception ex) {
if (ex instanceof RuntimeException runtimeException) {
@@ -1016,9 +1018,11 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
if (this.actual == null) {
return compareForNull(expectedJson);
}
if (expectedJson == null) {
return fail("Expected JSON but got null");
}
try {
return JSONCompare.compareJSON((expectedJson != null) ? expectedJson.toString() : null,
this.actual.toString(), comparator);
return JSONCompare.compareJSON(expectedJson.toString(), this.actual.toString(), comparator);
}
catch (Exception ex) {
if (ex instanceof RuntimeException runtimeException) {
@@ -1037,6 +1041,12 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
return result;
}
private JSONCompareResult fail(String message) {
JSONCompareResult result = new JSONCompareResult();
result.fail(message);
return result;
}
private JsonContentAssert assertNotFailed(JSONCompareResult result) {
if (result.failed()) {
failWithMessage("JSON Comparison failure: %s", result.getMessage());