Skip to content

DATACMNS-621 - QSort treats long paths incorrectly. #111

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-commons</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATACMNS-621-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
29 changes: 26 additions & 3 deletions src/main/java/org/springframework/data/querydsl/QSort.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;
Expand All @@ -31,6 +32,7 @@
* Sort option for queries that wraps a querydsl {@link OrderSpecifier}.
*
* @author Thomas Darimont
* @author Christoph Strobl
*/
public class QSort extends Sort implements Serializable {

Expand Down Expand Up @@ -87,8 +89,8 @@ private static Order toOrder(OrderSpecifier<?> orderSpecifier) {
Assert.notNull(orderSpecifier, "Order specifier must not be null!");

Expression<?> target = orderSpecifier.getTarget();
Object targetElement = target instanceof Path ? ((com.mysema.query.types.Path<?>) target).getMetadata()
.getElement() : target;

Object targetElement = target instanceof Path ? preparePropertyPath((Path<?>) target) : target;

Assert.notNull(targetElement, "Target element must not be null!");

Expand Down Expand Up @@ -142,4 +144,25 @@ public QSort and(OrderSpecifier<?>... orderSpecifiers) {
Assert.notEmpty(orderSpecifiers, "OrderSpecifiers must not be null or empty!");
return and(Arrays.asList(orderSpecifiers));
}

private static String preparePropertyPath(Path<?> path) {

Stack<String> stack = new Stack<String>();
Path<?> pathElement = path;
while (pathElement.getMetadata() != null && pathElement.getMetadata().getParent() != null) {

stack.push(pathElement.getMetadata().getElement().toString());
pathElement = pathElement.getMetadata().getParent();
}

StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
if (!stack.isEmpty()) {
sb.append(".");
}
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,8 @@
*/
package org.springframework.data.querydsl;

import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.List;

Expand All @@ -34,6 +33,7 @@
*
* @author Thomas Darimont
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class QSortUnitTests {

Expand Down Expand Up @@ -147,7 +147,7 @@ public void concatenatesPlainSortCorrectly() {
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname")));
}

/**
* @see DATACMNS-566
*/
Expand All @@ -159,6 +159,44 @@ public void shouldSupportSortByOperatorExpressions() {

Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString())));
assertThat(
result,
hasItems(new Order(Direction.ASC, "lastname"),
new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString())));
}

/**
* @see DATACMNS-621
*/
@Test
public void shouldCreateSortForNestedPathCorrectly() {

QSort sort = new QSort(QUserWrapper.userWrapper.user.firstname.asc());

assertThat(sort, hasItems(new Order(Direction.ASC, "user.firstname")));
}

/**
* @see DATACMNS-621
*/
@Test
public void shouldCreateSortForDeepNestedPathCorrectly() {

QSort sort = new QSort(QWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname.asc());

assertThat(sort, hasItems(new Order(Direction.ASC, "wrapper.user.firstname")));
}

/**
* @see DATACMNS-621
*/
@Test
public void shouldCreateSortForReallyDeepNestedPathCorrectly() {

QSort sort = new QSort(
QWrapperToWrapWrapperForUserWrapper.wrapperToWrapWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname
.asc());

assertThat(sort, hasItems(new Order(Direction.ASC, "wrapperForUserWrapper.wrapper.user.firstname")));
}
}
28 changes: 28 additions & 0 deletions src/test/java/org/springframework/data/querydsl/UserWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.querydsl;

import com.mysema.query.annotations.QueryEntity;

/**
* @author Christoph Strobl
*/
@QueryEntity
public class UserWrapper {

User user;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.querydsl;

import com.mysema.query.annotations.QueryEntity;

/**
* @author Christoph Strobl
*/
@QueryEntity
public class WrapperForUserWrapper {

UserWrapper wrapper;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.querydsl;

import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.annotations.QueryInit;

/**
* @author Christoph Strobl
*/
@QueryEntity
public class WrapperToWrapWrapperForUserWrapper {

@QueryInit("wrapper.user")//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 slashes are probably not needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes and no, spring data code formatter would otherwise move everything into one line

WrapperForUserWrapper wrapperForUserWrapper;
}