Skip to content

Commit d6c9b00

Browse files
committed
GH-2313 - Comments.
1 parent b9e0fea commit d6c9b00

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,55 @@ public static List<PropertyPath> getInputProperties(ResultProcessor resultProces
4747
ReturnedType returnedType = resultProcessor.getReturnedType();
4848
List<PropertyPath> filteredProperties = new ArrayList<>();
4949

50+
boolean isProjecting = returnedType.isProjecting();
5051
for (String inputProperty : returnedType.getInputProperties()) {
51-
if (returnedType.isProjecting()) {
52+
if (isProjecting) {
5253
addPropertiesFrom(returnedType.getDomainType(), returnedType.getReturnedType(), factory,
5354
filteredProperties, inputProperty, mappingContext);
5455
} else {
5556
addPropertiesFromEntity(filteredProperties, PropertyPath.from(inputProperty, returnedType.getDomainType()),
5657
returnedType.getReturnedType(), mappingContext, new HashSet<>());
5758
}
5859
}
59-
return returnedType.isProjecting() ? filteredProperties : Collections.emptyList();
60+
return isProjecting ? filteredProperties : Collections.emptyList();
6061
}
6162

6263
public static List<PropertyPath> addPropertiesFrom(Class<?> returnType, Class<?> domainType,
6364
ProjectionFactory projectionFactory,
6465
Neo4jMappingContext neo4jMappingContext) {
6566

6667
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(returnType);
67-
List<PropertyPath> pps = new ArrayList<>();
68+
List<PropertyPath> propertyPaths = new ArrayList<>();
6869
for (PropertyDescriptor inputProperty : projectionInformation.getInputProperties()) {
69-
addPropertiesFrom(returnType, domainType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
70+
addPropertiesFrom(returnType, domainType, projectionFactory, propertyPaths, inputProperty.getName(), neo4jMappingContext);
7071
}
71-
return pps;
72+
return propertyPaths;
7273
}
7374

7475
private static void addPropertiesFrom(Class<?> domainType, Class<?> returnedType, ProjectionFactory factory,
7576
Collection<PropertyPath> filteredProperties, String inputProperty,
7677
Neo4jMappingContext mappingContext) {
7778

78-
ProjectionInformation projectionInformation1 = factory.getProjectionInformation(returnedType);
79+
ProjectionInformation projectionInformation = factory.getProjectionInformation(returnedType);
7980
PropertyPath propertyPath;
80-
if (projectionInformation1.isClosed()) {
81+
82+
// If this is a closed projection we can assume that the return type (possible projection type) contains
83+
// only fields accessible with a property path.
84+
if (projectionInformation.isClosed()) {
8185
propertyPath = PropertyPath.from(inputProperty, returnedType);
8286
} else {
87+
// otherwise the domain type is used right from the start
8388
propertyPath = PropertyPath.from(inputProperty, domainType);
8489
}
8590

8691
Class<?> propertyType = propertyPath.getLeafType();
92+
// 1. simple types can be added directly
93+
// 2. something that looks like an entity needs to get processed as such
94+
// 3. Embedded projection
8795
if (Neo4jSimpleTypes.HOLDER.isSimpleType(propertyType) || mappingContext.hasCustomWriteTarget(propertyType)) {
8896
filteredProperties.add(propertyPath);
8997
} else if (mappingContext.hasPersistentEntityFor(propertyType)) {
98+
// avoid recursion / cycles
9099
if (propertyType.equals(domainType)) {
91100
return;
92101
}
@@ -95,9 +104,10 @@ private static void addPropertiesFrom(Class<?> domainType, Class<?> returnedType
95104
} else {
96105
ProjectionInformation nestedProjectionInformation = factory.getProjectionInformation(propertyType);
97106
filteredProperties.add(propertyPath);
107+
// Closed projection should get handled as above (recursion)
98108
if (nestedProjectionInformation.isClosed()) {
99-
for (PropertyDescriptor secondInputProperty : nestedProjectionInformation.getInputProperties()) {
100-
PropertyPath nestedPropertyPath = propertyPath.nested(secondInputProperty.getName());
109+
for (PropertyDescriptor nestedInputProperty : nestedProjectionInformation.getInputProperties()) {
110+
PropertyPath nestedPropertyPath = propertyPath.nested(nestedInputProperty.getName());
101111
filteredProperties.add(nestedPropertyPath);
102112
addPropertiesFrom(domainType, returnedType, factory, filteredProperties,
103113
nestedPropertyPath.toDotPath(), mappingContext);
@@ -124,6 +134,7 @@ private static void addPropertiesFromEntity(Collection<PropertyPath> filteredPro
124134
Collection<Neo4jPersistentEntity<?>> processedEntities) {
125135

126136
Neo4jPersistentEntity<?> persistentEntityFromProperty = mappingContext.getPersistentEntity(propertyType);
137+
// break the recursion / cycles
127138
if (hasProcessedEntity(persistentEntityFromProperty, processedEntities)) {
128139
return;
129140
}
@@ -151,6 +162,7 @@ private static void takeAllPropertiesFromEntity(Collection<PropertyPath> filtere
151162

152163
filteredProperties.add(propertyPath);
153164

165+
// there is no unifying accessor for plain properties and associations, need to do the same thing twice.
154166
for (String propertyName : propertyNames) {
155167
addPropertiesFrom(filteredProperties, propertyPath.nested(propertyName), mappingContext, processedEntities);
156168
}
@@ -170,8 +182,10 @@ private static void addPropertiesFrom(Collection<PropertyPath> filteredPropertie
170182
return;
171183
}
172184
Class<?> propertyType = propertyPath.getLeafType();
185+
// simple types can get added directly to the list.
173186
if (Neo4jSimpleTypes.HOLDER.isSimpleType(propertyType) || mappingContext.hasCustomWriteTarget(propertyType)) {
174187
filteredProperties.add(propertyPath);
188+
// Other types are handled also as entites because there cannot be any nested projection within a real entity.
175189
} else if (mappingContext.hasPersistentEntityFor(propertyType)) {
176190
addPropertiesFromEntity(filteredProperties, propertyPath, propertyType, mappingContext, processedEntities);
177191
}

0 commit comments

Comments
 (0)