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.
This commit is contained in:
Christoph Strobl
2020-01-28 08:59:50 +01:00
committed by Mark Paluch
parent 8857903831
commit 1b7273db42
2 changed files with 33 additions and 2 deletions

View File

@@ -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();
}

View File

@@ -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;
}
}