Skip to content

Commit

Permalink
Create spring 2.7 data YML file with getReferenceById changes (#518)
Browse files Browse the repository at this point in the history
* Create spring 2.7 data YML file with getReferenceById changes

* Include Spring Data migration with Spring Boot 2.7 migration

* Add a quick unit test, that fails for now

* Add DocumentExample

* ChangeMethodName for Spring Data 2.5 should match overrides

* Add missing spring-data-commons reference to test

* Drop unused test dependency

---------

Co-authored-by: Lucas Han <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored May 19, 2024
1 parent e9e3340 commit 4ecc3f2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/spring-boot-27.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ recipeList:
groupId: org.springframework
artifactId: "*"
newVersion: 5.3.x
- org.openrewrite.java.spring.data.UpgradeSpringData_2_7
- org.openrewrite.java.spring.security5.UpgradeSpringSecurity_5_7
# Use recommended replacements for deprecated APIs
- org.openrewrite.java.ChangeType:
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/rewrite/spring-data-25.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.springframework.data.jpa.repository.JpaRepository getOne(..)
newMethodName: getById
matchOverrides: true
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.data.UseJpaRepositoryDeleteAllInBatch
Expand All @@ -43,3 +44,4 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.springframework.data.jpa.repository.JpaRepository deleteInBatch(..)
newMethodName: deleteAllInBatch
matchOverrides: true
40 changes: 40 additions & 0 deletions src/main/resources/META-INF/rewrite/spring-data-27.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright 2024 the original author or authors.
# <p>
# 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
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# 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.
#

########################################################################################################################
# Spring Data 2.7
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.data.UpgradeSpringData_2_7
displayName: Migrate to Spring Data 2.7
description: Migrate applications to the latest Spring Data 2.7 release.
recipeList:
- org.openrewrite.java.spring.data.UpgradeSpringData_2_5
- org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById
---
type: specs.openrewrite.org/v1beta/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
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.openrewrite.java.spring.data;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class UseJpaRepositoryGetReferenceByIdTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-data-commons-2.7", "spring-data-jpa-2.7"))
.recipeFromResource("/META-INF/rewrite/spring-data-27.yml", "org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById");
}

@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);
}
}
"""
)
);
}
}

0 comments on commit 4ecc3f2

Please sign in to comment.