Skip to content

Commit 53909d0

Browse files
christophstroblmp911de
authored andcommitted
Clear indexed data if property value is set to null.
Closes #2882 Original pull request: #2895
1 parent 304c007 commit 53909d0

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

Diff for: src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void doWithPersistentProperty(RedisPersistentProperty persistentProperty)
122122
Object propertyValue = accessor.getProperty(persistentProperty);
123123

124124
if (propertyValue == null) {
125+
indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, null));
125126
return;
126127
}
127128

@@ -212,21 +213,32 @@ protected Set<IndexedData> resolveIndex(String keyspace, String propertyPath,
212213

213214
IndexedData indexedData = null;
214215
if (transformedValue == null) {
215-
indexedData = new RemoveIndexedData(indexedData);
216+
217+
indexedData = new RemoveIndexedData(new IndexedData() {
218+
@Override
219+
public String getIndexName() {
220+
return indexDefinition.getIndexName();
221+
}
222+
223+
@Override
224+
public String getKeyspace() {
225+
return indexDefinition.getKeyspace();
226+
}
227+
});
216228
} else {
217229
indexedData = indexedDataFactoryProvider.getIndexedDataFactory(indexDefinition).createIndexedDataFor(value);
218230
}
219231
data.add(indexedData);
220232
}
221233
}
222234

223-
else if (property != null && property.isAnnotationPresent(Indexed.class)) {
235+
else if (property != null && value != null && property.isAnnotationPresent(Indexed.class)) {
224236

225237
SimpleIndexDefinition indexDefinition = new SimpleIndexDefinition(keyspace, path);
226238
indexConfiguration.addIndexDefinition(indexDefinition);
227239

228240
data.add(indexedDataFactoryProvider.getIndexedDataFactory(indexDefinition).createIndexedDataFor(value));
229-
} else if (property != null && property.isAnnotationPresent(GeoIndexed.class)) {
241+
} else if (property != null && value != null && property.isAnnotationPresent(GeoIndexed.class)) {
230242

231243
GeoIndexDefinition indexDefinition = new GeoIndexDefinition(keyspace, path);
232244
indexConfiguration.addIndexDefinition(indexDefinition);

Diff for: src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ void putWritesSimpleIndexDataCorrectly() {
147147
assertThat(template.opsForSet().members("persons:firstname:rand")).contains("1");
148148
}
149149

150+
@Test // GH-2882
151+
void indexDataShouldBeCleardIfPropertyValueIsSetToNull() {
152+
153+
Person rand = new Person();
154+
rand.firstname = "rand";
155+
156+
adapter.put("1", rand, "persons");
157+
158+
assertThat(template.keys("persons*")).contains("persons:firstname:rand");
159+
assertThat(template.opsForSet().members("persons:firstname:rand")).contains("1");
160+
161+
rand.id = "1";
162+
rand.firstname = null;
163+
adapter.put("1", rand, "persons");
164+
165+
assertThat(template.keys("persons*")).doesNotContain("persons:firstname:rand");
166+
assertThat(template.opsForSet().members("persons:firstname:rand")).doesNotContain("1");
167+
}
168+
150169
@Test // DATAREDIS-744
151170
void putWritesSimpleIndexDataWithColonCorrectly() {
152171

0 commit comments

Comments
 (0)