From 291d84591cd982394b3cb6d5fc453dd4691dd314 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 28 Jan 2020 08:59:50 +0100 Subject: [PATCH] DATAMONGO-2460 - Fix target type computation for complex id properties with @Field annotation. We now set the target type to org.bson.Document for id properties annotated with @Field having the implicit target type derived from the annotation. Along the lines we fixed warn message when an id property with explicit (unsupported) field name is detected. Original pull request: #830. --- .../mapping/BasicMongoPersistentProperty.java | 15 ++++++++++++-- ...BasicMongoPersistentPropertyUnitTests.java | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java index 1bb0cf83a..f0caf4e7d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java @@ -79,8 +79,14 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope this.fieldNamingStrategy = fieldNamingStrategy == null ? PropertyNameFieldNamingStrategy.INSTANCE : fieldNamingStrategy; - if (isIdProperty() && getFieldName() != ID_FIELD_NAME) { - LOG.warn("Customizing field name for id property not allowed! Custom name will not be considered!"); + if (isIdProperty() && hasExplicitFieldName()) { + + String annotatedName = getAnnotatedFieldName(); + if (!ID_FIELD_NAME.equals(annotatedName)) { + LOG.warn( + "Customizing field name for id property '{}.{}' is not allowed! Custom name ('{}') will not be considered!", + owner.getName(), getName(), annotatedName); + } } } @@ -167,6 +173,11 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope FieldType fieldType = fieldAnnotation.targetType(); if (fieldType == FieldType.IMPLICIT) { + + if (isEntity()) { + return org.bson.Document.class; + } + return getType(); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java index 5e9d6868c..3e0671f04 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import org.bson.Document; import org.bson.types.ObjectId; import org.junit.Before; import org.junit.Test; @@ -223,6 +224,13 @@ public class BasicMongoPersistentPropertyUnitTests { assertThat(property.getFieldType()).isEqualTo(ObjectId.class); } + @Test // DATAMONGO-2460 + public void fieldTypeShouldBeDocumentForPropertiesAnnotatedIdWhenAComplexTypeAndFieldTypeImplicit() { + + MongoPersistentProperty property = getPropertyFor(WithComplexId.class, "id"); + assertThat(property.getFieldType()).isEqualTo(Document.class); + } + private MongoPersistentProperty getPropertyFor(Field field) { return getPropertyFor(entity, field); } @@ -329,4 +337,16 @@ public class BasicMongoPersistentPropertyUnitTests { @MongoId(FieldType.OBJECT_ID) String id; } + + static class ComplexId { + + String value; + } + + static class WithComplexId { + + @Id + @org.springframework.data.mongodb.core.mapping.Field + ComplexId id; + } }