[Bug]: Spring DI resolver sends calls on a concrete injected type to its only subclass, including a test-source subclass
What happens
When a field's injected type is a concrete class and exactly one class extends it, resolve_spring_di_calls rewrites every call on that field to the subclass's method. That happens when the subclass lives under src/test/. Spring injects the declared bean there, not the subclass, so each production caller ends up pointing at test code.
code_review_graph/spring_resolver.py at v2.3.9:
- lines 101–112 build
implementorsfrom everyINHERITSedge whose source is Java, Kotlin or Scala. Test-source classes and non-interface parents are included. - lines 171–175 take the only implementor whenever
len(impls) == 1. They never check whether the injected type is an interface or abstract class.
Reproduction (v2.3.9, code-review-graph build)
src/main/java/app/Sink.java
package app;
import org.springframework.stereotype.Component;
@Component
public class Sink {
public void emit(String event) { }
}src/main/java/app/Publisher.java
package app;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class Publisher {
private final Sink sink;
public void publish(String event) { sink.emit(event); }
}src/test/java/app/RecordingSinkConfig.java
package app;
import org.springframework.boot.test.context.TestConfiguration;
@TestConfiguration
public class RecordingSinkConfig {
public static final class RecordingSink extends Sink {
@Override
public void emit(String event) { }
}
}Resulting edge:
CALLS src/main/java/app/Publisher.java::Publisher.publish
-> src/test/java/app/RecordingSinkConfig.java::RecordingSinkConfig.RecordingSink.emit
extra = {"receiver": "sink", "spring_resolved": true, "injected_type": "Sink"}Expected: src/main/java/app/Sink.java::Sink.emit.
The same thing happens if RecordingSinkConfig.java is moved under src/main/. So there are two separate problems: test sources are counted as implementors, and a concrete injected type is replaced by its subclass.
Impact
In a real Spring Boot service, 20 production call sites of one event-sink class resolved to a test recording subclass. callers_of on the real emit method returned 3 of 19 production callers, and "which production code reaches this sink" had no correct answer. A test double that extends a production bean is common in Spring test setups, so this pattern is likely widespread.
Suggested fix
- Only replace the declared type with an implementor when the injected type is an interface or abstract class. A concrete class is itself the bean Spring injects.
- Exclude test-source classes (
is_test, or files under test roots) fromimplementorswhen resolving calls from non-test code.
Source: tirth8205/code-review-graph