mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-26 15:21:41 +01:00
[jsonpath] Upgrade dependencies and some cleanup (#16365)
* Upgrade json-path from 2.5.0 to 2.9.0 * Cleanup code * Fix all SAT findings Fixes CVE-2023-1370 and CVE-2023-51074 For json-path release notes, see: https://github.com/json-path/JsonPath/releases Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
parent
235abce531
commit
3ce7fa326f
@ -15,32 +15,39 @@
|
|||||||
<name>openHAB Add-ons :: Bundles :: Transformation Service :: JSonPath</name>
|
<name>openHAB Add-ons :: Bundles :: Transformation Service :: JSonPath</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<bnd.importpackage>!org.apache.tapestry5.json.*,!org.codehaus.jettison.json.*,!org.json.*,!com.fasterxml.jackson.*</bnd.importpackage>
|
<bnd.importpackage>!org.apache.tapestry5.json.*,!org.codehaus.jettison.json.*,!org.json.*,!com.fasterxml.jackson.*,!jakarta.json.*</bnd.importpackage>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.jayway.jsonpath</groupId>
|
<groupId>com.jayway.jsonpath</groupId>
|
||||||
<artifactId>json-path</artifactId>
|
<artifactId>json-path</artifactId>
|
||||||
<version>2.5.0</version>
|
<version>2.9.0</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<!-- Exclude slf4j-api to prevent its scope being changed from 'provided' to 'runtime' -->
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.ow2.asm</groupId>
|
<groupId>org.ow2.asm</groupId>
|
||||||
<artifactId>asm</artifactId>
|
<artifactId>asm</artifactId>
|
||||||
<version>5.0.4</version>
|
<version>9.3</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.minidev</groupId>
|
<groupId>net.minidev</groupId>
|
||||||
<artifactId>accessors-smart</artifactId>
|
<artifactId>accessors-smart</artifactId>
|
||||||
<version>1.2</version>
|
<version>2.5.0</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.minidev</groupId>
|
<groupId>net.minidev</groupId>
|
||||||
<artifactId>json-smart</artifactId>
|
<artifactId>json-smart</artifactId>
|
||||||
<version>2.3</version>
|
<version>2.5.0</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -30,12 +30,10 @@ import com.jayway.jsonpath.JsonPath;
|
|||||||
import com.jayway.jsonpath.PathNotFoundException;
|
import com.jayway.jsonpath.PathNotFoundException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* The implementation of a {@link TransformationService} which transforms the input by JSonPath Expressions.
|
||||||
* The implementation of {@link TransformationService} which transforms the input by JSonPath Expressions.
|
|
||||||
*
|
|
||||||
* @author Gaël L'hopital
|
|
||||||
* @author Sebastian Janzen
|
|
||||||
*
|
*
|
||||||
|
* @author Gaël L'hopital - Initial contribution
|
||||||
|
* @author Sebastian Janzen - Initial contribution
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
@Component(property = { "openhab.transform=JSONPATH" })
|
@Component(property = { "openhab.transform=JSONPATH" })
|
||||||
@ -68,7 +66,7 @@ public class JSonPathTransformationService implements TransformationService {
|
|||||||
logger.debug("transformation resulted in '{}'", transformationResult);
|
logger.debug("transformation resulted in '{}'", transformationResult);
|
||||||
if (transformationResult == null) {
|
if (transformationResult == null) {
|
||||||
return null;
|
return null;
|
||||||
} else if (transformationResult instanceof List list) {
|
} else if (transformationResult instanceof List<?> list) {
|
||||||
return flattenList(list);
|
return flattenList(list);
|
||||||
} else {
|
} else {
|
||||||
return transformationResult.toString();
|
return transformationResult.toString();
|
||||||
@ -98,10 +96,10 @@ public class JSonPathTransformationService implements TransformationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String createNumberList(List<?> list) {
|
private String createNumberList(List<?> list) {
|
||||||
return list.stream().map(n -> String.valueOf(n)).collect(Collectors.joining(", ", "[", "]"));
|
return list.stream().map(String::valueOf).collect(Collectors.joining(", ", "[", "]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createStringList(List<?> list) {
|
private String createStringList(List<?> list) {
|
||||||
return list.stream().map(n -> "\"" + String.valueOf(n) + "\"").collect(Collectors.joining(", ", "[", "]"));
|
return list.stream().map(n -> "\"" + n + "\"").collect(Collectors.joining(", ", "[", "]"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,8 +30,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
/**
|
/**
|
||||||
* Profile to offer the JSonPathTransformationservice on an ItemChannelLink
|
* Profile to offer the JSonPathTransformationservice on an ItemChannelLink
|
||||||
*
|
*
|
||||||
* @author Stefan Triller - initial contribution
|
* @author Stefan Triller - Initial contribution
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class JSonPathTransformationProfile implements StateProfile {
|
public class JSonPathTransformationProfile implements StateProfile {
|
||||||
@ -47,10 +46,8 @@ public class JSonPathTransformationProfile implements StateProfile {
|
|||||||
private static final String FUNCTION_PARAM = "function";
|
private static final String FUNCTION_PARAM = "function";
|
||||||
private static final String SOURCE_FORMAT_PARAM = "sourceFormat";
|
private static final String SOURCE_FORMAT_PARAM = "sourceFormat";
|
||||||
|
|
||||||
@NonNullByDefault({})
|
private final @NonNullByDefault({}) String function;
|
||||||
private final String function;
|
private final @NonNullByDefault({}) String sourceFormat;
|
||||||
@NonNullByDefault({})
|
|
||||||
private final String sourceFormat;
|
|
||||||
|
|
||||||
public JSonPathTransformationProfile(ProfileCallback callback, ProfileContext context,
|
public JSonPathTransformationProfile(ProfileCallback callback, ProfileContext context,
|
||||||
TransformationService service) {
|
TransformationService service) {
|
||||||
|
@ -12,8 +12,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.transform.jsonpath.internal.profiles;
|
package org.openhab.transform.jsonpath.internal.profiles;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
@ -31,21 +31,19 @@ import org.osgi.service.component.annotations.Component;
|
|||||||
import org.osgi.service.component.annotations.Reference;
|
import org.osgi.service.component.annotations.Reference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Profilefactory that creates the transformation profile for the jsonpath transformation service
|
* {@link ProfileFactory} that creates the transformation profile for the jsonpath transformation service
|
||||||
*
|
|
||||||
* @author Stefan Triller - initial contribution
|
|
||||||
*
|
*
|
||||||
|
* @author Stefan Triller - Initial contribution
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
@Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
|
@Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
|
||||||
public class JSonPathTransformationProfileFactory implements ProfileFactory, ProfileTypeProvider {
|
public class JSonPathTransformationProfileFactory implements ProfileFactory, ProfileTypeProvider {
|
||||||
|
|
||||||
@NonNullByDefault({})
|
private @NonNullByDefault({}) TransformationService service;
|
||||||
private TransformationService service;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
|
public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
|
||||||
return Arrays.asList(ProfileTypeBuilder.newState(JSonPathTransformationProfile.PROFILE_TYPE_UID,
|
return List.of(ProfileTypeBuilder.newState(JSonPathTransformationProfile.PROFILE_TYPE_UID,
|
||||||
JSonPathTransformationProfile.PROFILE_TYPE_UID.getId()).build());
|
JSonPathTransformationProfile.PROFILE_TYPE_UID.getId()).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +55,7 @@ public class JSonPathTransformationProfileFactory implements ProfileFactory, Pro
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
|
public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
|
||||||
return Arrays.asList(JSonPathTransformationProfile.PROFILE_TYPE_UID);
|
return List.of(JSonPathTransformationProfile.PROFILE_TYPE_UID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Reference(target = "(openhab.transform=JSONPATH)")
|
@Reference(target = "(openhab.transform=JSONPATH)")
|
||||||
|
@ -1 +0,0 @@
|
|||||||
Bundle resources go in here!
|
|
@ -14,6 +14,7 @@ package org.openhab.transform.jsonpath.internal;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.openhab.core.transform.TransformationException;
|
import org.openhab.core.transform.TransformationException;
|
||||||
@ -21,9 +22,10 @@ import org.openhab.core.transform.TransformationException;
|
|||||||
/**
|
/**
|
||||||
* @author Gaël L'hopital - Initial contribution
|
* @author Gaël L'hopital - Initial contribution
|
||||||
*/
|
*/
|
||||||
|
@NonNullByDefault
|
||||||
public class JSonPathTransformationServiceTest {
|
public class JSonPathTransformationServiceTest {
|
||||||
|
|
||||||
private JSonPathTransformationService processor;
|
private @NonNullByDefault({}) JSonPathTransformationService processor;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void init() {
|
public void init() {
|
||||||
@ -40,59 +42,59 @@ public class JSonPathTransformationServiceTest {
|
|||||||
assertEquals("Nigel Rees", transformedResponse);
|
assertEquals("Nigel Rees", transformedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String jsonArray = "[" + //
|
private static final String JSON_ARRAY = "[" + //
|
||||||
"{ \"id\":1, \"name\":\"bob\", \"empty\":null }," + //
|
"{ \"id\":1, \"name\":\"bob\", \"empty\":null }," + //
|
||||||
"{ \"id\":2, \"name\":\"alice\" }" + //
|
"{ \"id\":2, \"name\":\"alice\" }" + //
|
||||||
"]";
|
"]";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidPath1() throws TransformationException {
|
public void testValidPath1() throws TransformationException {
|
||||||
String transformedResponse = processor.transform("$[0].name", jsonArray);
|
String transformedResponse = processor.transform("$[0].name", JSON_ARRAY);
|
||||||
assertEquals("bob", transformedResponse);
|
assertEquals("bob", transformedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidPath2() throws TransformationException {
|
public void testValidPath2() throws TransformationException {
|
||||||
String transformedResponse = processor.transform("$[1].id", jsonArray);
|
String transformedResponse = processor.transform("$[1].id", JSON_ARRAY);
|
||||||
assertEquals("2", transformedResponse);
|
assertEquals("2", transformedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidPathThrowsException() {
|
public void testInvalidPathThrowsException() {
|
||||||
assertThrows(TransformationException.class, () -> processor.transform("$$", jsonArray));
|
assertThrows(TransformationException.class, () -> processor.transform("$$", JSON_ARRAY));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPathMismatchReturnNull() {
|
public void testPathMismatchReturnNull() {
|
||||||
assertThrows(TransformationException.class, () -> processor.transform("$[5].id", jsonArray));
|
assertThrows(TransformationException.class, () -> processor.transform("$[5].id", JSON_ARRAY));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidJsonReturnNull() throws TransformationException {
|
public void testInvalidJsonReturnNull() {
|
||||||
assertThrows(TransformationException.class, () -> processor.transform("$", "{id:"));
|
assertThrows(TransformationException.class, () -> processor.transform("$", "{id:"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNullValue() throws TransformationException {
|
public void testNullValue() throws TransformationException {
|
||||||
String transformedResponse = processor.transform("$[0].empty", jsonArray);
|
String transformedResponse = processor.transform("$[0].empty", JSON_ARRAY);
|
||||||
assertEquals(null, transformedResponse);
|
assertNull(transformedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIndefinite_filteredToSingle() throws TransformationException {
|
public void testIndefiniteFilteredToSingle() throws TransformationException {
|
||||||
String transformedResponse = processor.transform("$.*[?(@.name=='bob')].id", jsonArray);
|
String transformedResponse = processor.transform("$.*[?(@.name=='bob')].id", JSON_ARRAY);
|
||||||
assertEquals("1", transformedResponse);
|
assertEquals("1", transformedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIndefinite_notFiltered() throws TransformationException {
|
public void testIndefiniteNotFiltered() throws TransformationException {
|
||||||
String transformedResponse = processor.transform("$.*.id", jsonArray);
|
String transformedResponse = processor.transform("$.*.id", JSON_ARRAY);
|
||||||
assertEquals("[1, 2]", transformedResponse);
|
assertEquals("[1, 2]", transformedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIndefinite_noMatch() throws TransformationException {
|
public void testIndefiniteNoMatch() throws TransformationException {
|
||||||
String transformedResponse = processor.transform("$.*[?(@.name=='unknown')].id", jsonArray);
|
String transformedResponse = processor.transform("$.*[?(@.name=='unknown')].id", JSON_ARRAY);
|
||||||
assertEquals("NULL", transformedResponse);
|
assertEquals("NULL", transformedResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user