Skip to content
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

JpaRepository#getReferenceById(ID id) recipe didn't replace all getById() method calls #658

Open
rogeriofrsouza opened this issue Jan 5, 2025 · 1 comment
Labels
boot-2.7 bug Something isn't working spring-data

Comments

@rogeriofrsouza
Copy link

What version of OpenRewrite are you using?

I am using the latest version, probably 8.42.0

How are you running OpenRewrite?

I am using the Maven Command Line, and my project is a single module project, not public.

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById -Drewrite.exportDatatables=true

What is the smallest, simplest way to reproduce the problem?

A project with JPA interfaces with or without an inheritance on JpaSpecificationExecutor<Company>.

public interface JPACompany extends JpaRepository<Company, Long>,
        JpaSpecificationExecutor<Company> {
        // some methods...
}
@RequiredArgsConstructor
@Component
public class Foo {

    private final JPACompany jpaCompany;

    public Company getCompany(Long id) {
        return jpaCompany.getById(id));
    }
}

What did you expect to see?

@RequiredArgsConstructor
@Component
public class Foo {

    private final JPACompany jpaCompany;

    public Company getCompany(Long id) {
        return jpaCompany.getReferenceById(id));
    }
}

What did you see instead?

JPA method getById wasn't replaced.

What is the full stack trace of any errors you encountered?

No errors.

Are you interested in contributing a fix to OpenRewrite?

Yes, I can try to help with some guidance.

@rogeriofrsouza rogeriofrsouza added the bug Something isn't working label Jan 5, 2025
@timtebeek
Copy link
Contributor

hi @rogeriofrsouza ; thanks for the report & offer to help. The best way to get started is with a draft PR containing an additional test, adjusting this reference test to match the situation you're seeing:

@Test
@DocumentExample
void matchAndUpdateReferences() {
//language=java
rewriteRun(
java(
"""
package foo;
public class Book {}
"""
),
java(
"""
package foo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BookRepository extends JpaRepository<Book, Long> {
}
"""
),
java(
"""
import foo.*;
class A {
BookRepository repo;
void method(Long id) {
repo.getById(id);
repo.getOne(id);
}
}
""",
"""
import foo.*;
class A {
BookRepository repo;
void method(Long id) {
repo.getReferenceById(id);
repo.getReferenceById(id);
}
}
"""
)
);
}
}

Then we can from there look to see if there's any adjustments we need to make to the recipe:

name: org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById
displayName: Use `JpaRepository#getReferenceById(ID id)`
description: '`JpaRepository#getOne(ID)` was deprecated in 2.5 and `JpaRepository#getById(ID)` was deprecated in 2.7.'
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.springframework.data.jpa.repository.JpaRepository getById(..)
newMethodName: getReferenceById
matchOverrides: true
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.springframework.data.jpa.repository.JpaRepository getOne(..)
newMethodName: getReferenceById
matchOverrides: true

If you're finding you can not reproduce the issue with a unit test, then it might make sense to have a look at alternative causes:
https://docs.openrewrite.org/reference/faq#my-recipe-runs-but-is-not-making-any-changes-whats-happening

@timtebeek timtebeek moved this to Backlog in OpenRewrite Jan 5, 2025
@timtebeek timtebeek changed the title JpaRepository#getReferenceById(ID id) recipe didn't replace all getById() method calls JpaRepository#getReferenceById(ID id) recipe didn't replace all getById() method calls Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
boot-2.7 bug Something isn't working spring-data
Projects
Status: Backlog
Development

No branches or pull requests

2 participants