Skip to content

Commit d601735

Browse files
committed
Search API changes for JsonPaths
Signed-off-by: kiray <[email protected]>
1 parent ba76615 commit d601735

File tree

2 files changed

+116
-3
lines changed

2 files changed

+116
-3
lines changed

xyz-hub-test/src/test/java/com/here/xyz/hub/rest/ApiParamTest.java

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2019 HERE Europe B.V.
2+
* Copyright (C) 2017-2025 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
package com.here.xyz.hub.rest;
2121

2222
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertTrue;
2324

2425
import com.here.xyz.events.PropertiesQuery;
2526
import com.here.xyz.events.PropertyQuery;
@@ -69,7 +70,6 @@ public void parsePropertiesQuery() {
6970
assertEquals(2, query.getValues().size());
7071
assertEquals("string", query.getValues().get(0));
7172
assertEquals("5", query.getValues().get(1));
72-
7373
}
7474

7575
@Test
@@ -148,4 +148,93 @@ public void parsePropertiesQuerySpace() {
148148
assertEquals(1, query.getValues().size());
149149
assertEquals(3L, query.getValues().get(0));
150150
}
151+
152+
@Test
153+
public void parsePropertiesQueryJsonPath() {
154+
String URIquery = "p.jsonPath=$.properties.address.city";
155+
PropertiesQuery pq = PropertiesQuery.fromString(URIquery, "", false);
156+
157+
assertEquals("1 OR block is expected", 1, pq.size());
158+
PropertyQueryList pql = pq.get(0);
159+
assertEquals("1 AND block is expected.", 1, pql.size());
160+
161+
PropertyQuery query = pql.stream()
162+
.filter(q -> q.getKey().equals("properties.jsonPath"))
163+
.findFirst()
164+
.get();
165+
166+
assertEquals(QueryOperation.EQUALS, query.getOperation());
167+
assertEquals(1, query.getValues().size());
168+
Object value = query.getValues().get(0);
169+
assertTrue("JSONPath value should be a String", value instanceof String);
170+
assertEquals("$.properties.address.city", value);
171+
}
172+
173+
@Test
174+
public void parsePropertiesQueryJsonPathWithOtherFilters() {
175+
String URIquery =
176+
"p.jsonPath=$.properties.address.city"
177+
+ "&p.a=3"
178+
+ "&p.boolean=true"
179+
+ "&f.createdAt>0";
180+
181+
PropertiesQuery pq = PropertiesQuery.fromString(URIquery, "", false);
182+
assertEquals("1 OR block is expected", 1, pq.size());
183+
184+
PropertyQueryList pql = pq.get(0);
185+
// 4 AND blocks: jsonPath, a, boolean, createdAt
186+
assertEquals("4 AND blocks are expected.", 4, pql.size());
187+
188+
// jsonPath
189+
PropertyQuery query = pql.stream()
190+
.filter(q -> q.getKey().equals("properties.jsonPath"))
191+
.findFirst()
192+
.get();
193+
assertEquals(QueryOperation.EQUALS, query.getOperation());
194+
assertEquals(1, query.getValues().size());
195+
assertTrue(query.getValues().get(0) instanceof String);
196+
assertEquals("$.properties.address.city", query.getValues().get(0));
197+
198+
// properties.a (still parsed as number)
199+
query = pql.stream().filter(q -> q.getKey().equals("properties.a")).findFirst().get();
200+
assertEquals(QueryOperation.EQUALS, query.getOperation());
201+
assertEquals(1, query.getValues().size());
202+
assertEquals(3L, query.getValues().get(0));
203+
204+
// properties.boolean (still parsed as boolean)
205+
query = pql.stream().filter(q -> q.getKey().equals("properties.boolean")).findFirst().get();
206+
assertEquals(QueryOperation.EQUALS, query.getOperation());
207+
assertEquals(1, query.getValues().size());
208+
assertEquals(true, query.getValues().get(0));
209+
210+
// createdAt (mapped via SEARCH_KEY_REPLACEMENTS)
211+
query = pql.stream()
212+
.filter(q -> q.getKey().equals("properties.@ns:com:here:xyz.createdAt"))
213+
.findFirst()
214+
.get();
215+
assertEquals(QueryOperation.GREATER_THAN, query.getOperation());
216+
assertEquals(1, query.getValues().size());
217+
assertEquals(0L, query.getValues().get(0));
218+
}
219+
220+
@Test
221+
public void parsePropertiesQuerySpaceJsonPath() {
222+
String URISpaceQuery = "a=1&b=2&contentUpatedAt=$.space.properties.version";
223+
PropertiesQuery pq = PropertiesQuery.fromString(URISpaceQuery, "contentUpatedAt", true);
224+
225+
assertEquals("1 OR block is expected", 1, pq.size());
226+
PropertyQueryList pql = pq.get(0);
227+
assertEquals("1 AND block is expected.", 1, pql.size());
228+
229+
PropertyQuery query = pql.stream()
230+
.filter(q -> q.getKey().equals("contentUpatedAt"))
231+
.findFirst()
232+
.get();
233+
234+
assertEquals(QueryOperation.EQUALS, query.getOperation());
235+
assertEquals(1, query.getValues().size());
236+
Object value = query.getValues().get(0);
237+
assertTrue("JSONPath value should be a String", value instanceof String);
238+
assertEquals("$.space.properties.version", value);
239+
}
151240
}

xyz-models/src/main/java/com/here/xyz/events/PropertiesQuery.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017-2019 HERE Europe B.V.
2+
* Copyright (C) 2017-2025 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -155,6 +155,11 @@ public static String getConvertedKey(String rawKey) {
155155
}
156156

157157
public static Object getConvertedValue(String rawValue) {
158+
// JSONPath
159+
if (rawValue != null && rawValue.startsWith("$")) {
160+
return rawValue;
161+
}
162+
158163
// Boolean
159164
if (rawValue.equals("true")) {
160165
return true;
@@ -196,4 +201,23 @@ private static String transformLegacyTags(String legacyTagsQuery) {
196201

197202
return F_PREFIX + "tags" + "=cs=" + tags;
198203
}
204+
205+
public List<String> getJsonPathValues() {
206+
List<String> jsonPaths = new ArrayList<>();
207+
208+
for (PropertyQueryList queries : this) {
209+
for (PropertyQuery query : queries) {
210+
for (Object value : query.getValues()) {
211+
if (value instanceof String) {
212+
String s = (String) value;
213+
if (s.startsWith("$")) {
214+
jsonPaths.add(s);
215+
}
216+
}
217+
}
218+
}
219+
}
220+
221+
return jsonPaths;
222+
}
199223
}

0 commit comments

Comments
 (0)