Skip to content

Commit 71ec56e

Browse files
authored
Fix issue #680 and two testcase (#704)
* Fix issue 680 and two testcase * additional testcase * adapt jdk version * modify something wrong * a more elegant implementation
1 parent 938e48f commit 71ec56e

5 files changed

Lines changed: 89 additions & 13 deletions

File tree

json-path/src/main/java/com/jayway/jsonpath/Configuration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,14 @@ public interface Defaults {
254254
MappingProvider mappingProvider();
255255

256256
}
257+
258+
@Override
259+
public boolean equals(Object o) {
260+
if (this == o) return true;
261+
if (o == null || getClass() != o.getClass()) return false;
262+
Configuration that = (Configuration) o;
263+
return jsonProvider.getClass() == that.jsonProvider.getClass() &&
264+
mappingProvider.getClass() == that.mappingProvider.getClass() &&
265+
Objects.equals(options, that.options);
266+
}
257267
}

json-path/src/main/java/com/jayway/jsonpath/internal/function/Parameter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public void setJson(String json) {
7070
this.json = json;
7171
}
7272

73+
public ILateBindingValue getILateBingValue(){
74+
return lateBinding;
75+
}
76+
7377
/**
7478
* Translate the collection of parameters into a collection of values of type T.
7579
*

json-path/src/main/java/com/jayway/jsonpath/internal/function/latebinding/PathLateBindingValue.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.jayway.jsonpath.Configuration;
1818
import com.jayway.jsonpath.internal.Path;
1919

20+
import java.util.Objects;
21+
2022
/**
2123
* Defines the contract for late bindings, provides document state and enough context to perform the evaluation at a later
2224
* date such that we can operate on a dynamically changing value.
@@ -26,21 +28,31 @@
2628
*/
2729
public class PathLateBindingValue implements ILateBindingValue {
2830
private final Path path;
29-
private final Object rootDocument;
31+
private final String rootDocument;
3032
private final Configuration configuration;
31-
33+
private final Object result;
3234
public PathLateBindingValue(final Path path, final Object rootDocument, final Configuration configuration) {
3335
this.path = path;
34-
this.rootDocument = rootDocument;
36+
this.rootDocument = rootDocument.toString();
3537
this.configuration = configuration;
38+
this.result = path.evaluate(rootDocument, rootDocument, configuration).getValue();
3639
}
3740

3841
/**
39-
* Evaluate the expression at the point of need for Path type expressions
4042
*
4143
* @return the late value
4244
*/
4345
public Object get() {
44-
return path.evaluate(rootDocument, rootDocument, configuration).getValue();
46+
return result;
47+
}
48+
49+
@Override
50+
public boolean equals(Object o) {
51+
if (this == o) return true;
52+
if (o == null || getClass() != o.getClass()) return false;
53+
PathLateBindingValue that = (PathLateBindingValue) o;
54+
return Objects.equals(path, that.path) &&
55+
rootDocument.equals(that.rootDocument) &&
56+
Objects.equals(configuration, that.configuration);
4557
}
4658
}

json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,20 @@ private void evaluateParameters(String currentPath, PathRef parent, Object model
4848

4949
if (null != functionParams) {
5050
for (Parameter param : functionParams) {
51-
if (!param.hasEvaluated()) {
52-
switch (param.getType()) {
53-
case PATH:
54-
param.setLateBinding(new PathLateBindingValue(param.getPath(), ctx.rootDocument(), ctx.configuration()));
51+
switch (param.getType()) {
52+
case PATH:
53+
PathLateBindingValue pathLateBindingValue = new PathLateBindingValue(param.getPath(), ctx.rootDocument(), ctx.configuration());
54+
if (!param.hasEvaluated()||!pathLateBindingValue.equals(param.getILateBingValue())) {
55+
param.setLateBinding(pathLateBindingValue);
5556
param.setEvaluated(true);
56-
break;
57-
case JSON:
57+
}
58+
break;
59+
case JSON:
60+
if (!param.hasEvaluated()) {
5861
param.setLateBinding(new JsonLateBindingValue(ctx.configuration().jsonProvider(), param));
5962
param.setEvaluated(true);
60-
break;
61-
}
63+
}
64+
break;
6265
}
6366
}
6467
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.jayway.jsonpath.internal.function;
2+
3+
import com.jayway.jsonpath.Configuration;
4+
import com.jayway.jsonpath.JsonPath;
5+
import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider;
6+
import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider;
7+
import org.junit.Test;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
public class Issue680 {
15+
16+
@Test
17+
public void testIssue680concat() {
18+
String json = "{ \"key\": \"first\"}";
19+
Object value = JsonPath.read(json, "concat(\"/\", $.key)");
20+
assertThat(value).isEqualTo("/first");
21+
json = "{ \"key\": \"second\"}";
22+
value = JsonPath.read(json, "concat(\"/\", $.key)");
23+
assertThat(value).isEqualTo("/second");
24+
}
25+
26+
@Test
27+
public void testIssue680min() {
28+
String json = "{ \"key\": 1}";
29+
double value = JsonPath.read(json, "min($.key)");
30+
assertThat(value).isEqualTo(1d);
31+
json = "{ \"key\": 2}";
32+
value = JsonPath.read(json, "min($.key)");
33+
assertThat(value).isEqualTo(2d);
34+
}
35+
36+
@Test
37+
public void testIssue680concat_2() {
38+
Map<String, String> context = new HashMap<String, String>();
39+
context.put("key", "first");
40+
Object value = JsonPath.read(context, "concat(\"/\", $.key)");
41+
assertThat(value).isEqualTo("/first");
42+
Map<String, String> context2 = new HashMap<String, String>();
43+
context2.put("key", "second");
44+
value = JsonPath.read(context2, "concat(\"/\", $.key)");
45+
assertThat(value).isEqualTo("/second");
46+
}
47+
}

0 commit comments

Comments
 (0)