#1980 - Avoid compiling agains JSONArray in JsonPathLinkDiscoverer.
So far, the handling of links detected via a JSON Path expression has assumed that the JsonProvider would always return JSONArray objects for collections. However, the JacksonJsonProvider for example, returns plain List instances. As JSONArray implements List, we now only refer to the latter for maximum compatibility with different JsonProvider implementations.
This commit is contained in:
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package org.springframework.hateoas.client;
|
||||
|
||||
import net.minidev.json.JSONArray;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
@@ -161,13 +159,13 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer {
|
||||
*/
|
||||
private Links createLinksFrom(Object parseResult, LinkRelation rel) {
|
||||
|
||||
if (JSONArray.class.isInstance(parseResult)) {
|
||||
if (List.class.isInstance(parseResult)) {
|
||||
|
||||
JSONArray jsonArray = (JSONArray) parseResult;
|
||||
List<?> list = (List<?>) parseResult;
|
||||
|
||||
return jsonArray.stream() //
|
||||
.flatMap(it -> JSONArray.class.isInstance(it) ? ((JSONArray) it).stream() : Stream.of(it)) //
|
||||
.map(it -> extractLink(it, rel)) //
|
||||
return list.stream()
|
||||
.flatMap(it -> List.class.isInstance(it) ? ((List<?>) it).stream() : Stream.of(it))
|
||||
.map(it -> extractLink(it, rel))
|
||||
.collect(Links.collector());
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.hateoas.mediatype.hal;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
||||
import com.jayway.jsonpath.Configuration;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.jayway.jsonpath.Option;
|
||||
import com.jayway.jsonpath.internal.DefaultsImpl;
|
||||
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
|
||||
import com.jayway.jsonpath.spi.json.JsonProvider;
|
||||
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
|
||||
import com.jayway.jsonpath.spi.mapper.MappingProvider;
|
||||
|
||||
/**
|
||||
* Unit tests to cover {@link HalLinkDiscovererUnitTest}s with a different {@link JsonPath} {@link Configuration}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
class CustomizedHalLinkDiscovererUnitTest extends HalLinkDiscovererUnitTest {
|
||||
|
||||
@BeforeAll
|
||||
static void customizeConfiguration() {
|
||||
|
||||
Configuration.setDefaults(new Configuration.Defaults() {
|
||||
|
||||
private final JsonProvider jsonProvider = new JacksonJsonProvider();
|
||||
private final MappingProvider mappingProvider = new JacksonMappingProvider();
|
||||
|
||||
@Override
|
||||
public JsonProvider jsonProvider() {
|
||||
return jsonProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MappingProvider mappingProvider() {
|
||||
return mappingProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Option> options() {
|
||||
return EnumSet.noneOf(Option.class);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void resetConfiguration() {
|
||||
Configuration.setDefaults(DefaultsImpl.INSTANCE);
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import lombok.Getter;
|
||||
import net.minidev.json.JSONArray;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -600,8 +599,8 @@ class Jackson2HalFormsIntegrationTest {
|
||||
|
||||
Object actual = JsonPath.compile(path).read(json);
|
||||
|
||||
Object value = JSONArray.class.isInstance(actual) //
|
||||
? JSONArray.class.cast(actual).get(0) //
|
||||
Object value = List.class.isInstance(actual) //
|
||||
? List.class.cast(actual).get(0) //
|
||||
: actual;
|
||||
|
||||
assertThat(value).isEqualTo(expected);
|
||||
|
||||
Reference in New Issue
Block a user