From d83dd7e1387bdf7f7b65df238c7e27c32b13873f Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 25 Oct 2023 14:48:16 +0200 Subject: [PATCH] Un-mangle Kotlin method names in PartTree. Due to shortcomings with value classes mangling and the impossible usage of JvmName on Kotlin interfaces, we're inspecting now the method name if it contains a dash (-). If so, then we truncate the method name to be able to parse it. Closes #2965 --- .../data/repository/query/parser/PartTree.java | 6 ++++++ .../data/repository/query/parser/PartTreeUnitTests.java | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/repository/query/parser/PartTree.java b/src/main/java/org/springframework/data/repository/query/parser/PartTree.java index 647b8f109..992c171d9 100644 --- a/src/main/java/org/springframework/data/repository/query/parser/PartTree.java +++ b/src/main/java/org/springframework/data/repository/query/parser/PartTree.java @@ -87,6 +87,12 @@ public class PartTree implements Streamable { Assert.notNull(source, "Source must not be null"); Assert.notNull(domainClass, "Domain class must not be null"); + // Kotlin name mangling, @JvmName cannot be used with interfaces + int dash = source.indexOf('-'); + if (dash > -1) { + source = source.substring(0, dash); + } + Matcher matcher = PREFIX_TEMPLATE.matcher(source); if (!matcher.find()) { diff --git a/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java b/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java index 00a3acd10..4c046098d 100755 --- a/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java @@ -27,7 +27,6 @@ import java.util.Date; import java.util.List; import org.junit.jupiter.api.Test; - import org.springframework.data.domain.Limit; import org.springframework.data.domain.Sort; import org.springframework.data.mapping.PropertyPath; @@ -657,6 +656,14 @@ class PartTreeUnitTests { assertThat(tree.getSort()).hasSize(1); } + @Test // GH-2965 + void unmangleKotlinMethodName() { + + var tree = new PartTree("findById-u1QWhUI", Order.class); + + assertThat(tree.getParts()).hasSize(1); + } + private static void assertLimiting(String methodName, Class entityType, boolean limiting, Integer maxResults) { assertLimiting(methodName, entityType, limiting, maxResults, false); }