Skip to content

Commit

Permalink
Also handle J.NewArray in MigrateRequestMappingOnFeignClient
Browse files Browse the repository at this point in the history
Fixes #666
  • Loading branch information
timtebeek committed Jan 15, 2025
1 parent d0364cb commit 58357dd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.List;

public class MigrateRequestMappingOnFeignClient extends Recipe {

private static final String FEIGN_CLIENT = "org.springframework.cloud.openfeign.FeignClient";
Expand Down Expand Up @@ -122,6 +124,11 @@ private String getPathValue(Expression arg) {
}
}
}
} else if (arg instanceof J.NewArray) {
List<Expression> initializer = ((J.NewArray) arg).getInitializer();
if (initializer != null && initializer.size() == 1) {
return getPathValue(initializer.get(0));
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,43 @@ public interface MyServiceClient {
);
}

@Test
void requestMappingWithDefaultAttributeNameAndArray() {
rewriteRun(
//language=java
java(
"""
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "myService", url = "http://localhost:8080")
@RequestMapping({"/posts"})
public interface MyServiceClient {
@GetMapping(value = "/{postId}", produces = "application/json")
String getPostById(@PathVariable("postId") Long postId);
}
""",
"""
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(path = "/posts", name = "myService", url = "http://localhost:8080")
public interface MyServiceClient {
@GetMapping(value = "/{postId}", produces = "application/json")
String getPostById(@PathVariable("postId") Long postId);
}
"""
)
);
}

@Test
void requestMappingWithValueAttributeName() {
rewriteRun(
Expand Down

0 comments on commit 58357dd

Please sign in to comment.