Fix JsonContentAssert type safety warnings

Previously, JsonContentAssert returns AbstractMapAssert from
extractingJsonPathMapValue. This could lead to type safety warnings
when calling one of the assert's methods with a generic varargs
parameter such as
contains(Entry<? extends Object, ? extends Object>...).

This commit replaces the use of both AbstractMapAssert and
AbstractListAssert with MapAssert and ListAssert respectively. These
classes use final methods and @SafeVargs args to prevent the
above-described problem from occurring.

See gh-9675
pull/9828/head
Mihhail Lapushkin 7 years ago committed by Andy Wilkinson
parent 15410a406e
commit ae91c8460f

@ -26,12 +26,11 @@ import com.jayway.jsonpath.JsonPath;
import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractBooleanAssert; import org.assertj.core.api.AbstractBooleanAssert;
import org.assertj.core.api.AbstractCharSequenceAssert; import org.assertj.core.api.AbstractCharSequenceAssert;
import org.assertj.core.api.AbstractListAssert;
import org.assertj.core.api.AbstractMapAssert;
import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.Assert; import org.assertj.core.api.Assert;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.assertj.core.api.ObjectAssert; import org.assertj.core.api.ListAssert;
import org.assertj.core.api.MapAssert;
import org.skyscreamer.jsonassert.JSONCompare; import org.skyscreamer.jsonassert.JSONCompare;
import org.skyscreamer.jsonassert.JSONCompareMode; import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult; import org.skyscreamer.jsonassert.JSONCompareResult;
@ -947,11 +946,12 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
* @param expression the {@link JsonPath} expression * @param expression the {@link JsonPath} expression
* @param args arguments to parameterize the {@code JsonPath} expression with, using * @param args arguments to parameterize the {@code JsonPath} expression with, using
* formatting specifiers defined in {@link String#format(String, Object...)} * formatting specifiers defined in {@link String#format(String, Object...)}
* @param <E> element type
* @return a new assertion object whose object under test is the extracted item * @return a new assertion object whose object under test is the extracted item
* @throws AssertionError if the path is not valid or does not result in an array * @throws AssertionError if the path is not valid or does not result in an array
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public AbstractListAssert<?, ?, Object, ObjectAssert<Object>> extractingJsonPathArrayValue( public <E> ListAssert<E> extractingJsonPathArrayValue(
CharSequence expression, Object... args) { CharSequence expression, Object... args) {
return Assertions.assertThat( return Assertions.assertThat(
extractingJsonPathValue(expression, args, List.class, "an array")); extractingJsonPathValue(expression, args, List.class, "an array"));
@ -961,12 +961,14 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
* Extract the map value at the given JSON path for further object assertions. * Extract the map value at the given JSON path for further object assertions.
* @param expression the {@link JsonPath} expression * @param expression the {@link JsonPath} expression
* @param args arguments to parameterize the {@code JsonPath} expression with, using * @param args arguments to parameterize the {@code JsonPath} expression with, using
* @param <K> key type
* @param <V> value type
* formatting specifiers defined in {@link String#format(String, Object...)} * formatting specifiers defined in {@link String#format(String, Object...)}
* @return a new assertion object whose object under test is the extracted item * @return a new assertion object whose object under test is the extracted item
* @throws AssertionError if the path is not valid or does not result in a map * @throws AssertionError if the path is not valid or does not result in a map
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public AbstractMapAssert<?, ?, Object, Object> extractingJsonPathMapValue( public <K, V> MapAssert<K, V> extractingJsonPathMapValue(
CharSequence expression, Object... args) { CharSequence expression, Object... args) {
return Assertions.assertThat( return Assertions.assertThat(
extractingJsonPathValue(expression, args, Map.class, "a map")); extractingJsonPathValue(expression, args, Map.class, "a map"));

Loading…
Cancel
Save