Skip to content

Commit 67fc916

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-1214 - Fix null return value of GEODIST command.
Original pull request: #565.
1 parent b26a932 commit 67fc916

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

src/main/java/org/springframework/data/redis/connection/jedis/JedisGeoCommands.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ public Distance geoDist(byte[] key, byte[] member1, byte[] member2) {
163163
return null;
164164
}
165165

166-
return distanceConverter.convert(connection.getJedis().geodist(key, member1, member2));
166+
Double distance = connection.getJedis().geodist(key, member1, member2);
167+
return distance != null ? distanceConverter.convert(distance) : null;
167168
} catch (Exception ex) {
168169
throw convertJedisAccessException(ex);
169170
}
@@ -196,7 +197,8 @@ public Distance geoDist(byte[] key, byte[] member1, byte[] member2, Metric metri
196197
return null;
197198
}
198199

199-
return distanceConverter.convert(connection.getJedis().geodist(key, member1, member2, geoUnit));
200+
Double distance = connection.getJedis().geodist(key, member1, member2, geoUnit);
201+
return distance != null ? distanceConverter.convert(distance) : null;
200202
} catch (Exception ex) {
201203
throw convertJedisAccessException(ex);
202204
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceGeoCommands.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ public Distance geoDist(byte[] key, byte[] member1, byte[] member2, Metric metri
176176
distanceConverter));
177177
return null;
178178
}
179-
return distanceConverter.convert(getConnection().geodist(key, member1, member2, geoUnit));
179+
180+
Double distance = getConnection().geodist(key, member1, member2, geoUnit);
181+
return distance != null ? distanceConverter.convert(distance) : null;
180182
} catch (Exception ex) {
181183
throw convertLettuceAccessException(ex);
182184
}

src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,6 +2704,19 @@ public void geoDist() {
27042704
assertThat(((Distance) result.get(1)).getUnit()).isEqualTo("m");
27052705
}
27062706

2707+
@Test // DATAREDIS-1214
2708+
@IfProfileValue(name = "redisVersion", value = "3.2+")
2709+
@WithRedisDriver({ RedisDriver.JEDIS, RedisDriver.LETTUCE })
2710+
public void geoDistNotExisting() {
2711+
2712+
String key = "geo-" + UUID.randomUUID();
2713+
actual.add(connection.geoAdd(key, Arrays.asList(PALERMO, CATANIA)));
2714+
actual.add(connection.geoDist(key, "Spring", "Data"));
2715+
2716+
List<Object> result = getResults();
2717+
assertThat(result.get(1)).isNull();
2718+
}
2719+
27072720
@Test // DATAREDIS-438
27082721
@IfProfileValue(name = "redisVersion", value = "3.2+")
27092722
@WithRedisDriver({ RedisDriver.JEDIS, RedisDriver.LETTUCE })

src/test/java/org/springframework/data/redis/core/DefaultGeoOperationsTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ public void geoDistShouldReturnDistanceInFeeCorrectly() {
186186
assertThat(dist.getUnit()).isEqualTo("ft");
187187
}
188188

189+
@Test // DATAREDIS-1214
190+
public void geoDistShouldReturnNullIfNoDistanceCalculable() {
191+
192+
K key = keyFactory.instance();
193+
M member1 = valueFactory.instance();
194+
M member2 = valueFactory.instance();
195+
M member3 = valueFactory.instance();
196+
M member4 = valueFactory.instance();
197+
198+
geoOperations.add(key, POINT_PALERMO, member1);
199+
geoOperations.add(key, POINT_CATANIA, member2);
200+
201+
Distance dist = geoOperations.distance(key, member3, member4, DistanceUnit.FEET);
202+
assertThat(dist).isNull();
203+
}
204+
189205
@Test // DATAREDIS-438, DATAREDIS-614
190206
public void testGeoHash() {
191207

0 commit comments

Comments
 (0)