GitNexus/gitnexus/tools/compiler-conformance/java/test/provider/spring-interface-inheritance.ts
Gergő Magyar 50228e11ab
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
fix(java): match javac resolution and move fixtures into conformance (#4056)
* test(java): compare overload resolution with javac in CI

* test(conformance): share call resolution reports across tools

* test(java): cover the overload conversion matrix with javac (#4057)

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>

* test(java): run Java-backed fixtures in dedicated CI shards

* test(java): replace handwritten resolution cases with javac comparisons

* test(java): compare lambda and receiver fixtures against javac

* fix(ci): restore Java test evidence and use locked toolchain commands

* test(skills): fail incomplete CLI generation checks (#4056)

* test(java): compare method references with javac

* fix(java): resolve qualified bases and static wildcard overloads

* test(java): preserve declared dollar names in compiler comparisons

* fix(java): preserve distinct call targets for colliding type names

* fix(conformance): resolve private nested bases and retain overload selections

* fix(java): distinguish variadic overloads in the published graph

* fix(java): match javac for nested types, imports and initializer calls

* Move Java fixture testing out of Vitest and into compiler conformance

* Verify Java graph endpoints and leave call selection to javac

* Match javac for annotated bases and legal static imports

* Resolve nested Java member types and clean up failed conformance fixtures

* Preserve qualified Java bases across unrelated static wildcard imports

* test(java): run inline source fixtures in compiler conformance

* test(conformance): check fetch callers, lexical bindings and cleanup

* test(java): move virtual source and text fixtures into conformance

* fix(java): resolve imported member types and qualified base collisions

* Match javac constructor overloads and preserve Java conformance coverage

* Compare recovered Java inheritance with javac in the conformance runner

* Run the Java query compilation smoke test in native conformance

* Clarify Java conformance expectations and check populated suites

* Write javac conformance records as UTF-8 on Windows

* Compile the Java conformance tools for SonarCloud analysis

* Compare Java method implementation edges with javac

* Resolve fixture writers and inherited Java methods in their lexical context

* Resolve qualified Java bases through their accessible type members

* Match local qualified Java bases and bounded varargs identities to javac

* Match Java call resolution to javac across the conformance corpus

Preserve generic declaration identity, follow nested call results, and type lambda aliases and enhanced-loop variables in their declaration scopes. Report proven JDK call targets without creating workspace edges. Compare every selected source and binary call against javac and remove the twelve mismatch lists.

* Compare Java recovery targets as sets and check empty destination addresses

Keep repeated uses of the same declaration equivalent across graph producers while detecting missing and extra targets. Check raw destination addresses and document the source and classpath inputs to the compiler comparison.

* Avoid extra native parent walks while capturing Java call facts

Index lambda origins by spelling before walking lexical scopes, and only check value shadowing for qualifiers rooted in an identifier. This restores the capture traversal budget for anonymous classes while preserving javac call targets.

* Refresh the Java capture fingerprint for corrected fixture comments

The module captures include the revised comments and the following declarations move to earlier lines. Both emitters produce the same 4,288 capture groups when given the same fixture revision.

* Preserve Java conformance assertions and use trusted fixture paths

Use explicit canonical sort orders, require missing evidence to fail assertions, and keep pending query results in the replay cache. Resolve the Git executable independently of the test repository and isolate the Spring workspace per test.

* Resolve inherited Java members through the scoped superclass

* Reuse validated analyzer build hashes for parser cache identity

* Resolve SonarCloud findings in Java resolution and compiler conformance

* Align Java compiler tool packages and pin benchmark baselines to the tested merge

* Honor wildcard method families and fixture ownership in Java conformance

* fix(java): resolve inherited and annotation member types against javac

Keep workspace links tied to import declarations and compare Java varargs overloads through comments. Make generated-project selection, method metadata checks and Java fixture ownership explicit in conformance.

* fix(java): resolve bounded and imported bases during dispatch

Keep the type selected from a qualified heritage clause available during overload resolution. Compare annotated bounds, inherited static imports, and String.valueOf arguments directly with javac. Detect Java fixture paths on either side of a filesystem rename.

* Conform Java method visibility and wildcard lambda bounds to javac

Compare method access at source positions, recognize CommonJS fixture reads, and preserve diagnostics when native conformance setup fails.

* Match javac for qualified member overloads and selective call diagnostics

* Track reassigned Java fixture paths at their point of use

* Respect Java visibility and Node path semantics in conformance checks

* Align Java graph benchmarks with compiler-verified visibility

* Record the public Java record accessor in the mixed-provider benchmark

* Address remaining Java conformance analysis findings

Keep repository import scans sequential, simplify Java header and overload resolution, and reuse Spring conformance measurements. Preserve compiler comparison assertions and emitted fixture contents.

* Match javac on nested binary names, null factories and recovered calls

Preserve the qualified annotation target in workspace links. Compare valid and invalid arguments separately so an attributed recovery symbol cannot mask a failed invocation.

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-25 18:03:32 +00:00

401 lines
13 KiB
TypeScript

/**
* Unit coverage for the ingestion-side Spring interface-inheritance resolution
* (#2288): a concrete `@RestController` inherits the `@*Mapping`s declared on
* the interface it implements. This pins the two pieces the cross-file pipeline
* pass wires together:
* 1. `extractSpringTypes` — the per-file `SharedSpringType` collector.
* 2. `resolveInheritedSpringRoutes` — the shared, language-agnostic algorithm
* (also used by the group Java/Kotlin plugins) that attributes inherited
* routes to the implementing controller.
*
* It also pins the suppression half: `extractSpringRoutes` must NOT emit an
* interface method's own `@*Mapping` as a standalone route (that route is
* resolved onto the controller by the inheritance pass instead).
*/
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import Parser from 'tree-sitter';
import Java from 'tree-sitter-java';
import {
extractSpringRoutes,
extractSpringTypes,
} from '../../../../../src/core/ingestion/route-extractors/spring.js';
import { resolveInheritedSpringRoutes } from '../../../../../src/core/ingestion/route-extractors/spring-shared.js';
import { JAVA_HTTP_PLUGIN } from '../../../../../src/core/group/extractors/http-patterns/java.js';
import { runProjectScan } from '../../../../../test/helpers/run-project-scan.js';
function parse(src: string): Parser.Tree {
const p = new Parser();
p.setLanguage(Java);
return p.parse(src);
}
/** Run extractSpringTypes over several files and resolve inherited routes. */
function inherited(files: Array<{ path: string; src: string }>) {
const types = files.flatMap((f) => extractSpringTypes(parse(f.src), f.path));
return resolveInheritedSpringRoutes(types).map((r) => ({
filePath: r.filePath,
methodName: r.methodName,
key: `${r.method} ${r.path}`,
}));
}
/** ingestion inherited routes as a `METHOD path` set (cross-file pass result). */
function ingestionInheritedKeys(files: Array<{ path: string; src: string }>): Set<string> {
return new Set(inherited(files).map((r) => r.key));
}
/** group inherited provider routes via the project-level scan, as a `METHOD path` set. */
function groupInheritedKeys(files: Array<{ path: string; src: string }>): Set<string> {
const inputs = files.map((f) => ({ filePath: f.path, tree: parse(f.src) }));
const out = new Set<string>();
for (const fileDet of runProjectScan(JAVA_HTTP_PLUGIN, inputs)) {
for (const d of fileDet.detections) {
if (d.role === 'provider') out.add(`${d.method} ${d.path}`);
}
}
return out;
}
describe('Spring interface-inheritance resolution (ingestion, #2288)', () => {
it('attributes an interface-declared route to the implementing controller', () => {
const iface = {
path: 'OrderApi.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
public interface OrderApi {
@GetMapping("/orders") Object list();
}
`,
};
const controller = {
path: 'OrderController.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
public class OrderController implements OrderApi {
public Object list() { return null; }
}
`,
};
const routes = inherited([iface, controller]);
assert.deepEqual(routes, [
{ filePath: 'OrderController.java', methodName: 'list', key: 'GET /orders' },
]);
});
it('joins both the interface and controller class prefixes (no doubling)', () => {
const iface = {
path: 'Api.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/api")
public interface Api {
@GetMapping("/list") Object list();
@PostMapping({"/a", "/b"}) Object multi();
}
`,
};
const controller = {
path: 'C.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/v1")
public class C implements Api {
public Object list() { return null; }
public Object multi() { return null; }
}
`,
};
const keys = new Set(inherited([iface, controller]).map((r) => r.key));
assert.deepEqual(keys, new Set(['GET /v1/api/list', 'POST /v1/api/a', 'POST /v1/api/b']));
});
it('does NOT inherit a route the controller overrides with its own @*Mapping', () => {
const iface = {
path: 'Api.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
public interface Api {
@GetMapping("/from-iface") Object get();
}
`,
};
const controller = {
path: 'C.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
public class C implements Api {
@GetMapping("/own") public Object get() { return null; }
}
`,
};
// The controller's own @GetMapping wins; the interface route is not also added.
assert.deepEqual(inherited([iface, controller]), []);
});
it('extractSpringRoutes suppresses an interface method route (handled by the pass)', () => {
const iface = `package com.example;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/api")
public interface OrderApi {
@GetMapping("/orders") Object list();
}
`;
// The interface file on its own must yield NO standalone route.
assert.deepEqual(extractSpringRoutes(parse(iface), 'OrderApi.java'), []);
});
it('still emits concrete class routes unchanged', () => {
const ctrl = `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class C {
@GetMapping("/x") public Object x() { return null; }
}
`;
const routes = extractSpringRoutes(parse(ctrl), 'C.java');
assert.equal(routes.length, 1);
assert.partialDeepStrictEqual(routes[0], {
routePath: '/x',
httpMethod: 'GET',
prefix: '/api',
});
});
});
describe('Spring interface-inheritance parity — ingestion vs group projectScan (#2078)', () => {
// The strongest anti-drift guard: the same fixture must yield the same
// inherited provider routes from the ingestion cross-file pass and the group
// project-level scan. Both call the shared resolveInheritedSpringRoutes, so
// this pins that the two SharedSpringType collectors agree on every shape.
it('agrees on a plain interface-inherited route', () => {
const files = [
{
path: 'OrderApi.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
public interface OrderApi { @GetMapping("/orders") Object list(); }
`,
},
{
path: 'OrderController.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
public class OrderController implements OrderApi { public Object list() { return null; } }
`,
},
];
assert.deepEqual(ingestionInheritedKeys(files), groupInheritedKeys(files));
assert.deepEqual(groupInheritedKeys(files), new Set(['GET /orders']));
});
it('agrees with interface + controller prefixes and an array mapping', () => {
const files = [
{
path: 'Api.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/api")
public interface Api {
@GetMapping("/list") Object list();
@PostMapping({"/a", "/b"}) Object multi();
}
`,
},
{
path: 'C.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/v1")
public class C implements Api {
public Object list() { return null; }
public Object multi() { return null; }
}
`,
},
];
const group = groupInheritedKeys(files);
// Pin the expected set — this is the array-form fixture (the #2265 root),
// so the parity assertion must not be vacuously true on two empty sets.
assert.deepEqual(group, new Set(['POST /v1/api/a', 'POST /v1/api/b', 'GET /v1/api/list']));
assert.deepEqual(ingestionInheritedKeys(files), group);
});
it('agrees on inherited @RequestMapping verbs and wildcard mappings', () => {
const files = [
{
path: 'LegacyApi.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/api")
public interface LegacyApi {
@RequestMapping(path = "/save", method = RequestMethod.POST) Object save();
@RequestMapping("/all") Object all();
}
`,
},
{
path: 'LegacyController.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
public class LegacyController implements LegacyApi {
public Object save() { return null; }
public Object all() { return null; }
}
`,
},
];
const expected = new Set(['POST /api/save', '* /api/all']);
assert.deepEqual(groupInheritedKeys(files), expected);
assert.deepEqual(ingestionInheritedKeys(files), expected);
});
it('agrees on fully-qualified annotation names', () => {
// Both sides normalise an FQN annotation to its trailing segment, so an
// interface using `@org.springframework...GetMapping` still resolves.
const files = [
{
path: 'Api.java',
src: `package com.example;
@org.springframework.web.bind.annotation.RequestMapping("/api")
public interface Api {
@org.springframework.web.bind.annotation.GetMapping("/list") Object list();
}
`,
},
{
path: 'C.java',
src: `package com.example;
@org.springframework.web.bind.annotation.RestController
public class C implements Api { public Object list() { return null; } }
`,
},
];
assert.deepEqual(ingestionInheritedKeys(files), groupInheritedKeys(files));
assert.deepEqual(groupInheritedKeys(files), new Set(['GET /api/list']));
});
it('agrees that an ambiguous (duplicated) interface name drops its routes', () => {
const files = [
{
path: 'a/Api.java',
src: `package a;
import org.springframework.web.bind.annotation.*;
public interface Api { @GetMapping("/x") Object x(); }
`,
},
{
path: 'b/Api.java',
src: `package b;
import org.springframework.web.bind.annotation.*;
public interface Api { @GetMapping("/y") Object x(); }
`,
},
{
path: 'C.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
public class C implements Api { public Object x() { return null; } }
`,
},
];
// Ambiguous interface name → both sides drop the inherited routes.
assert.deepEqual(ingestionInheritedKeys(files), groupInheritedKeys(files));
assert.deepEqual(ingestionInheritedKeys(files), new Set());
});
it('agrees on a controller implementing multiple interfaces', () => {
const files = [
{
path: 'ReadApi.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
public interface ReadApi { @GetMapping("/read") Object read(); }
`,
},
{
path: 'WriteApi.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
public interface WriteApi { @PostMapping("/write") Object write(); }
`,
},
{
path: 'C.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class C implements ReadApi, WriteApi {
public Object read() { return null; }
public Object write() { return null; }
}
`,
},
];
assert.deepEqual(ingestionInheritedKeys(files), groupInheritedKeys(files));
assert.deepEqual(groupInheritedKeys(files), new Set(['GET /api/read', 'POST /api/write']));
});
it('agrees that a non-controller class does NOT inherit interface routes', () => {
const files = [
{
path: 'Api.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
public interface Api { @GetMapping("/x") Object x(); }
`,
},
{
path: 'PlainImpl.java',
src: `package com.example;
// No @RestController/@Controller — not a provider, must inherit nothing.
public class PlainImpl implements Api { public Object x() { return null; } }
`,
},
];
assert.deepEqual(ingestionInheritedKeys(files), groupInheritedKeys(files));
assert.deepEqual(ingestionInheritedKeys(files), new Set());
});
it('agrees when a controller mixes its own mapping with an inherited one', () => {
// `own()` carries its own @*Mapping (resolved by extractSpringRoutes, NOT the
// inheritance pass); `inh()` inherits from the interface. The inheritance
// result must contain only the inherited route on both sides.
const files = [
{
path: 'Api.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
public interface Api { @GetMapping("/inherited") Object inh(); }
`,
},
{
path: 'C.java',
src: `package com.example;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class C implements Api {
@GetMapping("/own") public Object own() { return null; }
public Object inh() { return null; }
}
`,
},
];
assert.deepEqual(ingestionInheritedKeys(files), groupInheritedKeys(files));
assert.deepEqual(groupInheritedKeys(files), new Set(['GET /api/inherited']));
});
});