From 37fa39fa15e263acc04f8d1098985e070b9cb674 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 2 May 2023 13:11:35 +0200 Subject: [PATCH] Fix Querydsl conversion for properties with explicit target type. This commit makes sure to convert values, for Querydsl predicates pointing to a property with an explicitly annotated target type, into the desired format. --- .../support/SpringDataMongodbSerializer.java | 8 +++-- .../SpringDataMongodbSerializerUnitTests.java | 10 +++++++ .../support/WithCustomTargetTypeProperty.java | 29 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/WithCustomTargetTypeProperty.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java index 7d14ef0b4..8b39a4da5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java @@ -27,6 +27,7 @@ import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.convert.QueryMapper; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; +import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -149,12 +150,15 @@ class SpringDataMongodbSerializer extends MongodbDocumentSerializer { protected Object convert(@Nullable Path path, @Nullable Constant constant) { + MongoPersistentProperty property = getPropertyFor(path); + if (!isReference(path)) { + if(property != null && property.hasExplicitWriteTarget()) { + return converter.convertToMongoType(constant.getConstant(), TypeInformation.of(property.getFieldType())); + } return super.convert(path, constant); } - MongoPersistentProperty property = getPropertyFor(path); - if (property.isDocumentReference()) { return converter.toDocumentPointer(constant.getConstant(), property).getPointer(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java index 82213d143..74d950e9b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializerUnitTests.java @@ -37,6 +37,7 @@ import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.convert.MongoCustomConversions; import org.springframework.data.mongodb.core.mapping.Field; +import org.springframework.data.mongodb.core.mapping.FieldType; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.repository.Person.Sex; import org.springframework.data.mongodb.repository.QAddress; @@ -246,6 +247,15 @@ public class SpringDataMongodbSerializerUnitTests { assertThat(serializer.handle(predicate)).isEqualTo(Document.parse("{ 'spiritAnimal' : '007' }")); } + @Test // GH-4359 + void parsesValueToExplicitTargetFieldType() { + + ObjectId oid = new ObjectId(); + Predicate predicate = QWithCustomTargetTypeProperty.withCustomTargetTypeProperty.customFieldType.eq(oid.toHexString()); + + assertThat(serializer.handle(predicate)).isEqualTo(new Document("customFieldType", oid)); + } + class Address { String id; String street; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/WithCustomTargetTypeProperty.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/WithCustomTargetTypeProperty.java new file mode 100644 index 000000000..f0d2cdaaa --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/WithCustomTargetTypeProperty.java @@ -0,0 +1,29 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.repository.support; + +import org.springframework.data.mongodb.core.mapping.Field; +import org.springframework.data.mongodb.core.mapping.FieldType; + +/** + * @author Christoph Strobl + */ +@org.springframework.data.mongodb.core.mapping.Document +public class WithCustomTargetTypeProperty { + + @Field(targetType = FieldType.OBJECT_ID) // + String customFieldType; +}