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

Add recipe to migrate ResponseStatusException changes #598

Merged
merged 4 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.framework;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.trait.Traits;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.MethodCall;

public class MigrateResponseStatusExceptionGetRawStatusCodeMethod extends Recipe {
@Override
public String getDisplayName() {
return "Migrate `ResponseStatusException#getRawStatusCode()` to `getStatusCode().value()`";
}

@Override
public String getDescription() {
return "Migrate Spring Framework 5.3's `ResponseStatusException` method `getRawStatusCode()` to Spring Framework 6's `getStatusCode().value()`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Traits.methodAccess("org.springframework.web.server.ResponseStatusException getRawStatusCode()")
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
.asVisitor((mc, ctx) -> {
MethodCall tree = mc.getTree();
if (tree instanceof J.MethodInvocation) {
return JavaTemplate.builder("#{any()}.getStatusCode().value()")
.contextSensitive()
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-core-6", "spring-beans-6", "spring-web-6"))
.build().apply(mc.getCursor(), tree.getCoordinates().replace(), ((J.MethodInvocation) tree).getSelect());
}
return tree;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
12 changes: 11 additions & 1 deletion src/main/resources/META-INF/rewrite/spring-framework-60.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ recipeList:
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5
- org.openrewrite.java.spring.framework.HttpComponentsClientHttpRequestFactoryReadTimeout
- org.openrewrite.java.spring.framework.MigrateResponseEntityExceptionHandlerHttpStatusToHttpStatusCode

- org.openrewrite.java.spring.framework.MigrateResponseStatusException
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.framework.MigrateResponseStatusException
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
displayName: Migrate breaking changes in `ResponseStatusException`
description: Migrate Spring Framework 5.3's `ResponseStatusException` method `getRawStatusCode()` to Spring Framework 6's `getStatusCode().value()` and `ResponseStatusException` method `getStatus()` to Spring Framework 6's `getStatusCode()` .
recipeList:
- org.openrewrite.java.spring.framework.MigrateResponseStatusExceptionGetRawStatusCodeMethod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps once the recipe for getStatus to getStatusCode is in place these could be included below line 33, or the recipe on line 33 could be pulled into here and this sub recipe could be transformed into a HttpStatus to HttpStatusCode recipe

- org.openrewrite.java.ChangeMethodName:
methodPattern: "org.springframework.web.server.ResponseStatusException getStatus()"
newMethodName: "getStatusCode"
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.framework.MigrateSpringAssert
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.framework;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

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

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

class MigrateResponseStatusExceptionTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResources("org.openrewrite.java.spring.framework.MigrateResponseStatusException")
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-core-5.3", "spring-beans-5.3", "spring-web-5.3"));
}

@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-spring/issues/554")
@Test
void migrateResponseStatusExceptionGetStatusMethod() {
rewriteRun(
//language=java
java(
"""
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
class A {
void foo(ResponseStatusException e) {
HttpStatus i = e.getStatus();
}
}
""",
"""
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
class A {
void foo(ResponseStatusException e) {
HttpStatus i = e.getStatusCode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Laurens-W is that something you can work in based on your earlier work on #589 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging this iteration already as it's an improvement already; we'd need to change the test in a follow up PR to expect the variable and import change, and create a new dedicated recipe to handle the ResponseStatusException getStatus().

}
}
"""
)
);
}

@DocumentExample
@Issue("https://github.com/openrewrite/rewrite-spring/issues/554")
@Test
void migrateResponseStatusExceptionGetRawStatusCodeMethod() {
rewriteRun(
//language=java
java(
"""
import org.springframework.web.server.ResponseStatusException;
class A {
void foo(ResponseStatusException e) {
int i = e.getRawStatusCode();
}
}
""",
"""
import org.springframework.web.server.ResponseStatusException;
class A {
void foo(ResponseStatusException e) {
int i = e.getStatusCode().value();
}
}
"""
)
);
}

}