Skip to content

Commit ee635a2

Browse files
Skip adding CrudRepository when already on Spring Data 3.x (#996)
1 parent 196eeb1 commit ee635a2

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/main/java/org/openrewrite/java/spring/data/MigratePagingAndSortingRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
6565
}
6666
}
6767

68+
// Only add CrudRepository when the classpath PagingAndSortingRepository still
69+
// extends CrudRepository (Spring Data 2.x). In 3.x the inheritance was removed
70+
// intentionally, so not extending CrudRepository may be a conscious choice.
6871
if (pagingAndSortingType != null && !alreadyHasCrud &&
72+
TypeUtils.isAssignableTo(crudRepoTarget,
73+
TypeUtils.asFullyQualified(pagingAndSortingType.getType())) &&
6974
pagingAndSortingType.getTypeParameters() != null &&
7075
pagingAndSortingType.getTypeParameters().size() == 2) {
7176

src/test/java/org/openrewrite/java/spring/data/MigratePagingAndSortingRepositoryTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,29 @@ public class NotAnInterface {
191191
)
192192
);
193193
}
194+
195+
@Test
196+
void noChangeWhenAlreadyOnSpringData3() {
197+
rewriteRun(
198+
spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),
199+
"spring-data-commons-3.*")),
200+
//language=java
201+
java(
202+
"""
203+
import org.springframework.data.repository.PagingAndSortingRepository;
204+
205+
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
206+
}
207+
"""
208+
),
209+
//language=java
210+
java(
211+
"""
212+
public class User {
213+
private Long id;
214+
}
215+
"""
216+
)
217+
);
218+
}
194219
}

0 commit comments

Comments
 (0)