Skip to content

Commit 37a2f12

Browse files
Make sure a SimpleTypeHolder is used that knows about custom conversions.
Needed as spring-projects/spring-data-commons@b3b590d relies on that information. We didn’t apply this correct beforehand.
1 parent f2bb752 commit 37a2f12

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

src/main/java/org/springframework/data/neo4j/core/convert/Neo4jConversionService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,10 @@ Object readValue(
8282
Value writeValue(
8383
@Nullable Object value, TypeInformation<?> sourceType, @Nullable Function<Object, Value> conversionOverride
8484
);
85+
86+
/**
87+
* @param type A type that should be checked whether it's simple or not.
88+
* @return True if {@code type} is a simple type, according to {@link Neo4jSimpleTypes} and the registered converters.
89+
*/
90+
boolean isSimpleType(Class<?> type);
8591
}

src/main/java/org/springframework/data/neo4j/core/convert/Neo4jSimpleTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public final class Neo4jSimpleTypes {
8787
}
8888

8989
/**
90-
* The simple types we support plus all the simple types recognized by Spring.
90+
* The simple types we support plus all the simple types recognized by Spring. Not taking custom conversions into account.
9191
*/
9292
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(NEO4J_NATIVE_TYPES, true);
9393

src/main/java/org/springframework/data/neo4j/core/mapping/DefaultNeo4jConversionService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.core.convert.support.ConfigurableConversionService;
2828
import org.springframework.core.convert.support.DefaultConversionService;
2929
import org.springframework.dao.TypeMismatchDataAccessException;
30+
import org.springframework.data.mapping.model.SimpleTypeHolder;
3031
import org.springframework.data.neo4j.core.convert.Neo4jConversionService;
3132
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
3233
import org.springframework.data.util.TypeInformation;
@@ -41,6 +42,7 @@ final class DefaultNeo4jConversionService implements Neo4jConversionService {
4142

4243
private final ConversionService conversionService;
4344
private final Predicate<Class<?>> hasCustomWriteTargetPredicate;
45+
private final SimpleTypeHolder simpleTypes;
4446

4547
DefaultNeo4jConversionService(Neo4jConversions neo4jConversions) {
4648

@@ -49,6 +51,7 @@ final class DefaultNeo4jConversionService implements Neo4jConversionService {
4951

5052
this.conversionService = configurableConversionService;
5153
this.hasCustomWriteTargetPredicate = neo4jConversions::hasCustomWriteTarget;
54+
this.simpleTypes = neo4jConversions.getSimpleTypeHolder();
5255
}
5356

5457
@Override
@@ -132,4 +135,9 @@ private Value writeValueImpl(@Nullable Object value, TypeInformation<?> type,
132135
private static boolean isCollection(TypeInformation<?> type) {
133136
return Collection.class.isAssignableFrom(type.getType());
134137
}
138+
139+
@Override
140+
public boolean isSimpleType(Class<?> type) {
141+
return simpleTypes.isSimpleType(type);
142+
}
135143
}

src/main/java/org/springframework/data/neo4j/core/mapping/Neo4jMappingContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
4747
import org.springframework.data.neo4j.core.convert.Neo4jPersistentPropertyConverter;
4848
import org.springframework.data.neo4j.core.convert.Neo4jPersistentPropertyConverterFactory;
49-
import org.springframework.data.neo4j.core.convert.Neo4jSimpleTypes;
5049
import org.springframework.data.neo4j.core.schema.IdGenerator;
5150
import org.springframework.data.util.ReflectionUtils;
5251
import org.springframework.data.util.TypeInformation;
@@ -125,10 +124,10 @@ public void setStrict(boolean strict) {
125124
@API(status = API.Status.INTERNAL, since = "6.0")
126125
public Neo4jMappingContext(Neo4jConversions neo4jConversions, @Nullable TypeSystem typeSystem) {
127126

128-
super.setSimpleTypeHolder(Neo4jSimpleTypes.HOLDER);
129127
this.conversionService = new DefaultNeo4jConversionService(neo4jConversions);
130-
131128
this.typeSystem = typeSystem == null ? InternalTypeSystem.TYPE_SYSTEM : typeSystem;
129+
130+
super.setSimpleTypeHolder(neo4jConversions.getSimpleTypeHolder());
132131
}
133132

134133
public Neo4jEntityConverter getEntityConverter() {

src/main/java/org/springframework/data/neo4j/repository/query/Neo4jQuerySupport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.data.geo.Circle;
4141
import org.springframework.data.geo.Distance;
4242
import org.springframework.data.geo.Metrics;
43-
import org.springframework.data.neo4j.core.convert.Neo4jSimpleTypes;
4443
import org.springframework.data.neo4j.core.mapping.CypherGenerator;
4544
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
4645
import org.springframework.data.repository.query.QueryMethod;
@@ -105,7 +104,7 @@ static Class<?> getDomainType(QueryMethod queryMethod) {
105104

106105
final BiFunction<TypeSystem, MapAccessor, ?> mappingFunction;
107106

108-
if (Neo4jSimpleTypes.HOLDER.isSimpleType(returnedType)) {
107+
if (mappingContext.getConversionService().isSimpleType(returnedType)) {
109108
// Clients automatically selects a single value mapping function.
110109
// It will thrown an error if the query contains more than one column.
111110
mappingFunction = null;

src/test/java/org/springframework/data/neo4j/core/mapping/Neo4jMappingContextTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
6464
import org.springframework.data.neo4j.core.schema.TargetNode;
6565
import org.springframework.data.neo4j.integration.shared.common.FriendshipRelationship;
66+
import org.springframework.data.neo4j.integration.shared.conversion.ThingWithCompositeProperties;
67+
import org.springframework.data.neo4j.integration.shared.conversion.ThingWithCustomTypes;
6668
import org.springframework.data.neo4j.test.LogbackCapture;
6769
import org.springframework.data.neo4j.test.LogbackCapturingExtension;
6870

@@ -480,6 +482,31 @@ void relAnnotationWithoutTypeMustOverwriteDefaultType() {
480482
assertThat(entity.getRelationships()).anyMatch(r -> r.getFieldName().equals("theSuperBike") && r.getType().equals("THE_SUPER_BIKE"));
481483
}
482484

485+
@Test // COMMONS-2390
486+
void shouldNotCreateEntityForConvertedSimpleTypes() {
487+
488+
Set<GenericConverter> additionalConverters = new HashSet<>();
489+
additionalConverters.add(new ThingWithCustomTypes.CustomTypeConverter());
490+
additionalConverters.add(new ThingWithCustomTypes.DifferentTypeConverter());
491+
492+
Neo4jMappingContext schema = new Neo4jMappingContext(new Neo4jConversions(additionalConverters));
493+
schema.setStrict(true);
494+
schema.setInitialEntitySet(
495+
new HashSet<>(Arrays.asList(ThingWithCustomTypes.class, ThingWithCompositeProperties.class)));
496+
schema.initialize();
497+
498+
assertThat(schema.hasPersistentEntityFor(ThingWithCustomTypes.class)).isTrue();
499+
assertThat(schema.hasPersistentEntityFor(ThingWithCompositeProperties.class)).isTrue();
500+
501+
Neo4jPersistentEntity<?> entity = schema.getPersistentEntity(ThingWithCustomTypes.class);
502+
assertThat(entity.getPersistentProperty("customType").isEntity()).isFalse();
503+
504+
entity = schema.getPersistentEntity(ThingWithCompositeProperties.class);
505+
assertThat(entity.getPersistentProperty("customTypeMap").isEntity()).isFalse();
506+
507+
assertThat(schema.hasPersistentEntityFor(ThingWithCustomTypes.CustomType.class)).isFalse();
508+
}
509+
483510
static class DummyIdGenerator implements IdGenerator<Void> {
484511

485512
@Override

0 commit comments

Comments
 (0)