Expand path bindings from static initializer map put entries.

When a static map field is populated in a static block via put calls with compile-time string keys, expand endpoint path parameters through those entries while remaining fail-closed for computed keys.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-14 18:43:12 +02:00
parent 85af06c89d
commit 60cd6511d4
2 changed files with 129 additions and 4 deletions

View File

@@ -2994,7 +2994,7 @@ class EntryPointBindingExpanderTest {
}
@Test
void shouldNotExpandCommandKeyFromStaticInitializerMap(@TempDir Path tempDir) throws Exception {
void shouldExpandCommandKeyFromStaticInitializerMapPutEntries(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
@@ -3029,6 +3029,63 @@ class EntryPointBindingExpanderTest {
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
Map<String, List<click.kamil.springstatemachineexporter.analysis.model.CallEdge>> graph = engine.buildCallGraph();
EntryPoint entryPoint = EntryPoint.builder()
.className("com.example.GenericCommandController")
.methodName("execute")
.name("POST /api/commands/{commandKey}")
.parameters(List.of(EntryPoint.Parameter.builder()
.name("commandKey")
.type("String")
.annotations(List.of("PathVariable"))
.build()))
.build();
List<Map<String, String>> variants =
EntryPointBindingExpander.expandPathVariableBindings(entryPoint, context, graph);
assertThat(variants).extracting(map -> map.get("commandKey"))
.containsExactlyInAnyOrder("order.pay", "order.ship");
}
@Test
void shouldNotExpandCommandKeyFromStaticInitializerMapWithComputedPutKeys(@TempDir Path tempDir) throws Exception {
String source = """
package com.example;
import java.util.HashMap;
import java.util.Map;
public class GenericCommandController {
OrderGateway gateway;
public void execute(String commandKey) {
gateway.trigger(commandKey);
}
}
class OrderGateway {
private static final Map<String, OrderEvent> ROUTES;
static {
ROUTES = new HashMap<>();
ROUTES.put(prefix("order.pay"), OrderEvent.PAY);
ROUTES.put("order.ship", OrderEvent.SHIP);
}
static String prefix(String value) {
return value;
}
void trigger(String commandKey) {
StateMachine sm = new StateMachine();
sm.sendEvent(ROUTES.get(commandKey));
}
}
class StateMachine { void sendEvent(OrderEvent event) {} }
enum OrderEvent { PAY, SHIP }
""";
Files.writeString(tempDir.resolve("App.java"), source);
CodebaseContext context = new CodebaseContext();
context.setResolveBindings(true);
context.scan(tempDir);
HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
Map<String, List<click.kamil.springstatemachineexporter.analysis.model.CallEdge>> graph = engine.buildCallGraph();
EntryPoint entryPoint = EntryPoint.builder()
.className("com.example.GenericCommandController")
.methodName("execute")