Skip to content

Commit

Permalink
#1985 - Avoid compiling agains JSONArray in JsonPathLinkDiscoverer.
Browse files Browse the repository at this point in the history
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.

Original ticket: #1980
  • Loading branch information
odrotbohm committed Jun 29, 2023
1 parent 9157958 commit 3e9c90d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,13 +159,13 @@ private JsonPath getExpression(LinkRelation rel) {
*/
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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -600,8 +599,8 @@ private void assertValueForPath(Object toMarshall, String path, Object expected)

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);
Expand Down

0 comments on commit 3e9c90d

Please sign in to comment.