Skip to content

Commit

Permalink
fix: Check if the pageable sort already contains the additional sort.
Browse files Browse the repository at this point in the history
If it does or is equal to, don't add it a second time.

Fixes #2940
  • Loading branch information
michael-simons committed Aug 20, 2024
1 parent af59d65 commit 79d8aaf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -220,7 +221,10 @@ private QueryFragments createQueryFragments(@Nullable Condition condition, Sort
queryFragments.setReturnExpression(Cypher.count(Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription)), true);
} else {

var theSort = pagingParameter.getSort().and(sort);
var theSort = pagingParameter.getSort();
if (!Objects.equals(theSort, sort)) {
theSort = theSort.and(sort);
}

if (pagingParameter.isUnpaged() && scrollPosition == null && maxResults != null) {
queryFragments.setLimit(limitModifier.apply(maxResults.intValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.neo4j.cypherdsl.core.Condition;
import org.neo4j.cypherdsl.core.Cypher;
import org.neo4j.cypherdsl.core.LabelExpression;
Expand Down Expand Up @@ -200,19 +201,24 @@
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.repository.query.QueryFragmentsAndParameters;
import org.springframework.data.neo4j.test.BookmarkCapture;
import org.springframework.data.neo4j.test.LogbackCapture;
import org.springframework.data.neo4j.test.LogbackCapturingExtension;
import org.springframework.data.neo4j.test.Neo4jImperativeTestConfiguration;
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import ch.qos.logback.classic.Level;

/**
* @author Michael J. Simons
* @soundtrack Sodom - Sodom
*/
@Neo4jIntegrationTest
@DisplayNameGeneration(SimpleDisplayNameGeneratorWithTags.class)
@TestMethodOrder(MethodOrderer.DisplayName.class)
@ExtendWith(LogbackCapturingExtension.class)
class IssuesIT extends TestBase {

// GH-2210
Expand Down Expand Up @@ -1657,6 +1663,20 @@ void shouldSupportGeoResultWithSelfRef(@Autowired LocatedNodeWithSelfRefReposito
assertSupportedGeoResultBehavior(repository);
}

@Test
@Tag("GH-2940")
void shouldNotGenerateDuplicateOrder(@Autowired LocatedNodeRepository repository, LogbackCapture logbackCapture) {

try {
logbackCapture.addLogger("org.springframework.data.neo4j.cypher", Level.DEBUG);
var nodes = repository.findAllByName("NEO4J_HQ", PageRequest.of(0, 10, Sort.by(Sort.Order.asc("name"))));
assertThat(nodes).isNotEmpty();
assertThat(logbackCapture.getFormattedMessages()).noneMatch(l -> l.contains("locatedNode.name, locatedNode.name"));
} finally {
logbackCapture.resetLogLevel();
}
}

@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories(namedQueriesLocation = "more-custom-queries.properties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package org.springframework.data.neo4j.integration.issues.gh2908;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.neo4j.repository.Neo4jRepository;

/**
* Repository spotting all supported Geo* results.
* @author Michael J. Simons
*/
public interface LocatedNodeRepository extends HasNameAndPlaceRepository<LocatedNode>, Neo4jRepository<LocatedNode, String> {

Page<LocatedNode> findAllByName(String whatever, PageRequest name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.jupiter.api.extension.ExtensionContext;

Expand Down Expand Up @@ -54,7 +53,7 @@ public void addLogger(String loggerName, Level level) {
}

public List<String> getFormattedMessages() {
return listAppender.list.stream().map(e -> e.getFormattedMessage()).collect(Collectors.toList());
return listAppender.list.stream().map(ILoggingEvent::getFormattedMessage).toList();
}

void start() {
Expand All @@ -75,6 +74,6 @@ public void close() {
}

public void resetLogLevel() {
this.additionalLoggers.entrySet().forEach(entry -> entry.getKey().setLevel(entry.getValue()));
this.additionalLoggers.forEach(Logger::setLevel);
}
}

0 comments on commit 79d8aaf

Please sign in to comment.