Skip to content

Commit e9459b8

Browse files
schaudermp911de
authored andcommitted
Do not prefix unsafe orer by expressions with table prefix.
Such expressions now get passed on unchanged. This also deprecates org.springframework.data.r2dbc.query.QueryMapper.getMappedObject(Sort, RelationalPersistentEntity<?>). It was only used in tests and translates an Order into another Order, which sounds wrong. Closes #1512 Original pull request: #1513
1 parent 48df38e commit e9459b8

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/QueryMapper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati
9393

9494
for (Sort.Order order : sort) {
9595

96+
SqlSort.validate(order);
97+
9698
OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
9799
OrderByField orderBy = simpleOrderByField
98100
.withNullHandling(order.getNullHandling());
@@ -105,7 +107,9 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati
105107

106108
private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) {
107109

108-
SqlSort.validate(order);
110+
if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
111+
return OrderByField.from(Expressions.just(sqlOrder.getProperty()));
112+
}
109113

110114
Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
111115
return OrderByField.from(table.column(field.getMappedColumnName()));

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/QueryMapperUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ public void shouldMapSortWithUnsafeExpression() {
430430

431431
assertThat(fields) //
432432
.extracting(Objects::toString) //
433-
.containsExactly("tbl." + unsafeExpression + " ASC");
433+
.containsExactly( unsafeExpression + " ASC");
434434
}
435435

436436
private Condition map(Criteria criteria) {

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/query/QueryMapper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Map;
2323
import java.util.regex.Pattern;
2424

25+
import org.jetbrains.annotations.NotNull;
2526
import org.springframework.data.domain.Sort;
2627
import org.springframework.data.mapping.MappingException;
2728
import org.springframework.data.mapping.PersistentPropertyPath;
@@ -50,6 +51,8 @@
5051
import org.springframework.util.Assert;
5152
import org.springframework.util.ClassUtils;
5253

54+
import static org.springframework.data.relational.core.sql.Expressions.*;
55+
5356
/**
5457
* Maps {@link CriteriaDefinition} and {@link Sort} objects considering mapping metadata and dialect-specific
5558
* conversion.
@@ -102,7 +105,9 @@ public String toSql(SqlIdentifier identifier) {
102105
* @param sort must not be {@literal null}.
103106
* @param entity related {@link RelationalPersistentEntity}, can be {@literal null}.
104107
* @return
108+
* @deprecated without replacement.
105109
*/
110+
@Deprecated(since = "3.2", forRemoval = true)
106111
public Sort getMappedObject(Sort sort, @Nullable RelationalPersistentEntity<?> entity) {
107112

108113
if (entity == null) {
@@ -137,6 +142,8 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati
137142

138143
for (Sort.Order order : sort) {
139144

145+
SqlSort.validate(order);
146+
140147
OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
141148
OrderByField orderBy = simpleOrderByField
142149
.withNullHandling(order.getNullHandling());
@@ -147,9 +154,12 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati
147154

148155
}
149156

157+
150158
private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) {
151159

152-
SqlSort.validate(order);
160+
if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
161+
return OrderByField.from(Expressions.just(sqlOrder.getProperty()));
162+
}
153163

154164
Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
155165
return OrderByField.from(table.column(field.getMappedColumnName()));

spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/query/QueryMapperUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.data.relational.core.sql.Functions;
4040
import org.springframework.data.relational.core.sql.OrderByField;
4141
import org.springframework.data.relational.core.sql.Table;
42+
import org.springframework.data.relational.domain.SqlSort;
4243
import org.springframework.r2dbc.core.Parameter;
4344
import org.springframework.r2dbc.core.binding.BindMarkersFactory;
4445
import org.springframework.r2dbc.core.binding.BindTarget;
@@ -440,6 +441,19 @@ public void shouldMapSortWithUnknownField() {
440441
.containsExactly("tbl.unknownField DESC");
441442
}
442443

444+
@Test // GH-1512
445+
public void shouldTablePrefixUnsafeOrderExpression() {
446+
447+
Sort sort = Sort.by(SqlSort.SqlOrder.desc("unknownField").withUnsafe());
448+
449+
List<OrderByField> fields = mapper.getMappedSort(Table.create("tbl"), sort,
450+
mapper.getMappingContext().getRequiredPersistentEntity(Person.class));
451+
452+
assertThat(fields) //
453+
.extracting(Objects::toString) //
454+
.containsExactly("unknownField DESC");
455+
}
456+
443457
@Test // GH-1507
444458
public void shouldMapSortWithAllowedSpecialCharacters() {
445459

0 commit comments

Comments
 (0)