Skip to content

Commit 2fe1366

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 fd9a996 commit 2fe1366

File tree

7 files changed

+47
-9
lines changed

7 files changed

+47
-9
lines changed

src/main/java/org/springframework/data/neo4j/core/PropertyFilterSupport.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.data.neo4j.core;
1717

1818
import org.springframework.data.mapping.PropertyPath;
19-
import org.springframework.data.neo4j.core.convert.Neo4jSimpleTypes;
2019
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
2120
import org.springframework.data.neo4j.core.mapping.Neo4jPersistentEntity;
2221
import org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty;
@@ -89,7 +88,7 @@ private static void addPropertiesFrom(Class<?> domainType, Class<?> returnedType
8988
// 1. Simple types can be added directly
9089
// 2. Something that looks like an entity needs to get processed as such
9190
// 3. Embedded projection
92-
if (Neo4jSimpleTypes.HOLDER.isSimpleType(propertyType) || mappingContext.hasCustomWriteTarget(propertyType)) {
91+
if (mappingContext.getConversionService().isSimpleType(propertyType)) {
9392
filteredProperties.add(propertyPath);
9493
} else if (mappingContext.hasPersistentEntityFor(propertyType)) {
9594
// avoid recursion / cycles
@@ -176,7 +175,7 @@ private static void addPropertiesFrom(Collection<PropertyPath> filteredPropertie
176175
}
177176
Class<?> propertyType = propertyPath.getLeafType();
178177
// simple types can get added directly to the list.
179-
if (Neo4jSimpleTypes.HOLDER.isSimpleType(propertyType) || mappingContext.hasCustomWriteTarget(propertyType)) {
178+
if (mappingContext.getConversionService().isSimpleType(propertyType)) {
180179
filteredProperties.add(propertyPath);
181180
// Other types are handled also as entities because there cannot be any nested projection within a real entity.
182181
} else if (mappingContext.hasPersistentEntityFor(propertyType)) {

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

+6
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

+1-1
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

+8
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

+2-3
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.neo4j.core.schema.Node;
5251
import org.springframework.data.util.ReflectionUtils;
@@ -126,10 +125,10 @@ public void setStrict(boolean strict) {
126125
@API(status = API.Status.INTERNAL, since = "6.0")
127126
public Neo4jMappingContext(Neo4jConversions neo4jConversions, @Nullable TypeSystem typeSystem) {
128127

129-
super.setSimpleTypeHolder(Neo4jSimpleTypes.HOLDER);
130128
this.conversionService = new DefaultNeo4jConversionService(neo4jConversions);
131-
132129
this.typeSystem = typeSystem == null ? InternalTypeSystem.TYPE_SYSTEM : typeSystem;
130+
131+
super.setSimpleTypeHolder(neo4jConversions.getSimpleTypeHolder());
133132
}
134133

135134
public Neo4jEntityConverter getEntityConverter() {

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.springframework.data.geo.Distance;
4444
import org.springframework.data.geo.Metrics;
4545
import org.springframework.data.neo4j.core.TemplateSupport;
46-
import org.springframework.data.neo4j.core.convert.Neo4jSimpleTypes;
4746
import org.springframework.data.neo4j.core.mapping.CypherGenerator;
4847
import org.springframework.data.neo4j.core.mapping.EntityInstanceWithSource;
4948
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;
@@ -112,7 +111,7 @@ static Class<?> getDomainType(QueryMethod queryMethod) {
112111

113112
final BiFunction<TypeSystem, MapAccessor, ?> mappingFunction;
114113

115-
if (Neo4jSimpleTypes.HOLDER.isSimpleType(returnedType)) {
114+
if (mappingContext.getConversionService().isSimpleType(returnedType)) {
116115
// Clients automatically selects a single value mapping function.
117116
// It will thrown an error if the query contains more than one column.
118117
mappingFunction = null;

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

+27
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
6565
import org.springframework.data.neo4j.core.schema.TargetNode;
6666
import org.springframework.data.neo4j.integration.shared.common.FriendshipRelationship;
67+
import org.springframework.data.neo4j.integration.shared.conversion.ThingWithCompositeProperties;
68+
import org.springframework.data.neo4j.integration.shared.conversion.ThingWithCustomTypes;
6769
import org.springframework.data.neo4j.test.LogbackCapture;
6870
import org.springframework.data.neo4j.test.LogbackCapturingExtension;
6971

@@ -496,6 +498,31 @@ void relAnnotationWithoutTypeMustOverwriteDefaultType() {
496498
assertThat(entity.getRelationships()).anyMatch(r -> r.getFieldName().equals("theSuperBike") && r.getType().equals("THE_SUPER_BIKE"));
497499
}
498500

501+
@Test // COMMONS-2390
502+
void shouldNotCreateEntityForConvertedSimpleTypes() {
503+
504+
Set<GenericConverter> additionalConverters = new HashSet<>();
505+
additionalConverters.add(new ThingWithCustomTypes.CustomTypeConverter());
506+
additionalConverters.add(new ThingWithCustomTypes.DifferentTypeConverter());
507+
508+
Neo4jMappingContext schema = new Neo4jMappingContext(new Neo4jConversions(additionalConverters));
509+
schema.setStrict(true);
510+
schema.setInitialEntitySet(
511+
new HashSet<>(Arrays.asList(ThingWithCustomTypes.class, ThingWithCompositeProperties.class)));
512+
schema.initialize();
513+
514+
assertThat(schema.hasPersistentEntityFor(ThingWithCustomTypes.class)).isTrue();
515+
assertThat(schema.hasPersistentEntityFor(ThingWithCompositeProperties.class)).isTrue();
516+
517+
Neo4jPersistentEntity<?> entity = schema.getPersistentEntity(ThingWithCustomTypes.class);
518+
assertThat(entity.getPersistentProperty("customType").isEntity()).isFalse();
519+
520+
entity = schema.getPersistentEntity(ThingWithCompositeProperties.class);
521+
assertThat(entity.getPersistentProperty("customTypeMap").isEntity()).isFalse();
522+
523+
assertThat(schema.hasPersistentEntityFor(ThingWithCustomTypes.CustomType.class)).isFalse();
524+
}
525+
499526
static class DummyIdGenerator implements IdGenerator<Void> {
500527

501528
@Override

0 commit comments

Comments
 (0)