Skip to content

Commit

Permalink
Correcting sort object reference in FetchableFluentQueryByExample.sor…
Browse files Browse the repository at this point in the history
…tBy and FetchableFluentQueryByPredicate.sortBy

Closes spring-projects#2438
  • Loading branch information
jonyschak committed Feb 13, 2022
1 parent f5b1380 commit bb76f07
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2022 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 Down Expand Up @@ -45,6 +45,7 @@
* @author Greg Turnquist
* @author Mark Paluch
* @author Jens Schauder
* @author J.R. Onyschak
* @since 2.6
*/
class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> implements FetchableFluentQuery<R> {
Expand Down Expand Up @@ -86,8 +87,8 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {

Assert.notNull(sort, "Sort must not be null!");

return new FetchableFluentQueryByExample<>(example, entityType, resultType, sort.and(sort), properties, finder,
countOperation, existsOperation, entityManager, escapeCharacter);
return new FetchableFluentQueryByExample<>(example, entityType, resultType, this.sort.and(sort), properties,
finder, countOperation, existsOperation, entityManager, escapeCharacter);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2022 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 Down Expand Up @@ -46,6 +46,7 @@
* @author Greg Turnquist
* @author Mark Paluch
* @author Jens Schauder
* @author J.R. Onyschak
* @since 2.6
*/
class FetchableFluentQueryByPredicate<S, R> extends FluentQuerySupport<S, R> implements FetchableFluentQuery<R> {
Expand Down Expand Up @@ -89,8 +90,8 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {

Assert.notNull(sort, "Sort must not be null!");

return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, sort.and(sort), properties, finder,
pagedFinder, countOperation, existsOperation, entityManager);
return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, this.sort.and(sort), properties,
finder, pagedFinder, countOperation, existsOperation, entityManager);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 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
*
* https://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.jpa.repository.support;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;

/**
* Unit tests for {@link FetchableFluentQueryByExample}.
*
* @author J.R. Onyschak
*/
class FetchableFluentQueryByExampleUnitTests {

@Test // GH-2438
@SuppressWarnings({ "rawtypes", "unchecked" })
void multipleSortBy() {
Sort s1 = Sort.by(Order.by("s1"));
Sort s2 = Sort.by(Order.by("s2"));
FetchableFluentQueryByExample f = new FetchableFluentQueryByExample(Example.of(""), null, null, null, null, null);
f = (FetchableFluentQueryByExample) f.sortBy(s1).sortBy(s2);
assertThat(f.sort).isEqualTo(s1.and(s2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 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
*
* https://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.jpa.repository.support;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;

/**
* Unit tests for {@link FetchableFluentQueryByPredicate}.
*
* @author J.R. Onyschak
*/
class FetchableFluentQueryByPredicateUnitTests {

@Test // GH-2438
@SuppressWarnings({ "rawtypes", "unchecked" })
void multipleSortBy() {
Sort s1 = Sort.by(Order.by("s1"));
Sort s2 = Sort.by(Order.by("s2"));
FetchableFluentQueryByPredicate f = new FetchableFluentQueryByPredicate(null, null, null, null, null, null, null);
f = (FetchableFluentQueryByPredicate) f.sortBy(s1).sortBy(s2);
assertThat(f.sort).isEqualTo(s1.and(s2));
}
}

0 comments on commit bb76f07

Please sign in to comment.