From fb9d38d79c472b73d21db48f7aead5a8550483c2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 14 Aug 2017 14:06:16 +0200 Subject: [PATCH] DATACMNS-1139 - AbstractPersistentProperty.getRawType() now correctly resolves generics. We're now favoring the generic TypeInformation over trying to resolve the property type via field or PropertyDescriptor as only the former does proper generic type resolution. --- .../model/AbstractPersistentProperty.java | 7 ++----- .../AbstractPersistentPropertyUnitTests.java | 16 +++++++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index ea4c3f971..773efa2b6 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -23,13 +23,11 @@ import java.lang.reflect.Method; import java.util.Collections; import java.util.Map; import java.util.Optional; -import java.util.regex.Pattern; import org.springframework.data.annotation.Reference; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.PropertyPath; import org.springframework.data.util.Lazy; import org.springframework.data.util.ReflectionUtils; import org.springframework.data.util.TypeInformation; @@ -74,9 +72,8 @@ public abstract class AbstractPersistentProperty

Assert.notNull(owner, "Owner entity must not be null!"); this.name = property.getName(); - this.rawType = property.getType(); - this.information = PropertyPath.from(Pattern.quote(property.getName()), owner.getTypeInformation()) - .getTypeInformation(); + this.information = owner.getTypeInformation().getRequiredProperty(getName()); + this.rawType = this.information.getType(); this.property = property; this.association = Lazy.of(() -> isAssociation() ? createAssociation() : null); this.owner = owner; diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index 028ef07fc..a1c286644 100755 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -200,6 +200,14 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.getName()).isEqualTo("var_name_with_underscores"); } + @Test // DATACMNS-1139 + public void resolvesGenericsForRawType() { + + SamplePersistentProperty property = getProperty(FirstConcrete.class, "genericField"); + + assertThat(property.getRawType()).isEqualTo(String.class); + } + private BasicPersistentEntity getEntity(Class type) { return new BasicPersistentEntity<>(ClassTypeInformation.from(type)); } @@ -209,11 +217,9 @@ public class AbstractPersistentPropertyUnitTests { Optional field = Optional.ofNullable(ReflectionUtils.findField(type, name)); Optional descriptor = getPropertyDescriptor(type, name); - Property property = field - .map(it -> descriptor// - .map(foo -> Property.of(it, foo))// - .orElseGet(() -> Property.of(it))) - .orElseGet(() -> getPropertyDescriptor(type, name)// + Property property = field.map(it -> descriptor// + .map(foo -> Property.of(it, foo))// + .orElseGet(() -> Property.of(it))).orElseGet(() -> getPropertyDescriptor(type, name)// .map(it -> Property.of(it))// .orElseThrow( () -> new IllegalArgumentException(String.format("Couldn't find property %s on %s!", name, type))));