Skip to content

Commit f0f4fe7

Browse files
authored
MigrateJaxRsRecipe adds required=false to all @RequestParam with unset required (#422)
1 parent 3aeccb2 commit f0f4fe7

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import com.fasterxml.jackson.annotation.JsonIgnore;
2020
import lombok.*;
21+
import lombok.experimental.SuperBuilder;
2122
import org.openrewrite.Recipe;
2223
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.sbm.engine.context.ProjectContext;
2425

25-
@Builder
26-
@NoArgsConstructor
26+
@SuperBuilder
2727
@AllArgsConstructor
2828
public class OpenRewriteDeclarativeRecipeAdapter extends AbstractAction {
2929
@Setter
@@ -32,11 +32,17 @@ public class OpenRewriteDeclarativeRecipeAdapter extends AbstractAction {
3232

3333
@Autowired
3434
@JsonIgnore
35+
@Setter
3536
private RewriteRecipeLoader rewriteRecipeLoader;
3637
@JsonIgnore
3738
@Autowired
39+
@Setter
3840
private RewriteRecipeRunner rewriteRecipeRunner;
3941

42+
public OpenRewriteDeclarativeRecipeAdapter() {
43+
super(builder());
44+
}
45+
4046
@Override
4147
public boolean isApplicable(ProjectContext context) {
4248
return super.isApplicable(context);

components/sbm-core/src/test/java/org/springframework/sbm/engine/recipe/OpenRewriteDeclarativeRecipeAdapterIntegrationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ void adapterActionShouldExecuteOpenRewriteYamlRecipe() throws IOException {
6161
" type: org.springframework.sbm.common.migration.conditions.TrueCondition\n" +
6262
" actions:\n" +
6363
" - type: org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter\n" +
64+
" condition:\n" +
65+
" type: org.springframework.sbm.common.migration.conditions.TrueCondition\n" +
6466
" description: Call a OpenRewrite recipe\n" +
6567
" openRewriteRecipe: |-\n" +
6668
" type: specs.openrewrite.org/v1beta/recipe\n" +

components/sbm-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/MigrateJaxRsRecipe.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
import org.springframework.sbm.build.api.Dependency;
2424
import org.springframework.sbm.build.migration.actions.AddDependencies;
2525
import org.springframework.sbm.build.migration.conditions.NoExactDependencyExist;
26+
import org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter;
2627
import org.springframework.sbm.engine.recipe.Recipe;
28+
import org.springframework.sbm.engine.recipe.RewriteRecipeLoader;
29+
import org.springframework.sbm.engine.recipe.RewriteRecipeRunner;
2730
import org.springframework.sbm.java.JavaRecipeAction;
31+
import org.springframework.sbm.java.impl.ClasspathRegistry;
2832
import org.springframework.sbm.java.migration.actions.ReplaceTypeAction;
2933
import org.springframework.sbm.java.migration.conditions.HasAnnotation;
3034
import org.springframework.sbm.java.migration.conditions.HasImportStartingWith;
@@ -34,19 +38,21 @@
3438
import org.springframework.sbm.jee.jaxrs.recipes.SwapCacheControl;
3539
import org.springframework.sbm.jee.jaxrs.recipes.SwapHttHeaders;
3640
import org.springframework.sbm.jee.jaxrs.recipes.SwapResponseWithResponseEntity;
41+
import org.springframework.sbm.support.openrewrite.java.AddOrReplaceAnnotationAttribute;
3742

3843
import java.util.List;
3944
import java.util.function.Supplier;
4045

4146
@Configuration
42-
@RequiredArgsConstructor
47+
//@RequiredArgsConstructor
4348
public class MigrateJaxRsRecipe {
4449

4550

46-
private final JavaParser javaParserSupplier;
51+
private final JavaParser javaParserSupplier = JavaParser.fromJavaVersion().classpath(
52+
ClasspathRegistry.getInstance().getCurrentDependencies()).build();
4753

4854
@Bean
49-
public Recipe jaxRs() {
55+
public Recipe jaxRs(RewriteRecipeLoader rewriteRecipeLoader, RewriteRecipeRunner rewriteRecipeRunner) {
5056
return Recipe.builder()
5157
.name("migrate-jax-rs")
5258
.order(60)
@@ -121,6 +127,26 @@ public Recipe jaxRs() {
121127
.condition(HasImportStartingWith.builder().value("javax.ws.rs.core.Response").build())
122128
.description("Replace JaxRs Response and ResponseBuilder with it's Spring equivalent.")
123129
.recipe(new SwapResponseWithResponseEntity(() -> javaParserSupplier))
130+
.build(),
131+
132+
OpenRewriteDeclarativeRecipeAdapter.builder()
133+
.condition(HasAnnotation.builder().annotation("org.springframework.web.bind.annotation.RequestParam").build())
134+
.description("Adds required=false to all @RequestParam annotations")
135+
.rewriteRecipeLoader(rewriteRecipeLoader)
136+
.rewriteRecipeRunner(rewriteRecipeRunner)
137+
.openRewriteRecipe(
138+
"""
139+
type: specs.openrewrite.org/v1beta/recipe
140+
name: org.openrewrite.java.spring.boot3.data.UpgradeSpringData30
141+
displayName: Upgrade to SpringBoot 3.0
142+
description: 'Upgrade to SpringBoot to 3.0 from any prior version.'
143+
recipeList:
144+
- org.openrewrite.java.AddOrUpdateAnnotationAttribute:
145+
annotationType: "org.springframework.web.bind.annotation.RequestParam"
146+
attributeName: "required"
147+
attributeValue: "false"
148+
addOnly: true
149+
""")
124150
.build()
125151
)
126152
)

components/sbm-recipes-jee-to-boot/src/test/java/org/springframework/sbm/jee/jaxrs/recipes/BootifyJaxRsAnnotationsRecipeTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.sbm.jee.jaxrs.recipes;
1717

18+
import org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapter;
1819
import org.springframework.sbm.java.impl.RewriteJavaParser;
1920
import org.springframework.sbm.project.resource.SbmApplicationProperties;
2021
import org.springframework.sbm.test.RecipeTestSupport;
@@ -33,7 +34,7 @@ public class BootifyJaxRsAnnotationsRecipeTest {
3334
@Test
3435
void test() {
3536

36-
Recipe jaxRsRecipe = new MigrateJaxRsRecipe(new RewriteJavaParser(new SbmApplicationProperties())).jaxRs();
37+
Recipe jaxRsRecipe = new MigrateJaxRsRecipe().jaxRs(null, null);
3738
Optional<Recipe> recipe = Optional.of(jaxRsRecipe);
3839
RecipeTestSupport.assertThatRecipeExists(recipe);
3940
RecipeTestSupport.assertThatRecipeHasActions(recipe,
@@ -46,6 +47,7 @@ void test() {
4647
JavaRecipeAction.class,
4748
JavaRecipeAction.class,
4849
JavaRecipeAction.class,
49-
JavaRecipeAction.class);
50+
JavaRecipeAction.class,
51+
OpenRewriteDeclarativeRecipeAdapter.class);
5052
}
5153
}

components/sbm-support-boot/src/test/java/org/springframework/sbm/boot/common/conditions/HasSpringBootDependencyManuallyManagedTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.sbm.boot.common.conditions;
1817

1918
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)