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

Spring Framework 6.0 - ResponseStatusException method changes #554

Open
nmck257 opened this issue Jul 19, 2024 · 1 comment
Open

Spring Framework 6.0 - ResponseStatusException method changes #554

nmck257 opened this issue Jul 19, 2024 · 1 comment
Labels
good first issue Good for newcomers recipe Recipe requested

Comments

@nmck257
Copy link
Collaborator

nmck257 commented Jul 19, 2024

What problem are you trying to solve?

In Spring Framework 5.3, ResponseStatusException had methods getRawStatusCode() and getStatus():
https://docs.spring.io/spring-framework/docs/5.3.37/javadoc-api/org/springframework/web/server/ResponseStatusException.html

In Spring Framework 6.0, these are removed. The getStatusCode() method from superclass ErrorResponseException can be used as a direct replacement for getStatus(), and getStatusCode().value() can replace getRawStatusCode():
https://docs.spring.io/spring-framework/docs/6.0.0/javadoc-api/org/springframework/web/server/ResponseStatusException.html

What precondition(s) should be checked before applying this recipe?

None

Describe the situation before applying the recipe

class A {
    void foo(ResponseStatusException e) {
        int i = e.getRawStatusCode();
        HttpStatus s = e.getStatus();
    }
}

Describe the situation after applying the recipe

class A {
    void foo(ResponseStatusException e) {
        int i = e.getStatusCode().value();
        HttpStatus s = e.getStatusCode();
    }
}

Have you considered any alternatives or workarounds?

Any additional context

Are you interested in contributing this recipe to OpenRewrite?

Not urgent for me; probably a good first issue?

@nmck257 nmck257 added the recipe Recipe requested label Jul 19, 2024
@timtebeek timtebeek added the good first issue Good for newcomers label Jul 22, 2024
@timtebeek
Copy link
Contributor

Thanks for logging the suggested improvement; I've had a quick look at the getRawStatusCode case using the new trait API. I'm running up against some test class path issues that stop me from adding this as is, but figured log this here as a starter for folks that want to help out.

package org.openrewrite.java.spring.framework;

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 MigrateResponseStatusException 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()")
                .asVisitor((mc, ctx) -> {
                    MethodCall tree = mc.getTree();
                    if (tree instanceof J.MethodInvocation) {
                        return JavaTemplate.builder("#{any()}.getStatusCode().value()")
                                .contextSensitive()
                                .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-web-6"))
                                .build().apply(mc.getCursor(), tree.getCoordinates().replace(), ((J.MethodInvocation) tree).getSelect());
                    }
                    return tree;
                });
    }
}
package org.openrewrite.java.spring.framework;

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.recipe(new MigrateResponseStatusException())
          .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "spring-web-5.3"));
    }

    @DocumentExample
    @Issue("https://github.com/openrewrite/rewrite-spring/issues/554")
    @Test
    void migrateResponseStatusException() {
        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();
                  }
              }
              """
          )
        );
    }
}

The getStatus to getStatusCode change sounds like it could be a declarative yaml recipe for a quick first migration already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers recipe Recipe requested
Projects
Status: Backlog
Development

No branches or pull requests

2 participants