Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(workspace): read WebClient, RestClient, Apache HttpClient and MicroProfile REST clients as calls (#4540)

Raghav Chamadiyaa7f602c2114e
diff --git a/docs/scale/WORKSPACES.md b/docs/scale/WORKSPACES.md
index 02962acb7..2e2a93945 100644
--- a/docs/scale/WORKSPACES.md
+++ b/docs/scale/WORKSPACES.md
@@ -141,7 +141,7 @@ Scans source files for HTTP routes, gRPC services, database tables, message topi
| JS / TS | Express, Hono, Fastify, Koa, Elysia; NestJS controllers; Next.js App Router; Remix | `fetch`; axios, ky, got, ofetch and their instances; Angular `HttpClient`; HTTP-named wrappers |
| Python | FastAPI, Flask, Django | requests, httpx (aiohttp through wrappers the index confirms) |
| PHP | Laravel | Guzzle, Laravel `Http` |
-| Java / Kotlin | Spring, JAX-RS, Micronaut | Feign (`@RequestLine`, and `@FeignClient` with Spring MVC annotations, its `name` read as the host), Spring `@HttpExchange` interfaces, `java.net.http`, `RestTemplate`, Ktor (Kotlin) |
+| Java / Kotlin | Spring, JAX-RS, Micronaut | Feign (`@RequestLine`, and `@FeignClient` with Spring MVC annotations, its `name` read as the host), Spring `@HttpExchange` interfaces, Spring `WebClient` and `RestClient`, `RestTemplate` (also through a getter or a `RequestEntity`), `java.net.http`, Apache HttpClient (`new HttpPost(url)`, with a base URL passed to a helper's constructor in another file), MicroProfile `@RegisterRestClient` interfaces, Ktor (Kotlin) |
| Go | gin, echo, chi, net/http | net/http |
| C# | ASP.NET (attribute and minimal API) | HttpClient (`GetAsync`, `GetFromJsonAsync`, `PostAsJsonAsync`, `GetStringAsync`, `new HttpRequestMessage(HttpMethod.X, url)`; a URL held in a local or a `const` / `readonly` field), UnityWebRequest, Best.HTTP |
| Ruby | | HTTParty, RestClient, Faraday, Net::HTTP |
diff --git a/packages/core/src/repowise/core/ingestion/framework_routes.py b/packages/core/src/repowise/core/ingestion/framework_routes.py
index 7c2477916..ce347330e 100644
--- a/packages/core/src/repowise/core/ingestion/framework_routes.py
+++ b/packages/core/src/repowise/core/ingestion/framework_routes.py
@@ -932,6 +932,12 @@ _JAXRS_PATH_RE = re.compile(r"""@Path\s*\(\s*(?:value\s*=\s*)?["'](?P<path>[^"']
_JAXRS_VERB_RE = re.compile(rf"@(?P<verb>{_JAXRS_VERBS})\b")
+# Any `@Path(...)`, literal or not.
+_JAXRS_PATH_OPEN_RE = re.compile(r"@Path\s*\(")
+
+# A MicroProfile REST client: a JAX-RS interface whose routes are calls out.
+_MICROPROFILE_CLIENT_RE = re.compile(r"@RegisterRestClient\b")
+
_JAXRS_TYPE_RE = re.compile(
r"^[^\S\n]*(?:public|final|abstract|open|internal|sealed|data|\s)*"
r"(?:class|interface)\s+\w+",
@@ -970,15 +976,20 @@ def jaxrs_class_paths(content: str) -> list[tuple[int, str]]:
def jaxrs_routes(content: str) -> Iterator[RouteMatch]:
"""Verb-annotated resource methods in *content*.
- ``path`` is the method's own ``@Path`` or ``""``; the class prefix is
- stitched on by the consumer, the only side that knows how to compose one.
+ ``path`` is the method's own ``@Path``, ``""`` when it has none and
+ ``None`` when it names one this cannot read (a constant); the class prefix
+ is stitched on by the consumer, the only side that knows how to compose one.
"""
for m in _JAXRS_VERB_RE.finditer(content):
end = _jaxrs_decl_end(content, m.start())
sub = _JAXRS_PATH_RE.search(content, m.end(), end)
+ if sub is None and _JAXRS_PATH_OPEN_RE.search(content, m.end(), end):
+ path = None
+ else:
+ path = sub.group("path") if sub else ""
yield RouteMatch(
verb=m.group("verb").upper(),
- path=sub.group("path") if sub else "",
+ path=path,
receiver=None,
handler=None,
offset=m.start(),
@@ -1663,12 +1674,41 @@ def spring_feign_clients(
The arguments are the annotation's parenthesised source text, ``""`` when
it has none; the client's ``name``, ``url`` and ``path`` sit in it.
"""
- if "@FeignClient" not in content:
+ return _client_types(content, annotations, _SPRING_CLIENT_RE, "@FeignClient")
+
+
+def microprofile_rest_clients(
+ content: str, annotations: set[int] | None = None
+) -> list[tuple[int, int, str, str | None]]:
+ """``(body start, body end, annotation arguments, prefix)`` per ``@RegisterRestClient`` type.
+
+ A MicroProfile client is a JAX-RS interface: its verb annotations are calls
+ out, not routes served. ``prefix`` is the type's own ``@Path``, ``""`` when
+ it has none and ``None`` when it names one this cannot read.
+ """
+ clients = _client_types(content, annotations, _MICROPROFILE_CLIENT_RE, "@RegisterRestClient")
+ if not clients:
+ return []
+ code = micronaut_annotations(content) if annotations is None else annotations
+ # A type-level @Path is the one whose declaration ends at the type's body.
+ paths: dict[int, str | None] = {}
+ for m in _JAXRS_PATH_OPEN_RE.finditer(content):
+ if m.start() in code:
+ literal = _JAXRS_PATH_RE.match(content, m.start())
+ paths[_jaxrs_decl_end(content, m.start())] = literal.group("path") if literal else None
+ return [(start, end, args, paths.get(start, "")) for start, end, args in clients]
+
+
+def _client_types(
+ content: str, annotations: set[int] | None, pattern: re.Pattern[str], marker: str
+) -> list[tuple[int, int, str]]:
+ """``(body start, body end, annotation arguments)`` per type annotated *pattern*."""
+ if marker not in content:
return []
code = micronaut_annotations(content) if annotations is None else annotations
return [
(start, end, _spring_args(content, name_end))
- for name_end, start, end in _spring_type_bodies(content, code, _SPRING_CLIENT_RE)
+ for name_end, start, end in _spring_type_bodies(content, code, pattern)
]
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/__init__.py b/packages/core/src/repowise/core/workspace/extractors/http/__init__.py
index 9cb7f4ef6..f1cb15dd7 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/__init__.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/__init__.py
@@ -17,7 +17,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ..base import ScanContext, select_files
-from ..dialect import ContractDialect, run_dialect, runs_on, union_extensions
+from ..dialect import ContractDialect, run_dialect, union_extensions
from ..langs import PYTHON
from .apex_callouts import ApexCalloutsDialect
from .apex_rest import ApexRestDialect
@@ -29,7 +29,7 @@ from .fastapi import FastApiDialect
from .flask import FlaskDialect
from .go import GoDialect
from .go_clients import GoClientsDialect
-from .java_clients import JavaClientsDialect, SpringClientsDialect
+from .java_clients import JavaClientsDialect, MicroProfileClientsDialect, SpringClientsDialect
from .jaxrs import JaxRsDialect
from .js_clients import JsClientsDialect
from .kotlin_clients import KotlinClientsDialect
@@ -88,6 +88,7 @@ CONSUMER_DIALECTS: tuple[ContractDialect, ...] = (
PhpClientsDialect(),
ApexCalloutsDialect(),
SpringClientsDialect(),
+ MicroProfileClientsDialect(),
)
@@ -225,7 +226,9 @@ class HttpExtractor:
for rel_path, suffix, content in files:
ctx = ScanContext(repo_alias, rel_path, suffix, content)
for dialect in collectors:
- if runs_on(dialect, ctx):
+ # A collector may gate its repo-wide pass apart from extraction.
+ gate = getattr(dialect, "mount_requires", dialect.requires)
+ if ctx.suffix in dialect.extensions and ctx.has_any(gate):
found = dialect.collect_mounts(ctx)
if found:
per_file.append(found)
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/java_clients.py b/packages/core/src/repowise/core/workspace/extractors/http/java_clients.py
index c7eb5497f..b376cf8c2 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/java_clients.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/java_clients.py
@@ -1,6 +1,6 @@
-"""Java HTTP consumer dialect.
+"""Java and Kotlin HTTP consumer dialects.
-Three call shapes, each with its own source for the verb:
+Five call shapes, each with its own source for the verb:
* Feign declarative clients: ``@RequestLine("POST /v1/{entityType}/images")``.
Verb and path both sit inside one annotation string and the placeholders are
@@ -11,7 +11,16 @@ Three call shapes, each with its own source for the verb:
by walking the chain up to its ``;`` or its ``.build()``.
* Spring ``RestTemplate``: ``getForObject`` / ``postForEntity`` and friends name
the verb in the callee, ``exchange(uri, HttpMethod.GET, ...)`` names it in the
- second argument.
+ second argument, or in the ``RequestEntity.get(uri)`` it is handed. The
+ template may be reached through a getter (``proxy.getRestTemplate()``).
+* Spring ``WebClient`` and ``RestClient``: ``client.get().uri(url, vars)``, the
+ verb step (or ``.method(HttpMethod.X)``) directly before the ``uri`` step.
+* Apache HttpClient: ``new HttpPost(url)``, the class naming the verb.
+
+A helper constructed with its base URL in another file
+(``new CartHelper(CART_URL)`` beside ``this.baseUrl = baseUrl``) has that base
+folded in: constructor calls passing a URL are collected repo-wide as mounts,
+and a call whose URL starts with such a field is a dataflow row.
Spring's declarative clients, Java or Kotlin, are read by their own dialect:
an ``@FeignClient`` interface written with the controller annotations, and a
@@ -28,13 +37,16 @@ a URL validated upstream, so no site carries a path this layer could read.
from __future__ import annotations
import re
-from collections.abc import Callable, Iterator
+from collections.abc import Callable, Iterator, Mapping
+from dataclasses import replace
from typing import TYPE_CHECKING
from repowise.core.ingestion.framework_routes import (
SPRING_EXCHANGE,
RouteMatch,
+ jaxrs_routes,
micronaut_annotations,
+ microprofile_rest_clients,
spring_class_paths,
spring_feign_clients,
spring_route_prefixes,
@@ -43,7 +55,15 @@ from repowise.core.ingestion.framework_routes import (
from repowise.core.workspace.origins import ContractOrigin
from ..langs import JAVA, KOTLIN
-from ..strings import JAVA_SYNTAX, call_arguments, match_paren, string_constants
+from ..strings import (
+ JAVA_SYNTAX,
+ call_arguments,
+ match_paren,
+ resolve_string,
+ split_top_level,
+ string_constants,
+ syntax_for_suffix,
+)
from .client_calls import VERBS, ClientCallMatch, consumer_contracts, method_from_argument
from .mounts import compose_prefix
@@ -69,12 +89,42 @@ _CHAIN_LIMIT = 2000
# `delete` are not, which is why the file is gated on the class name and the
# rows go through `path_only`.
_REST_TEMPLATE_RE = re.compile(
- r"\b(?:[A-Za-z_]\w*\s*\.\s*"
+ r"(?:(?:\b[A-Za-z_]\w*|\))\s*\.\s*"
r"(getForObject|getForEntity|postForObject|postForEntity|patchForObject|exchange)"
- r"|\w*[Tt]emplate\w*\s*\.\s*(put|delete))"
+ r"|\b\w*[Tt]emplate\w*(?:\(\s*\))?\s*\.\s*(put|delete))"
r"\s*\("
)
+# RequestEntity.get(url) / RequestEntity.method(HttpMethod.X, url): a request
+# whose builder head names its verb and URL.
+_REQUEST_ENTITY_RE = re.compile(
+ r"\bRequestEntity\s*\.\s*(get|post|put|delete|patch|head|options|method)\s*\("
+)
+_ENTITY_ASSIGN_RE = re.compile(r"(?<![.\w$])([A-Za-z_]\w*)\s*=(?!=)\s*(?=RequestEntity\s*\.)")
+
+# A WebClient / RestClient request: the verb step, then the `.uri(` naming the URL.
+_SPRING_REQUEST_RE = re.compile(
+ r"\.\s*(?:(?P<verb>get|post|put|delete|patch|head|options)\s*\(\s*\)"
+ r"|method\s*\(\s*(?P<method>[\w.]+)\s*\))\s*\.\s*uri\s*\("
+)
+
+# Apache HttpClient 4 and 5: the request class names the verb.
+_APACHE_REQUEST_RE = re.compile(r"\bnew\s+Http(Get|Post|Put|Delete|Patch|Head|Options)\s*\(")
+
+# A constructor call, whose arguments may carry a helper's base URL.
+_NEW_RE = re.compile(r"\bnew\s+([A-Z]\w*)\s*\(")
+_CLASS_RE = re.compile(r"\bclass\s+([A-Z]\w*)")
+# `this.base = base;` or `base = base;` in a constructor body.
+_FIELD_FROM_PARAM_RE = re.compile(
+ r"(?<![\w$])(?:this\s*\.\s*)?([A-Za-z_]\w*)\s*=\s*([A-Za-z_]\w*)\s*;"
+)
+# Mount key of the URL a `new C(...)` call passes: class, arity, position.
+_CTOR_KEY = "java-ctor-url:"
+# A generic argument list or a parameter annotation, dropped before parameters split.
+_GENERIC_RE = re.compile(r"<[^<>]*>")
+_PARAM_ANNOTATION_RE = re.compile(r"@\w+(?:\s*\([^()]*\))?")
+# Words that precede a name without declaring it.
+_NOT_TYPES = frozenset({"return", "throw", "case", "else", "new", "yield", "assert"})
def feign_calls(content: str) -> Iterator[ClientCallMatch]:
@@ -153,6 +203,7 @@ def java_net_http_calls(content: str) -> Iterator[ClientCallMatch]:
def resttemplate_calls(content: str) -> Iterator[ClientCallMatch]:
if "RestTemplate" not in content:
return
+ entities: dict[str, tuple[str, str]] | None = None
for m in _REST_TEMPLATE_RE.finditer(content):
callee = m.group(1) or m.group(2)
paren = m.end() - 1
@@ -160,15 +211,22 @@ def resttemplate_calls(content: str) -> Iterator[ClientCallMatch]:
if not args:
continue
method = None
+ url = args[0]
if callee == "exchange":
if len(args) < 2:
continue
method = method_from_argument(args[1])
if method is None:
- continue # a verb held in a variable is not one we know
+ # exchange(entity, type): the RequestEntity names verb and URL.
+ if entities is None:
+ entities = _request_entities(content)
+ entity = _request_entity(url.strip()) or entities.get(url.strip())
+ if entity is None:
+ continue # a verb held in a variable is not one we know
+ method, url = entity
yield ClientCallMatch(
client="resttemplate",
- url=args[0],
+ url=url,
offset=m.start(),
callee=callee,
method=method,
@@ -176,23 +234,214 @@ def resttemplate_calls(content: str) -> Iterator[ClientCallMatch]:
)
+def _request_entity(text: str, at: int = 0) -> tuple[str, str] | None:
+ """``(verb, URL text)`` of the ``RequestEntity`` builder starting at *at*, else ``None``."""
+ m = _REQUEST_ENTITY_RE.match(text, at)
+ if m is None:
+ return None
+ args = call_arguments(text, m.end() - 1)
+ if not args:
+ return None
+ if m.group(1) == "method":
+ verb = method_from_argument(args[0]) if len(args) == 2 else None
+ return (verb, args[1]) if verb else None
+ return (m.group(1).upper(), args[0]) if len(args) == 1 else None
+
+
+def _request_entities(content: str) -> dict[str, tuple[str, str]]:
+ """Names the file assigns exactly once, to a ``RequestEntity`` builder."""
+ out: dict[str, tuple[str, str]] = {}
+ for m in _ENTITY_ASSIGN_RE.finditer(content):
+ name = m.group(1)
+ entity = _request_entity(content, m.end())
+ assigned = re.findall(rf"(?<![.\w$]){name}\s*=(?!=)", content)
+ if entity is not None and len(assigned) == 1:
+ out[name] = entity
+ return out
+
+
+def spring_request_calls(content: str) -> Iterator[ClientCallMatch]:
+ """``WebClient`` and ``RestClient`` requests: a verb step, then ``.uri(url, ...)``.
+
+ A ``.uri(builder -> ...)`` function names no URL this can read and is
+ refused when it is resolved.
+ """
+ for m in _SPRING_REQUEST_RE.finditer(content):
+ verb = m.group("verb")
+ method = verb.upper() if verb else method_from_argument(m.group("method"))
+ args = call_arguments(content, m.end() - 1)
+ if method is None or not args:
+ continue
+ yield ClientCallMatch(
+ client="webclient",
+ url=args[0],
+ offset=m.start(),
+ method=method,
+ origin=ContractOrigin.CLIENT_CALL,
+ )
+
+
+def apache_calls(content: str) -> Iterator[ClientCallMatch]:
+ """Apache HttpClient requests: ``new HttpPost(url)`` and its siblings."""
+ for m in _APACHE_REQUEST_RE.finditer(content):
+ args = call_arguments(content, m.end() - 1)
+ if not args or len(args) != 1:
+ continue
+ yield ClientCallMatch(
+ client="apache-httpclient",
+ url=args[0],
+ offset=m.start(),
+ method=m.group(1).upper(),
+ origin=ContractOrigin.CLIENT_CALL,
+ )
+
+
+def ctor_url_arguments(content: str) -> dict[str, str]:
+ """The absolute URLs this file's ``new C(...)`` calls pass, keyed by class and position.
+
+ Ceiling: only a URL a file resolves is recorded, so another call site
+ passing a URL held in a variable does not make the key ambiguous. Two
+ sites passing different URLs do, and the merge drops the key.
+ """
+ out: dict[str, str] = {}
+ constants: dict[str, str] | None = None
+ for m in _NEW_RE.finditer(content):
+ args = call_arguments(content, m.end() - 1) or []
+ for i, arg in enumerate(args):
+ if "://" not in arg and not arg.strip().isidentifier():
+ continue # neither a URL literal nor a name that could hold one
+ if constants is None:
+ constants = string_constants(content, JAVA_SYNTAX)
+ value = resolve_string(arg, JAVA_SYNTAX, constants)
+ if value is not None and "://" in value and '"' not in value:
+ out[_ctor_key(m.group(1), len(args), i)] = value
+ return out
+
+
+def _ctor_key(cls: str, arity: int, position: int) -> str:
+ # The arity keeps an overload's arguments apart from another's.
+ return f"{_CTOR_KEY}{cls}/{arity}:{position}"
+
+
+def _param_names(text: str) -> list[str]:
+ """The names a parameter list declares, generics and annotations dropped."""
+ text = _PARAM_ANNOTATION_RE.sub(" ", text)
+ while True:
+ stripped = _GENERIC_RE.sub(" ", text)
+ if stripped == text:
+ break
+ text = stripped
+ return [p.split()[-1].lstrip("*") for p in text.split(",") if p.split()]
+
+
+def _ctor_params(content: str, cls: str) -> Iterator[tuple[list[str], int, int, int]]:
+ """``(parameter names, parameters start, body start, body end)`` per constructor of *cls*.
+
+ A declaration opens its line or follows an access modifier, so a call
+ (``new Cls(``, ``x = Cls(``) is not read as one.
+ """
+ decl_re = rf"(?m)(?:^[ \t]*|\b(?:public|protected|private)\s+){cls}\s*\("
+ for decl in re.finditer(decl_re, content):
+ close = match_paren(content, decl.end() - 1)
+ if close < 0:
+ continue
+ brace = re.match(r"\s*(?:throws[^{;]*)?\{", content[close + 1 : close + 200])
+ if brace is None:
+ continue
+ start = close + brace.end()
+ end = match_paren(content, start, closer="}")
+ params = _param_names(content[decl.end() : close])
+ yield params, decl.end(), start, end if end > 0 else start
+
+
+def _ctor_bases(content: str, mounts: Mapping[str, str]) -> dict[str, str]:
+ """Fields a constructor here sets from a parameter a ``new`` call fills with a URL.
+
+ The value is the URL as a Java literal. A field assigned anywhere else in
+ the file (an initialiser, a setter) is left out: which value reaches the
+ call is then not settled.
+ """
+ out: dict[str, str] = {}
+ for cls in dict.fromkeys(_CLASS_RE.findall(content)):
+ for params, params_start, start, end in _ctor_params(content, cls):
+ for a in _FIELD_FROM_PARAM_RE.finditer(content, start, end):
+ field, param = a.group(1), a.group(2)
+ if param not in params:
+ continue
+ value = mounts.get(_ctor_key(cls, len(params), params.index(param)))
+ if value is not None and _settled_field(content, field, params_start, start):
+ out[field] = f'"{value}"'
+ return out
+
+
+def _settled_field(content: str, field: str, ctor_start: int, ctor_body: int) -> bool:
+ """Whether *field* is written once, and no parameter or local elsewhere shadows it.
+
+ The constructor's own parameter list is the one place the name may also
+ be declared: a ``base`` parameter of another method would be any URL.
+ """
+ if len(re.findall(rf"(?<![\w$]){field}\s*=(?!=)", content)) != 1:
+ return False
+ declared = [
+ m.start()
+ for m in re.finditer(rf"(\w+|[>\]])\s+{field}\s*[,)=;:]", content)
+ if m.group(1) not in _NOT_TYPES and not ctor_start <= m.start() < ctor_body
+ ]
+ return len(declared) == 1 # the field's own declaration
+
+
+def _with_bases(rows: list[ClientCallMatch], bases: dict[str, str]) -> list[ClientCallMatch]:
+ """*rows*, each URL led by a constructor-set base field put onto that base's literal."""
+ out = []
+ for row in rows:
+ pieces = split_top_level(row.url, "+")
+ head = pieces[0].strip()
+ if head in bases:
+ url = " + ".join([bases[head], *pieces[1:]])
+ row = replace(row, url=url, origin=ContractOrigin.DATAFLOW)
+ out.append(row)
+ return out
+
+
class JavaClientsDialect:
name = "java-clients"
- extensions = JAVA
- requires = ("@RequestLine", "HttpRequest", "RestTemplate")
+ extensions = JAVA | KOTLIN
+ requires = (
+ "@RequestLine",
+ "HttpRequest",
+ "RestTemplate",
+ "WebClient",
+ "RestClient",
+ "org.apache.http",
+ "org.apache.hc",
+ )
+ # The repo-wide pass reads constructor calls that pass a URL literal on.
+ mount_requires = ('"http://', '"https://')
+
+ def collect_mounts(self, ctx: ScanContext) -> dict[str, str]:
+ return ctor_url_arguments(ctx.content) if ctx.suffix in JAVA else {}
def extract(self, ctx: ScanContext) -> list[Contract]:
content = ctx.content
named = [*feign_calls(content), *java_net_http_calls(content)]
+ if ctx.has_any(("WebClient", "RestClient")):
+ named.extend(spring_request_calls(content))
+ if ctx.has_any(("org.apache.http", "org.apache.hc")):
+ named.extend(apache_calls(content))
# `.put`/`.delete` on any receiver could be a collection, so a URL with
# no slash in it is a key rather than a route.
ambiguous = list(resttemplate_calls(content))
if not named and not ambiguous:
return []
- constants = string_constants(content, JAVA_SYNTAX)
+ syntax = syntax_for_suffix(ctx.suffix) or JAVA_SYNTAX
+ constants = string_constants(content, syntax)
+ if ctx.suffix in JAVA and ctx.mounts:
+ bases = _ctor_bases(content, ctx.mounts)
+ if bases:
+ named, ambiguous = _with_bases(named, bases), _with_bases(ambiguous, bases)
return [
- *consumer_contracts(ctx, named, JAVA_SYNTAX, constants=constants),
- *consumer_contracts(ctx, ambiguous, JAVA_SYNTAX, constants=constants, path_only=True),
+ *consumer_contracts(ctx, named, syntax, constants=constants),
+ *consumer_contracts(ctx, ambiguous, syntax, constants=constants, path_only=True),
]
@@ -379,3 +628,46 @@ class SpringClientsDialect:
# The URL text is built here, not read from the file, so it resolves as
# a plain literal whatever the file's language.
return consumer_contracts(ctx, rows, JAVA_SYNTAX)
+
+
+def _microprofile_base(args: str) -> str:
+ """The base a ``@RegisterRestClient`` names in ``baseUri``, else ``""``.
+
+ A ``configKey`` names configuration this file cannot read, so the call is
+ a bare path.
+ """
+ return _annotation_strings(_top_level(args)).get("baseUri", "").rstrip("/")
+
+
+def microprofile_client_calls(content: str, annotations: set[int]) -> Iterator[ClientCallMatch]:
+ """Calls a MicroProfile ``@RegisterRestClient`` interface declares with JAX-RS annotations."""
+ clients = microprofile_rest_clients(content, annotations)
+ if not clients:
+ return
+ class_paths = [
+ (start, end, None if prefix is None else (prefix.rstrip("/"),))
+ for start, end, _, prefix in clients
+ ]
+
+ def base_at(offset: int) -> tuple[str, str] | None:
+ inner = [args for start, end, args, _ in clients if start < offset < end]
+ return (_microprofile_base(inner[-1]), "") if inner else None
+
+ yield from _interface_rows(
+ jaxrs_routes(content),
+ class_paths,
+ base_at,
+ client="microprofile",
+ unrestricted=None,
+ )
+
+
+class MicroProfileClientsDialect:
+ name = "microprofile-clients"
+ extensions = JAVA | KOTLIN
+ requires = ("@RegisterRestClient",)
+
+ def extract(self, ctx: ScanContext) -> list[Contract]:
+ content = ctx.content
+ rows = list(microprofile_client_calls(content, micronaut_annotations(content)))
+ return consumer_contracts(ctx, rows, JAVA_SYNTAX) if rows else []
diff --git a/packages/core/src/repowise/core/workspace/extractors/http/jaxrs.py b/packages/core/src/repowise/core/workspace/extractors/http/jaxrs.py
index 1a349c6f2..89488515b 100644
--- a/packages/core/src/repowise/core/workspace/extractors/http/jaxrs.py
+++ b/packages/core/src/repowise/core/workspace/extractors/http/jaxrs.py
@@ -6,6 +6,10 @@ the Spring dialect stitches, with the annotations split apart.
The annotations are recognised by ``ingestion.framework_routes``, shared with
the graph-edge builder that reads them to stamp a ``jax_rs_resource`` role.
+
+A MicroProfile ``@RegisterRestClient`` interface is written with the same
+annotations, but declares calls out: its routes are left to the consumer
+dialect in ``java_clients``.
"""
from __future__ import annotations
@@ -16,6 +20,7 @@ from repowise.core.ingestion.framework_routes import (
HTTP_METHODS,
jaxrs_class_paths,
jaxrs_routes,
+ microprofile_rest_clients,
)
from ..base import line_at
@@ -38,10 +43,13 @@ class JaxRsDialect:
def extract(self, ctx: ScanContext) -> list[Contract]:
content = ctx.content
class_paths = jaxrs_class_paths(content)
+ clients = [(s, e) for s, e, _, _ in microprofile_rest_clients(content)]
out: list[Contract] = []
for route in outside_comments(ctx, jaxrs_routes(content)):
if route.verb not in HTTP_METHODS:
continue
+ if any(s < route.offset < e for s, e in clients):
+ continue # a call out, not a route served
path = compose_prefix(nearest_prefix(class_paths, route.offset), route.path or "")
c = build_provider_contract(
ctx,
diff --git a/tests/unit/workspace/test_java_clients.py b/tests/unit/workspace/test_java_clients.py
index 83860b904..3e8c3e82d 100644
--- a/tests/unit/workspace/test_java_clients.py
+++ b/tests/unit/workspace/test_java_clients.py
@@ -15,11 +15,14 @@ from repowise.core.workspace.extractors.http import HttpExtractor
from repowise.core.workspace.extractors.http.fastapi import FastApiDialect
from repowise.core.workspace.extractors.http.java_clients import (
JavaClientsDialect,
+ MicroProfileClientsDialect,
feign_calls,
java_net_http_calls,
resttemplate_calls,
)
+from repowise.core.workspace.extractors.http.jaxrs import JaxRsDialect
from repowise.core.workspace.matching import match_contracts
+from repowise.core.workspace.origins import ContractOrigin
FEIGN_CLIENT = """package com.example.client;
@@ -433,8 +436,8 @@ class TestJavaClientsRefusals:
def test_an_okhttp_request_builder_emits_nothing(self) -> None:
assert contracts(OKHTTP_BUILDER) == []
- def test_an_exchange_whose_verb_lives_in_a_request_entity_emits_nothing(self) -> None:
- assert contracts(REST_TEMPLATE_REQUEST_ENTITY) == []
+ def test_an_exchange_reads_verb_and_url_from_an_inline_request_entity(self) -> None:
+ assert ids(REST_TEMPLATE_REQUEST_ENTITY) == {"http::GET::/owners/1"}
def test_an_instance_formatted_call_is_not_a_format_head(self) -> None:
assert contracts(BUILDER_INSTANCE_FORMATTED) == []
@@ -477,3 +480,319 @@ class TestJavaClientsCrossRepoLink:
assert len(links) == 1
assert links[0].provider_repo == "api"
assert links[0].consumer_repo == "client"
+
+
+# spring-petclinic api-gateway: a WebClient call to a named service.
+WEBCLIENT_LITERAL = """package org.springframework.samples.petclinic.api.application;
+
+import org.springframework.web.reactive.function.client.WebClient;
+
+public class CustomersServiceClient {
+ public Mono<OwnerDetails> getOwner(final int ownerId) {
+ return webClientBuilder.build().get()
+ .uri("http://customers-service/owners/{ownerId}", ownerId)
+ .retrieve()
+ .bodyToMono(OwnerDetails.class);
+ }
+}
+"""
+
+# spring-petclinic api-gateway: the host held in a field a setter also writes
+# through `this.`, which leaves the field's own initialiser the one assignment.
+WEBCLIENT_FIELD_BASE = """import org.springframework.web.reactive.function.client.WebClient;
+
+public class VisitsServiceClient {
+ private String hostname = "http://visits-service/";
+
+ public Mono<Visits> getVisitsForPets(final List<Integer> petIds) {
+ return webClientBuilder.build()
+ .get()
+ .uri(hostname + "pets/visits?petId={petId}", joinIds(petIds))
+ .retrieve()
+ .bodyToMono(Visits.class);
+ }
+}
+"""
+
+RESTCLIENT_SHAPES = """import org.springframework.web.client.RestClient;
+
+class Client {
+ void run(RestClient restClient) {
+ restClient.post().uri("/orders").body(order).retrieve();
+ restClient.method(HttpMethod.PATCH).uri("/orders/{id}", id).retrieve();
+ restClient.get().uri(b -> b.path("/orders").build()).retrieve();
+ restClient.get().uri(discovery() + "/owners").retrieve();
+ restClient.method(verb).uri("/orders").retrieve();
+ }
+}
+"""
+
+KOTLIN_WEBCLIENT = """import org.springframework.web.reactive.function.client.WebClient
+
+class InventoryClient(private val webClient: WebClient) {
+ private val base = "http://inventory"
+ fun stock(sku: String) = webClient.get().uri("$base/stock/$sku").retrieve()
+}
+"""
+
+# A map lookup beside a WebClient, and a chain with no `.uri` step.
+WEBCLIENT_LOOKALIKE = """import org.springframework.web.reactive.function.client.WebClient;
+
+class Client {
+ void run(Map<String, String> routes) {
+ routes.get("/orders");
+ webClient.get().retrieve();
+ }
+}
+"""
+
+APACHE_REQUESTS = """import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpGet;
+
+class Client {
+ void run(String id) {
+ HttpPost post = new HttpPost("http://cart:8080/shipping/" + id);
+ HttpGet get = new HttpGet(URI.create("http://cart:8080/cart/" + id));
+ HttpPost opaque = new HttpPost(url);
+ }
+}
+"""
+
+APACHE_WITHOUT_IMPORT = """class Client {
+ void run() {
+ new HttpPost("/shipping/1");
+ }
+}
+"""
+
+REST_TEMPLATE_GETTER = """import org.springframework.web.client.RestTemplate;
+
+class Client {
+ void run(String id) {
+ RequestEntity<Order> request = RequestEntity.post(URI.create("http://payment/paymentAuth")).body(order);
+ proxy.getRestTemplate().exchange(request, Payment.class);
+ proxy.getRestTemplate().getForObject("/carts/" + id, Cart.class);
+ RequestEntity<Void> twice = RequestEntity.post("/a").build();
+ twice = RequestEntity.post("/b").build();
+ proxy.getRestTemplate().exchange(twice, Payment.class);
+ }
+}
+"""
+
+# robot-shop shipping: the base URL reaches a helper through its constructor.
+SHIPPING_CONTROLLER = """public class Controller {
+ private String CART_URL = String.format("http://%s/shipping/", getenv("CART_ENDPOINT", "cart"));
+
+ public String addToCart(@PathVariable String id, @RequestBody String body) {
+ CartHelper helper = new CartHelper(CART_URL);
+ return helper.addToCart(id, body);
+ }
+}
+"""
+
+CART_HELPER = """import org.apache.http.client.methods.HttpPost;
+
+public class CartHelper {
+ private String baseUrl;
+
+ public CartHelper(String baseUrl) {
+ this.baseUrl = baseUrl;
+ }
+
+ public String addToCart(String id, String data) {
+ HttpPost postRequest = new HttpPost(baseUrl + id);
+ return null;
+ }
+}
+"""
+
+
+def file_rows(content: str, rel_path: str = "Client.java") -> list[tuple[str, str | None]]:
+ suffix = "." + rel_path.rsplit(".", 1)[1]
+ ctx = ScanContext("svc", rel_path, suffix, content, {}, None)
+ return [(c.contract_id, c.meta.get("host")) for c in JavaClientsDialect().extract(ctx)]
+
+
+def consumers(files: list[tuple[str, str]]) -> list:
+ found = HttpExtractor().extract(
+ Path("."), "shipping", files=[(name, ".java", text) for name, text in files]
+ )
+ return [c for c in found if c.role == "consumer"]
+
+
+class TestSpringRequestClients:
+ def test_a_webclient_call_keeps_the_service_host(self) -> None:
+ (c,) = contracts(WEBCLIENT_LITERAL)
+ assert (c.contract_id, c.meta["host"], c.meta["client"]) == (
+ "http::GET::/owners/{param}",
+ "customers-service",
+ "webclient",
+ )
+ assert c.origin == ContractOrigin.CLIENT_CALL
+
+ def test_a_field_base_folds_and_the_query_drops(self) -> None:
+ assert file_rows(WEBCLIENT_FIELD_BASE) == [("http::GET::/pets/visits", "visits-service")]
+
+ def test_restclient_verb_steps_and_refusals(self) -> None:
+ # A builder function, a URL built by a call and a variable verb emit nothing.
+ assert file_rows(RESTCLIENT_SHAPES) == [
+ ("http::POST::/orders", None),
+ ("http::PATCH::/orders/{param}", None),
+ ]
+
+ def test_kotlin_templates_resolve(self) -> None:
+ assert file_rows(KOTLIN_WEBCLIENT, "InventoryClient.kt") == [
+ ("http::GET::/stock/{param}", "inventory")
+ ]
+
+ def test_a_chain_with_no_uri_step_emits_nothing(self) -> None:
+ assert contracts(WEBCLIENT_LOOKALIKE) == []
+
+
+class TestApacheHttpClient:
+ def test_the_request_class_names_the_verb(self) -> None:
+ assert file_rows(APACHE_REQUESTS) == [
+ ("http::POST::/shipping/{param}", "cart"),
+ ("http::GET::/cart/{param}", "cart"),
+ ]
+
+ def test_a_file_without_the_apache_import_is_not_read(self) -> None:
+ assert contracts(APACHE_WITHOUT_IMPORT) == []
+
+
+class TestRestTemplateReach:
+ def test_a_getter_receiver_and_a_request_entity_variable(self) -> None:
+ # The entity assigned twice is not settled, so its exchange is refused.
+ assert file_rows(REST_TEMPLATE_GETTER) == [
+ ("http::POST::/paymentauth", "payment"),
+ ("http::GET::/carts/{param}", None),
+ ]
+
+
+class TestConstructorBase:
+ def test_a_base_passed_to_a_helper_constructor_folds_in(self) -> None:
+ (c,) = consumers([("Controller.java", SHIPPING_CONTROLLER), ("CartHelper.java", CART_HELPER)])
+ assert (c.file_path, c.contract_id, c.origin) == (
+ "CartHelper.java",
+ "http::POST::/shipping/{param}",
+ ContractOrigin.DATAFLOW,
+ )
+
+ def test_without_the_constructor_call_the_helper_emits_nothing(self) -> None:
+ assert consumers([("CartHelper.java", CART_HELPER)]) == []
+
+ def test_two_call_sites_passing_different_urls_settle_nothing(self) -> None:
+ other = SHIPPING_CONTROLLER.replace("Controller", "Other").replace(
+ "http://%s/shipping/", "http://%s/other/"
+ )
+ files = [
+ ("Controller.java", SHIPPING_CONTROLLER),
+ ("Other.java", other),
+ ("CartHelper.java", CART_HELPER),
+ ]
+ assert consumers(files) == []
+
+ def test_a_field_written_outside_the_constructor_is_not_settled(self) -> None:
+ helper = CART_HELPER.replace(
+ " public String addToCart", " void reset() { baseUrl = other; }\n\n public String addToCart"
+ )
+ assert consumers([("Controller.java", SHIPPING_CONTROLLER), ("CartHelper.java", helper)]) == []
+
+ def test_a_generic_parameter_does_not_shift_the_positions(self) -> None:
+ helper = """import org.apache.http.client.methods.HttpPost;
+
+public class Helper {
+ private String base;
+
+ public Helper(Map<String, String> headers, String token, String base) {
+ this.base = base;
+ }
+
+ void send(String id) { new HttpPost(base + id); }
+}
+"""
+ caller = """class Caller {
+ void run() { new Helper(headers, "tok", "http://cart/shipping/"); }
+}
+"""
+ (c,) = consumers([("Caller.java", caller), ("Helper.java", helper)])
+ assert c.contract_id == "http::POST::/shipping/{param}"
+
+ def test_a_parameter_shadowing_the_field_is_not_settled(self) -> None:
+ helper = CART_HELPER.replace(
+ " public String addToCart",
+ " void other(String baseUrl) { new HttpPost(baseUrl + \"x\"); }\n\n public String addToCart",
+ )
+ found = consumers([("Controller.java", SHIPPING_CONTROLLER), ("CartHelper.java", helper)])
+ assert [c for c in found if c.origin == ContractOrigin.DATAFLOW] == []
+ assert "http::POST::/shipping/{param}" not in {c.contract_id for c in found}
+
+ def test_another_overload_s_arguments_are_not_read(self) -> None:
+ caller = SHIPPING_CONTROLLER.replace("new CartHelper(CART_URL)", "new CartHelper(CART_URL, 5)")
+ assert consumers([("Controller.java", caller), ("CartHelper.java", CART_HELPER)]) == []
+
+
+# A Quarkus caller: the interface declares calls to the stock service.
+MICROPROFILE_CLIENT = """package com.example;
+
+@Path("/api/stock")
+@RegisterRestClient(configKey = "stock-api")
+public interface StockService {
+ @GET
+ @Path("/{id}")
+ Stock get(@PathParam("id") String id);
+
+ @POST
+ void reserve(Reservation r);
+
+ @GET
+ @Path(Paths.HISTORY)
+ List<Stock> history();
+}
+"""
+
+MICROPROFILE_BASE_URI = """@RegisterRestClient(baseUri = "http://stock-service:8080/")
+@Path("/api/stock")
+interface StockService {
+ @DELETE
+ @Path("/{id}")
+ void drop(@PathParam("id") String id);
+}
+"""
+
+JAXRS_RESOURCE = """@Path("/api/stock")
+public class StockResource {
+ @GET
+ @Path("/{id}")
+ public Stock get(@PathParam("id") String id) { return null; }
+}
+"""
+
+
+def microprofile(content: str) -> list[tuple[str, str | None]]:
+ ctx = ScanContext("svc", "StockService.java", ".java", content, {}, None)
+ return [(c.contract_id, c.meta.get("host")) for c in MicroProfileClientsDialect().extract(ctx)]
+
+
+def served(content: str) -> list[str]:
+ ctx = ScanContext("svc", "S.java", ".java", content, {}, None)
+ return [c.contract_id for c in JaxRsDialect().extract(ctx)]
+
+
+class TestMicroProfileClients:
+ def test_a_rest_client_interface_is_a_consumer(self) -> None:
+ # The constant @Path is not read, so that method is refused.
+ assert microprofile(MICROPROFILE_CLIENT) == [
+ ("http::GET::/api/stock/{param}", None),
+ ("http::POST::/api/stock", None),
+ ]
+
+ def test_a_literal_base_uri_is_the_host(self) -> None:
+ assert microprofile(MICROPROFILE_BASE_URI) == [
+ ("http::DELETE::/api/stock/{param}", "stock-service")
+ ]
+
+ def test_the_client_is_no_provider_and_a_resource_still_is(self) -> None:
+ assert served(MICROPROFILE_CLIENT) == []
+ assert served(JAXRS_RESOURCE) == ["http::GET::/api/stock/{param}"]
+ assert microprofile(JAXRS_RESOURCE) == []