Skip to content

Commit 792f9d7

Browse files
cronikmymarche
authored andcommitted
fix: display value json input
Computing the display value should retain the original value type of the value. For example if the value is of type string but the contents of the string resemble a primitive value the some information may get lost. Given an value list of strings `["2024.20", "2024.0"]` the json string passed to `parseDisplayValue` would be the the values `2024.20` and `2024.0`, which when re-parsed as json values will be interpreted as numbers rather than strings. This has the end effect of the display values `2024.2` and `2024` while the associated values are `2024.20` and `2024.0`. To fix this behavior each value is "json stringified" before sent to `parseDisplayValue`. For the example above the values would be `"2024.20"` and `"2024.0"`.
1 parent a88b356 commit 792f9d7

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

src/main/java/io/jenkins/plugins/restlistparam/logic/ValueResolver.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.jenkins.plugins.restlistparam.model.MimeType;
66
import io.jenkins.plugins.restlistparam.model.ResultContainer;
77
import io.jenkins.plugins.restlistparam.model.ValueItem;
8+
import org.json.JSONStringer;
89
import org.w3c.dom.Document;
910
import org.w3c.dom.NodeList;
1011
import org.xml.sax.InputSource;
@@ -125,8 +126,7 @@ public static ResultContainer<List<ValueItem>> resolveJsonPath(final String json
125126
resolved.stream()
126127
.map(JsonPath::parse)
127128
.map(context -> context.read("$"))
128-
.map(ValueResolver::convertToString)
129-
.map(value -> new ValueItem(value, parseDisplayValue(value, displayExpression)))
129+
.map(value -> new ValueItem(convertToString(value), parseDisplayValue(convertToJson(value), displayExpression)))
130130
.collect(Collectors.toList())
131131
);
132132
}
@@ -155,8 +155,12 @@ public static ResultContainer<List<ValueItem>> resolveJsonPath(final String json
155155
return container;
156156
}
157157

158+
private static String convertToJson(Object obj) {
159+
return JSONStringer.valueToString(obj);
160+
}
161+
158162
public static String convertToString(Object obj) {
159-
if (obj instanceof Map) {
163+
if (obj instanceof Map || obj instanceof List) {
160164
return JsonPath.parse(obj).jsonString();
161165
}
162166
else if (obj instanceof Integer) {
@@ -165,6 +169,12 @@ else if (obj instanceof Integer) {
165169
else if (obj instanceof Float) {
166170
return Float.toString((Float) obj);
167171
}
172+
else if (obj instanceof Double) {
173+
return Double.toString((Double) obj);
174+
}
175+
else if (obj instanceof Boolean) {
176+
return Boolean.toString((Boolean) obj);
177+
}
168178
else if (obj instanceof String) {
169179
return (String) obj;
170180
}

src/test/java/io/jenkins/plugins/restlistparam/TestConst.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,48 @@ public class TestConst {
3434
" }\n" +
3535
"]\n";
3636

37+
public static final String numberLikeTestJson = "[\n" +
38+
" {\n" +
39+
" \"name\": \"2024.19\",\n" +
40+
" },\n" +
41+
" {\n" +
42+
" \"name\": \"2024.20\",\n" +
43+
" },\n" +
44+
" {\n" +
45+
" \"name\": \"2024.0\",\n" +
46+
" }\n" +
47+
"]\n";
48+
49+
public static final String numberValuesTestJson = "[\n" +
50+
" {\n" +
51+
" \"value\": 1.0\n" +
52+
" },\n" +
53+
" {\n" +
54+
" \"value\": 10\n" +
55+
" },\n" +
56+
" {\n" +
57+
" \"value\": 11.50\n" +
58+
" }\n" +
59+
"]\n";
60+
61+
public static final String mixedTypeValuesTestJson = "[\n" +
62+
" {\n" +
63+
" \"value\": 1.0\n" +
64+
" },\n" +
65+
" {\n" +
66+
" \"value\": 10\n" +
67+
" },\n" +
68+
" {\n" +
69+
" \"value\": \"11.50\"\n" +
70+
" },\n" +
71+
" {\n" +
72+
" \"value\": true\n" +
73+
" },\n" +
74+
" {\n" +
75+
" \"value\": false\n" +
76+
" }\n" +
77+
"]\n";
78+
3779
public static final String validJsonValueItem = "{" +
3880
"\"name\":\"v10.6.4\"," +
3981
"\"zipball_url\":\"https://api.github.com/repos/jellyfin/jellyfin/zipball/v10.6.4\"," +

src/test/java/io/jenkins/plugins/restlistparam/ValueResolverTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.Assert;
99
import org.junit.Test;
1010

11+
import java.util.HashMap;
1112
import java.util.List;
1213

1314
public class ValueResolverTest {
@@ -20,6 +21,46 @@ public void resolveJsonPathTest() {
2021
Assert.assertEquals(3, res.getValue().size());
2122
Assert.assertArrayEquals(new String[]{"v10.6.4", "v10.6.3", "v10.6.2"},
2223
res.getValue().stream().map(ValueItem::getValue).toArray());
24+
Assert.assertArrayEquals(new String[]{"v10.6.4", "v10.6.3", "v10.6.2"},
25+
res.getValue().stream().map(ValueItem::getDisplayValue).toArray());
26+
}
27+
28+
@Test
29+
public void resolveJsonPathNumberLikeValuesTest() {
30+
ResultContainer<List<ValueItem>> res = ValueResolver.resolveJsonPath(TestConst.numberLikeTestJson, "$.*.name", "$");
31+
Assert.assertNotNull(res);
32+
Assert.assertFalse(res.getErrorMsg().isPresent());
33+
Assert.assertEquals(3, res.getValue().size());
34+
Assert.assertArrayEquals(new String[]{"2024.19", "2024.20", "2024.0"},
35+
res.getValue().stream().map(ValueItem::getValue).toArray());
36+
Assert.assertArrayEquals(new String[]{"2024.19", "2024.20", "2024.0"},
37+
res.getValue().stream().map(ValueItem::getDisplayValue).toArray());
38+
}
39+
40+
@Test
41+
public void resolveJsonPathNumberValuesTest() {
42+
ResultContainer<List<ValueItem>> res = ValueResolver.resolveJsonPath(TestConst.numberValuesTestJson, "$.*.value", "$");
43+
Assert.assertNotNull(res);
44+
Assert.assertFalse(res.getErrorMsg().isPresent());
45+
Assert.assertEquals(3, res.getValue().size());
46+
Assert.assertArrayEquals(new String[]{"1.0", "10", "11.5"},
47+
res.getValue().stream().map(ValueItem::getValue).toArray());
48+
// trailing 0 and decimal points are shaved off numbers by JSONStringer
49+
Assert.assertArrayEquals(new String[]{"1", "10", "11.5"},
50+
res.getValue().stream().map(ValueItem::getDisplayValue).toArray());
51+
}
52+
53+
@Test
54+
public void resolveJsonPathMixedTypeValuesTest() {
55+
ResultContainer<List<ValueItem>> res = ValueResolver.resolveJsonPath(TestConst.mixedTypeValuesTestJson, "$.*.value", "$");
56+
Assert.assertNotNull(res);
57+
Assert.assertFalse(res.getErrorMsg().isPresent());
58+
Assert.assertEquals(5, res.getValue().size());
59+
Assert.assertArrayEquals(new String[]{"1.0", "10", "11.50", "true", "false"},
60+
res.getValue().stream().map(ValueItem::getValue).toArray());
61+
// trailing 0 and decimal points are shaved off numbers by JSONStringer
62+
Assert.assertArrayEquals(new String[]{"1", "10", "11.50", "true", "false"},
63+
res.getValue().stream().map(ValueItem::getDisplayValue).toArray());
2364
}
2465

2566
@Test

0 commit comments

Comments
 (0)