From cbed3346d5d8acf3a99bfaf54f008619c257a0b1 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 26 Sep 2023 14:47:19 +0200 Subject: [PATCH] Fixes it but needs a better concept --- .../mongodb/core/convert/QueryMapper.java | 14 +++++++++----- .../core/convert/UpdateMapperUnitTests.java | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java index 06aee31afc..bd55ca3e5b 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java @@ -1470,11 +1470,15 @@ protected String mapPropertyName(MongoPersistentProperty property) { return mappedName.toString(); } - String nextToken = nextToken(); - if (isPositionalParameter(nextToken)) { - - mappedName.append(".").append(nextToken); - currentIndex += 2; + int i = 1; + String nextToken = pathParts.get(currentIndex + i); + if(isPositionalParameter(nextToken)) { + while (isPositionalParameter(nextToken)) { + mappedName.append(".").append(nextToken); + i++; + nextToken = currentIndex + i < pathParts.size() ? pathParts.get(currentIndex + i) : ""; + } + currentIndex += i; return mappedName.toString(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java index df1e13aeae..5d8629a5b4 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java @@ -1350,6 +1350,17 @@ void updateConsidersValueConverterWhenPresent() { assertThat(mappedUpdate).isEqualTo("{ $set : { 'text' : 'eulav' } }"); } + @ParameterizedTest // GH-4502 + @ValueSource(strings = {"levelOne.levelTwo.1", "levelOne.levelTwo.1.0", "levelOne.levelTwo.2.0",}) + void objectNestedIntegerFieldCorrectly(String path) { + + Update update = new Update().set(path, "4"); + Document mappedUpdate = mapper.getMappedObject(update.getUpdateObject(), + context.getPersistentEntity(EntityWithNestedObject1.class)); + + assertThat(mappedUpdate).isEqualTo(new org.bson.Document("$set", new org.bson.Document(path, "4"))); + } + static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes { ListModelWrapper concreteTypeWithListAttributeOfInterfaceType; } @@ -1818,4 +1829,12 @@ static class WithPropertyValueConverter { @ValueConverter(ReversingValueConverter.class) String text; } + + static class EntityWithNestedObject1 { + EntityWithNestedObject2 levelOne; + } + + static class EntityWithNestedObject2 { + Integer levelTwo; + } }