Skip to content

Do not consider collection like types being entities. #4655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.4-SNAPSHOT</version>
<version>4.2.x-4653-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -26,7 +26,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>3.2.4-SNAPSHOT</springdata.commons>
<springdata.commons>3.2.4-GH-3056-SNAPSHOT</springdata.commons>
<mongo>4.11.1</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.4-SNAPSHOT</version>
<version>4.2.x-4653-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.4-SNAPSHOT</version>
<version>4.2.x-4653-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.4-SNAPSHOT</version>
<version>4.2.x-4653-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
Expand Down Expand Up @@ -318,7 +319,7 @@ protected void validate() {

String annotatedName = getAnnotatedFieldName();
if (!ID_FIELD_NAME.equals(annotatedName)) {
if(LOG.isWarnEnabled()) {
if (LOG.isWarnEnabled()) {
LOG.warn(String.format(
"Customizing field name for id property '%s.%s' is not allowed; Custom name ('%s') will not be considered",
getOwner().getName(), getName(), annotatedName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.jmolecules.ddd.annotation.Identity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.core.annotation.AliasFor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mapping.MappingException;
Expand All @@ -39,7 +44,7 @@
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.ReflectionUtils;

/**
Expand All @@ -56,7 +61,7 @@ public class BasicMongoPersistentPropertyUnitTests {

@BeforeEach
void setup() {
entity = new BasicMongoPersistentEntity<>(ClassTypeInformation.from(Person.class));
entity = new BasicMongoPersistentEntity<>(TypeInformation.of(Person.class));
}

@Test
Expand Down Expand Up @@ -90,7 +95,7 @@ void preventsNegativeOrder() {
void usesPropertyAccessForThrowableCause() {

BasicMongoPersistentEntity<Throwable> entity = new BasicMongoPersistentEntity<>(
ClassTypeInformation.from(Throwable.class));
TypeInformation.of(Throwable.class));
MongoPersistentProperty property = getPropertyFor(entity, "cause");

assertThat(property.usePropertyAccess()).isTrue();
Expand All @@ -99,7 +104,7 @@ void usesPropertyAccessForThrowableCause() {
@Test // DATAMONGO-607
void usesCustomFieldNamingStrategyByDefault() throws Exception {

ClassTypeInformation<Person> type = ClassTypeInformation.from(Person.class);
TypeInformation<Person> type = TypeInformation.of(Person.class);
Field field = ReflectionUtils.findField(Person.class, "lastname");

MongoPersistentProperty property = new BasicMongoPersistentProperty(Property.of(type, field), entity,
Expand All @@ -116,7 +121,7 @@ void usesCustomFieldNamingStrategyByDefault() throws Exception {
@Test // DATAMONGO-607
void rejectsInvalidValueReturnedByFieldNamingStrategy() {

ClassTypeInformation<Person> type = ClassTypeInformation.from(Person.class);
TypeInformation<Person> type = TypeInformation.of(Person.class);
Field field = ReflectionUtils.findField(Person.class, "lastname");

MongoPersistentProperty property = new BasicMongoPersistentProperty(Property.of(type, field), entity,
Expand Down Expand Up @@ -250,12 +255,32 @@ void considersJMoleculesIdentityExplicitlyAnnotatedIdentifier() {
assertThat(property.isExplicitIdProperty()).isTrue();
}

@ParameterizedTest // GH-4653
@ValueSource(strings = { "objectMap", "stringMap", "mapOfSet", "objectSet", "stringSet", "objectIterable",
"stringIterable", "iterableOfSet" })
void doesNotConsiderCollectionLikeTypesEntities(String field) {

MongoPersistentProperty property = getPropertyFor(WithCollectionAndMapTypes.class, field);
assertThat(property.getPersistentEntityTypeInformation()).isEmpty();
assertThat(property.isEntity()).isFalse();
}

@ParameterizedTest // GH-4653
@ValueSource(strings = { "mapOfSetOfComplexId", "mapKeyOfSetOfComplexId", "listOfSetOfComplexId" })
void doesConsiderEntityTypeInformation(String field) {

MongoPersistentProperty property = getPropertyFor(WithCollectionAndMapTypes.class, field);
assertThat(property.getPersistentEntityTypeInformation()).hasSize(1).map(TypeInformation::getType)
.contains((Class) ComplexId.class);
assertThat(property.isEntity()).isTrue();
}

private MongoPersistentProperty getPropertyFor(Field field) {
return getPropertyFor(entity, field);
}

private static <T> MongoPersistentProperty getPropertyFor(Class<T> type, String fieldname) {
return getPropertyFor(new BasicMongoPersistentEntity<>(ClassTypeInformation.from(type)), fieldname);
return getPropertyFor(new BasicMongoPersistentEntity<>(TypeInformation.of(type)), fieldname);
}

private static MongoPersistentProperty getPropertyFor(MongoPersistentEntity<?> entity, String fieldname) {
Expand Down Expand Up @@ -335,12 +360,14 @@ static class DocumentWithExplicitlyRenamedIdProperty {

static class DocumentWithExplicitlyRenamedIdPropertyHavingIdAnnotation {

@Id @org.springframework.data.mongodb.core.mapping.Field("id") String id;
@Id
@org.springframework.data.mongodb.core.mapping.Field("id") String id;
}

static class DocumentWithComposedAnnotations {

@ComposedIdAnnotation @ComposedFieldAnnotation String myId;
@ComposedIdAnnotation
@ComposedFieldAnnotation String myId;
@ComposedFieldAnnotation(name = "myField") String myField;
}

Expand All @@ -356,7 +383,8 @@ static class DocumentWithComposedAnnotations {
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Id
static @interface ComposedIdAnnotation {}
static @interface ComposedIdAnnotation {
}

static class WithStringMongoId {

Expand All @@ -375,10 +403,28 @@ static class ComplexId {

static class WithComplexId {

@Id @org.springframework.data.mongodb.core.mapping.Field ComplexId id;
@Id
@org.springframework.data.mongodb.core.mapping.Field ComplexId id;
}

static class WithJMoleculesIdentity {
@Identity ObjectId identifier;
}

static class WithCollectionAndMapTypes {

Map<String, Object> objectMap;
Map<String, String> stringMap;
Map<String, HashSet<String>> mapOfSet;
HashSet<Object> objectSet;
HashSet<String> stringSet;
Iterable<Object> objectIterable;
Iterable<String> stringIterable;
Iterable<Set<String>> iterableOfSet;
List<Set<String>> listWithNestedObject;

Map<String, HashSet<ComplexId>> mapOfSetOfComplexId;
Map<HashSet<ComplexId>, String> mapKeyOfSetOfComplexId;
List<Set<ComplexId>> listOfSetOfComplexId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ void shouldNotCreateEntityForOptionalGetter() {
MongoMappingContext context = new MongoMappingContext();
MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(InterfaceWithMethodReturningOptional.class);

assertThat(context.getPersistentEntities()).map(it -> it.getType()).doesNotContain((Class)
Optional.class).contains((Class)Person.class);
assertThat(context.getPersistentEntities()).map(it -> it.getType()).doesNotContain((Class) Optional.class)
.contains((Class) Person.class);
}

@Test // GH-3656
Expand All @@ -190,8 +190,8 @@ void shouldNotCreateEntityForOptionalField() {
MongoMappingContext context = new MongoMappingContext();
MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(ClassWithOptionalField.class);

assertThat(context.getPersistentEntities()).map(it -> it.getType()).doesNotContain((Class)
Optional.class).contains((Class)Person.class);
assertThat(context.getPersistentEntities()).map(it -> it.getType()).doesNotContain((Class) Optional.class)
.contains((Class) Person.class);
}

public class SampleClass {
Expand Down