#369 - Fall back to column name in QueryMapper if path expression maps into simple type property.

We now fall back to the column name when resolving a property path expression that maps into a simple-typed property. PropertyPath.from(…) fails internally as it attempts to look up a simple type from the MappingContext and this fails for primitive and simple types.
This commit is contained in:
Mark Paluch
2020-05-22 09:16:49 +02:00
parent 7cfac09aa0
commit 01105d2a6a
2 changed files with 34 additions and 5 deletions

View File

@@ -20,12 +20,13 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.PropertyReferenceException;
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.r2dbc.convert.R2dbcConverter;
import org.springframework.data.r2dbc.dialect.BindMarker;
@@ -626,7 +627,7 @@ public class QueryMapper {
private final RelationalPersistentEntity<?> entity;
private final MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext;
private final RelationalPersistentProperty property;
private final @Nullable RelationalPersistentProperty property;
private final @Nullable PersistentPropertyPath<RelationalPersistentProperty> path;
/**
@@ -675,7 +676,7 @@ public class QueryMapper {
/**
* Returns the {@link PersistentPropertyPath} for the given {@code pathExpression}.
*
* @param pathExpression
* @param pathExpression the path expression to use.
* @return
*/
@Nullable
@@ -683,18 +684,27 @@ public class QueryMapper {
try {
PropertyPath path = PropertyPath.from(pathExpression, this.entity.getTypeInformation());
PropertyPath path = forName(pathExpression);
if (isPathToJavaLangClassProperty(path)) {
return null;
}
return this.mappingContext.getPersistentPropertyPath(path);
} catch (PropertyReferenceException | InvalidPersistentPropertyPath e) {
} catch (MappingException | PropertyReferenceException e) {
return null;
}
}
private PropertyPath forName(String path) {
if (entity.getPersistentProperty(path) != null) {
return PropertyPath.from(Pattern.quote(path), entity.getTypeInformation());
}
return PropertyPath.from(path, entity.getTypeInformation());
}
private boolean isPathToJavaLangClassProperty(PropertyPath path) {
return path.getType().equals(Class.class) && path.getLeafProperty().getOwningType().getType().equals(Class.class);
}

View File

@@ -369,6 +369,25 @@ public class QueryMapperUnitTests {
assertThat(mapped.getOrderFor("alternative")).isNull();
}
@Test // gh-369
public void mapSortForPropertyPathInPrimitiveShouldFallBackToColumnName() {
Sort sort = Sort.by(desc("alternative_name"));
Sort mapped = mapper.getMappedObject(sort, context.getRequiredPersistentEntity(Person.class));
assertThat(mapped.getOrderFor("alternative_name")).isEqualTo(desc("alternative_name"));
}
@Test // gh-369
public void mapQueryForPropertyPathInPrimitiveShouldFallBackToColumnName() {
Criteria criteria = Criteria.where("alternative_name").is("a");
BoundCondition bindings = map(criteria);
assertThat(bindings.getCondition().toString()).isEqualTo("person.alternative_name = ?[$1]");
}
private BoundCondition map(Criteria criteria) {
BindMarkersFactory markers = BindMarkersFactory.indexed("$", 1);