Skip to content

Commit 933a84b

Browse files
committed
MigrateJaxRsRecipe adds required=false to all @RequestParam with unset required
1 parent eb3d6ed commit 933a84b

File tree

8 files changed

+94
-5
lines changed

8 files changed

+94
-5
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-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/MigrateJaxRsRecipe.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
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;
2831
import org.springframework.sbm.java.migration.actions.ReplaceTypeAction;
2932
import org.springframework.sbm.java.migration.conditions.HasAnnotation;
@@ -34,6 +37,7 @@
3437
import org.springframework.sbm.jee.jaxrs.recipes.SwapCacheControl;
3538
import org.springframework.sbm.jee.jaxrs.recipes.SwapHttHeaders;
3639
import org.springframework.sbm.jee.jaxrs.recipes.SwapResponseWithResponseEntity;
40+
import org.springframework.sbm.support.openrewrite.java.AddOrReplaceAnnotationAttribute;
3741

3842
import java.util.List;
3943
import java.util.function.Supplier;
@@ -46,7 +50,7 @@ public class MigrateJaxRsRecipe {
4650
private final JavaParser javaParserSupplier;
4751

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class BootifyJaxRsAnnotationsRecipeTest {
3333
@Test
3434
void test() {
3535

36-
Recipe jaxRsRecipe = new MigrateJaxRsRecipe(new RewriteJavaParser(new SbmApplicationProperties())).jaxRs();
36+
Recipe jaxRsRecipe = new MigrateJaxRsRecipe(new RewriteJavaParser(new SbmApplicationProperties())).jaxRs(null, null);
3737
Optional<Recipe> recipe = Optional.of(jaxRsRecipe);
3838
RecipeTestSupport.assertThatRecipeExists(recipe);
3939
RecipeTestSupport.assertThatRecipeHasActions(recipe,

components/sbm-support-boot/src/main/java/org/springframework/sbm/boot/common/conditions/IsSpringBootProject.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.sbm.boot.common.conditions;
217

318
import org.springframework.sbm.engine.context.ProjectContext;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.sbm.boot.common.conditions;
217

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

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;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.sbm.boot.common.conditions;
217

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.sbm.boot.common.conditions;
217

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

0 commit comments

Comments
 (0)