From d7fef31ad6f023795f375389098c3f656b70be54 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Sun, 1 Sep 2024 18:34:07 +0300 Subject: [PATCH] FMWK-525 Make template.findAll() work correctly with id-only records (#772) --- .../core/ReactiveAerospikeTemplate.java | 7 +++++- .../data/aerospike/core/TemplateUtils.java | 8 +++++-- ...ReactiveAerospikeTemplateFindAllTests.java | 21 ++++++++++++++++++ .../sync/AerospikeTemplateFindAllTests.java | 22 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java b/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java index 9e011194f..cc5f3ed2c 100644 --- a/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java +++ b/src/main/java/org/springframework/data/aerospike/core/ReactiveAerospikeTemplate.java @@ -1301,7 +1301,12 @@ private String[] getBinNamesFromTargetClass(Class targetClass) { List binNamesList = new ArrayList<>(); targetEntity.doWithProperties( - (PropertyHandler) property -> binNamesList.add(property.getFieldName())); + (PropertyHandler) property -> { + if (!property.isIdProperty()) { + binNamesList.add(property.getFieldName()); + } + } + ); return binNamesList.toArray(new String[0]); } diff --git a/src/main/java/org/springframework/data/aerospike/core/TemplateUtils.java b/src/main/java/org/springframework/data/aerospike/core/TemplateUtils.java index d2c4abc65..9f2d9f0a6 100644 --- a/src/main/java/org/springframework/data/aerospike/core/TemplateUtils.java +++ b/src/main/java/org/springframework/data/aerospike/core/TemplateUtils.java @@ -106,8 +106,12 @@ public static String[] getBinNamesFromTargetClass(Class targetClass, List binNamesList = new ArrayList<>(); - targetEntity.doWithProperties((PropertyHandler) property - -> binNamesList.add(property.getFieldName())); + targetEntity.doWithProperties( + (PropertyHandler) property -> { + if (!property.isIdProperty()) { + binNamesList.add(property.getFieldName()); + } + }); return binNamesList.toArray(new String[0]); } diff --git a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindAllTests.java b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindAllTests.java index 646df7901..018180b2d 100644 --- a/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindAllTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/reactive/ReactiveAerospikeTemplateFindAllTests.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.springframework.data.aerospike.sample.Person; +import org.springframework.data.aerospike.sample.SampleClasses; import reactor.core.scheduler.Schedulers; import java.util.List; @@ -52,4 +53,24 @@ public void findAll_findNothing() { assertThat(actual).isEmpty(); } + + @Test + public void findAll_findIdOnlyRecord() { + var id = 100; + var doc = new SampleClasses.DocumentWithPrimitiveIntId(id); // id-only document + var clazz = SampleClasses.DocumentWithPrimitiveIntId.class; + + var existingDoc = reactiveTemplate.findById(id, clazz).block(); + assertThat(existingDoc).withFailMessage("The same record already exists").isNull(); + + reactiveTemplate.insert(doc).block(); + var resultsFindById = reactiveTemplate.findById(id, clazz).block(); + assertThat(resultsFindById).withFailMessage("findById error").isEqualTo(doc); + var resultsFindAll = reactiveTemplate.findAll(clazz).collectList().block(); + // findAll() must correctly find the record that contains id and no bins + assertThat(resultsFindAll).size().withFailMessage("findAll error").isEqualTo(1); + + // cleanup + reactiveTemplate.delete(doc); + } } diff --git a/src/test/java/org/springframework/data/aerospike/core/sync/AerospikeTemplateFindAllTests.java b/src/test/java/org/springframework/data/aerospike/core/sync/AerospikeTemplateFindAllTests.java index 345c79b03..a39e84eab 100644 --- a/src/test/java/org/springframework/data/aerospike/core/sync/AerospikeTemplateFindAllTests.java +++ b/src/test/java/org/springframework/data/aerospike/core/sync/AerospikeTemplateFindAllTests.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import org.springframework.data.aerospike.sample.Person; +import org.springframework.data.aerospike.sample.SampleClasses; import org.springframework.data.domain.Sort; import java.util.List; @@ -65,6 +66,7 @@ public void findAll_findNothing() { Stream result = template.findAll(Person.class); assertThat(result).isEmpty(); + // bring records back // batch write operations are supported starting with Server version 6.0+ if (serverVersionSupport.isBatchWriteSupported()) { template.insertAll(allPersons); @@ -72,4 +74,24 @@ public void findAll_findNothing() { allPersons.forEach(person -> template.insert(person)); } } + + @Test + public void findAll_findIdOnlyRecord() { + var id = 100; + var doc = new SampleClasses.DocumentWithPrimitiveIntId(id); // id-only document + var clazz = SampleClasses.DocumentWithPrimitiveIntId.class; + + var existingDoc = template.findById(id, clazz); + assertThat(existingDoc).withFailMessage("The same record already exists").isNull(); + + template.insert(doc); + var resultsFindById = template.findById(id, clazz); + assertThat(resultsFindById).withFailMessage("findById error").isEqualTo(doc); + var resultsFindAll = template.findAll(clazz); + // findAll() must correctly find the record that contains id and no bins + assertThat(resultsFindAll.toList()).size().withFailMessage("findAll error").isEqualTo(1); + + // cleanup + template.delete(doc); + } }