Skip to content

Do not prefix unsafe order by expressions with table prefix. #1513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati

for (Sort.Order order : sort) {

SqlSort.validate(order);

OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
OrderByField orderBy = simpleOrderByField
.withNullHandling(order.getNullHandling());
Expand All @@ -105,7 +107,9 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati

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

SqlSort.validate(order);
if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
return OrderByField.from(Expressions.just(sqlOrder.getProperty()));
}

Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
return OrderByField.from(table.column(field.getMappedColumnName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public void shouldMapSortWithUnsafeExpression() {

assertThat(fields) //
.extracting(Objects::toString) //
.containsExactly("tbl." + unsafeExpression + " ASC");
.containsExactly( unsafeExpression + " ASC");
}

private Condition map(Criteria criteria) {
Expand Down
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.regex.Pattern;

import org.jetbrains.annotations.NotNull;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyPath;
Expand Down Expand Up @@ -50,6 +51,8 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import static org.springframework.data.relational.core.sql.Expressions.*;

/**
* Maps {@link CriteriaDefinition} and {@link Sort} objects considering mapping metadata and dialect-specific
* conversion.
Expand Down Expand Up @@ -102,7 +105,9 @@ public String toSql(SqlIdentifier identifier) {
* @param sort must not be {@literal null}.
* @param entity related {@link RelationalPersistentEntity}, can be {@literal null}.
* @return
* @deprecated without replacement.
*/
@Deprecated(since = "3.2", forRemoval = true)
public Sort getMappedObject(Sort sort, @Nullable RelationalPersistentEntity<?> entity) {

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

for (Sort.Order order : sort) {

SqlSort.validate(order);

OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
OrderByField orderBy = simpleOrderByField
.withNullHandling(order.getNullHandling());
Expand All @@ -147,9 +154,12 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati

}


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

SqlSort.validate(order);
if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
return OrderByField.from(Expressions.just(sqlOrder.getProperty()));
}

Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
return OrderByField.from(table.column(field.getMappedColumnName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.data.relational.core.sql.Functions;
import org.springframework.data.relational.core.sql.OrderByField;
import org.springframework.data.relational.core.sql.Table;
import org.springframework.data.relational.domain.SqlSort;
import org.springframework.r2dbc.core.Parameter;
import org.springframework.r2dbc.core.binding.BindMarkersFactory;
import org.springframework.r2dbc.core.binding.BindTarget;
Expand Down Expand Up @@ -440,6 +441,19 @@ public void shouldMapSortWithUnknownField() {
.containsExactly("tbl.unknownField DESC");
}

@Test // GH-1512
public void shouldTablePrefixUnsafeOrderExpression() {

Sort sort = Sort.by(SqlSort.SqlOrder.desc("unknownField").withUnsafe());

List<OrderByField> fields = mapper.getMappedSort(Table.create("tbl"), sort,
mapper.getMappingContext().getRequiredPersistentEntity(Person.class));

assertThat(fields) //
.extracting(Objects::toString) //
.containsExactly("unknownField DESC");
}

@Test // GH-1507
public void shouldMapSortWithAllowedSpecialCharacters() {

Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.0-1512-drop-table-prefix-for-unsafe-SNAPSHOT</version>
</parent>

<properties>
Expand Down