Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(workspace): read .NET HttpClient Json, string and request-message calls, with URLs held in locals and readonly fields (#4538)
Raghav Chamadiya4cdfd1aacaaf
diff --git a/docs/scale/WORKSPACES.md b/docs/scale/WORKSPACES.mdindex f46df0cc5..7daac5e50 100644--- a/docs/scale/WORKSPACES.md+++ b/docs/scale/WORKSPACES.md@@ -143,7 +143,7 @@ Scans source files for HTTP routes, gRPC services, database tables, message topi| PHP | Laravel | Guzzle, Laravel `Http` || Java / Kotlin | Spring, JAX-RS, Micronaut | Feign, `java.net.http`, `RestTemplate`, Ktor (Kotlin) || Go | gin, echo, chi, net/http | net/http |-| C# | ASP.NET (attribute and minimal API) | HttpClient, UnityWebRequest, Best.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 || Rust | Axum, Actix, Rocket | reqwest || Apex | `@RestResource` classes (`/services/apexrest` + `urlMapping`) | `HttpRequest` callouts (literal endpoint and verb) |diff --git a/packages/core/src/repowise/core/workspace/extractors/http/csharp_http.py b/packages/core/src/repowise/core/workspace/extractors/http/csharp_http.pyindex 961a5ea40..1a779821b 100644--- a/packages/core/src/repowise/core/workspace/extractors/http/csharp_http.py+++ b/packages/core/src/repowise/core/workspace/extractors/http/csharp_http.py@@ -3,26 +3,42 @@Recognises the call shapes common in C# / Unity service clients:* ``HttpClient`` and wrapper methods: ``GetAsync`` / ``PostAsync`` /- ``GetRequest<T>`` / ``PostRequest<T>`` (with or without a generic type arg);+ ``GetRequest<T>`` / ``PostRequest<T>``, the ``System.Net.Http.Json`` forms+ (``GetFromJsonAsync<T>`` / ``PostAsJsonAsync``) and ``GetStringAsync``;+* ``new HttpRequestMessage(HttpMethod.Post, url)``, the request ``SendAsync``+ sends;* ``UnityWebRequest.Get/Post/Put/Delete`` with a literal or interpolated URL;* Best.HTTP: ``new HTTPRequest(new Uri("..."), HTTPMethods.Get)``.-An interpolated string (``$"{_baseUrl}/path/{id}"``) resolves through the-shared C# syntax table, so the leading base placeholder is stripped and-interior expressions collapse to ``{param}``.+An HttpClient URL may be a literal, an interpolated string, or a name: a local+its enclosing block declares (``var uri = $"..."``) or a ``const`` /+``readonly`` string field. A field folds into an interpolation too, so+``$"{remoteServiceBaseUrl}items/{id}"`` keeps the field's ``api/catalog/``+prefix; any other leading hole is the base placeholder and is stripped, and+interior holes collapse to ``{param}``. The query string is cut before the URL+is read, so a hole there the strings layer refuses costs nothing."""from __future__ import annotationsimport refrom collections.abc import Iterator+from dataclasses import replacefrom typing import TYPE_CHECKING+from repowise.core.analysis.security.masking import mask_comments_and_strings, mask_dialectfrom repowise.core.workspace.origins import ContractOriginfrom ..langs import CSHARP-from ..strings import CSHARP_SYNTAX-from .client_calls import ClientCallMatch, consumer_contracts, matches_in+from ..strings import (+ CSHARP_SYNTAX,+ LOCAL_WINDOW,+ NAME_RE,+ call_arguments,+ enclosing_local,+ string_constants,+)+from .client_calls import ClientCallMatch, consumer_contracts, matches_in, method_from_argumentif TYPE_CHECKING:from repowise.core.workspace.contracts import Contract@@ -33,12 +49,21 @@ if TYPE_CHECKING:# (`@`) prefix: capture group 1 = prefix, group 2 = the inner text._STR = r"""(\$?@?)"([^"]*)\""""-# HttpClient + wrapper calls: GetAsync / PostAsync / GetRequest<T> / PostRequest.-# Method verbs are PascalCase (C# convention); the `Async`/`Request` suffix is-# required so we don't match unrelated `Get("key")` lookups.-_WRAPPER_RE = re.compile(- rf"""\b(Get|Post|Put|Delete|Patch)(?:Async|Request)\s*(?:<[^>]+>)?\s*\(\s*{_STR}"""+# A generic type argument, two levels of nesting (`<List<Item>>`).+_GENERIC = r"<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>"++# HttpClient + wrapper calls, ending at the argument list. Method verbs are+# PascalCase (C# convention); the `Async`/`Request` suffix is required so we+# don't match unrelated `Get("key")` lookups. Group 2 holds the+# `System.Net.Http.Json` and `GetStringAsync` forms.+_VERB_CALL_RE = re.compile(+ r"\b(Get|Post|Put|Delete|Patch)(FromJson|AsJson|String|ByteArray|Stream)?(?:Async|Request)"+ rf"\s*(?:{_GENERIC})?\s*\(")+# Extension methods only HttpClient has; `GetStringAsync` is also a cache's.+_JSON_FORMS = frozenset({"FromJson", "AsJson"})++_REQUEST_MESSAGE_RE = re.compile(r"\bnew\s+HttpRequestMessage\s*\(")# UnityWebRequest.Get(...) / .Post(...) — only when the first arg is a string._UNITY_RE = re.compile(rf"""\bUnityWebRequest\.(Get|Post|Put|Delete|Head)\s*\(\s*{_STR}""")@@ -53,17 +78,156 @@ _BESTHTTP_RE = re.compile(# HttpClient calls are read off any receiver; its type is not confirmed._ORIGIN = ContractOrigin.NAME_HEURISTIC+# Every `name = rhs;` (`this.name` too) is read, so a readonly field its+# constructor reassigns is retired; `=>` is a lambda or an expression body.+_FIELD_SYNTAX = replace(+ CSHARP_SYNTAX,+ assignment=re.compile(+ r"(?<![=!<>.\w])(?:this\.)?(?P<name>[A-Za-z_]\w*)[ \t]*=(?![=>])[ \t]*(?P<rhs>[^;\n]+);"+ ),+)-def httpclient_calls(content: str) -> Iterator[ClientCallMatch]:- yield from matches_in(- content,- _WRAPPER_RE,- client="httpclient",- url_group=3,- prefix_group=2,- method_group=1,- origin=_ORIGIN,- )+# A `const` / `readonly` declaration's name, read off the raw text as a cheap+# gate before the file's fields are folded.+_FIELD_DECL_RE = re.compile(r"\b(?:const|readonly)\b[^;=(){}\n]*?\b(\w+)[ \t]*=")+_WORD_RE = re.compile(r"\w+")++# What may follow a literal whose query was cut: nothing, or a trim of the+# query's own separators (`$"...?{qs}".TrimEnd('&', '?')`).+_TRIM_TAIL_RE = re.compile(r"\.\s*TrimEnd\s*\([^()]*\)")+_LITERAL_OPEN_RE = re.compile(r"(\$@|@\$|\$|@)?\"")+++def without_query(expr: str) -> str:+ """*expr* with its literal's query string cut, else *expr* unchanged.++ Only a lone C# literal (or one trimmed by ``TrimEnd``) is cut, at the first+ ``?`` outside its interpolation holes. A hole may hold quotes of its own+ (``{string.Join("&ids=", ids)}``), which is why the body is walked here+ rather than by the strings layer, which refuses such a literal.+ """+ m = _LITERAL_OPEN_RE.match(expr)+ if m is None:+ return expr+ prefix = m.group(1) or ""+ holes, verbatim = "$" in prefix, "@" in prefix+ cut, depth, inner = -1, 0, ""+ i = m.end()+ while i < len(expr):+ ch = expr[i]+ if inner: # a string inside a hole+ if ch == "\\":+ i += 1+ elif ch == inner:+ inner = ""+ elif depth:+ if ch in "\"'":+ inner = ch+ depth += {"{": 1, "}": -1}.get(ch, 0)+ elif ch == "\\" and not verbatim:+ i += 1+ elif ch == '"':+ if verbatim and expr[i + 1 : i + 2] == '"':+ i += 2 # `""` is a quote inside a verbatim literal+ continue+ tail = expr[i + 1 :].strip()+ if cut < 0 or (tail and not _TRIM_TAIL_RE.fullmatch(tail)):+ return expr+ return expr[:cut] + '"'+ elif ch == "?" and cut < 0:+ cut = i+ elif ch == "{" and holes:+ if expr[i + 1 : i + 2] == "{":+ i += 1+ else:+ depth = 1+ i += 1+ return expr+++class _FileNames:+ """The file's code with strings blanked and its foldable fields, read on first use."""++ def __init__(self, content: str) -> None:+ self.content = content+ self._code: str | None = None+ self._fields: dict[str, str] | None = None++ @property+ def code(self) -> str:+ if self._code is None:+ self._code = mask_comments_and_strings(+ self.content, strings=True, hash_comments=False, **mask_dialect(".cs")+ )+ return self._code++ def fields(self, urls: list[str]) -> dict[str, str]:+ """``const`` and ``readonly`` string fields the file assigns once.++ A name the file declares a second time (a parameter or a local, in any+ method or class) is left out, since a use might read that one instead;+ so is a plain field or a local assigned once. Read only when one of+ *urls* names such a field.+ """+ declared = {m.group(1) for m in _FIELD_DECL_RE.finditer(self.content)}+ if not any(w in declared for u in urls for w in _WORD_RE.findall(u)):+ return {}+ if self._fields is None:+ code = self.code+ self._fields = {+ name: rhs+ # Read off the source: the mask blanks a literal's quotes too.+ for name, rhs in string_constants(self.content, _FIELD_SYNTAX).items()+ if re.search(rf"\b(?:const|readonly)\b[^;=(){{}}]*\b{re.escape(name)}\s*=", code)+ and len(re.findall(rf"[\w>\]?][ \t]+{re.escape(name)}\s*[,)=;]", code)) == 1+ }+ return self._fields++ def url(self, expr: str, offset: int) -> str:+ """*expr* with a local name replaced by what its block assigned it."""+ # The raw text must assign the name near the use before the file is masked.+ if NAME_RE.fullmatch(expr) and "." not in expr:+ assigned = re.compile(rf"\b{re.escape(expr)}[ \t]*=")+ if assigned.search(self.content, max(0, offset - LOCAL_WINDOW), offset) is None:+ return without_query(expr)+ local = enclosing_local(self.content, self.code, expr, offset)+ if local is not None:+ expr = local+ return without_query(expr)+++def httpclient_calls(content: str, names: _FileNames) -> Iterator[ClientCallMatch]:+ for m in _VERB_CALL_RE.finditer(content):+ args = call_arguments(content, m.end() - 1)+ if not args:+ continue+ json_form = m.group(2) in _JSON_FORMS+ yield ClientCallMatch(+ client="httpclient",+ url=names.url(args[0], m.start()),+ offset=m.start(),+ method=m.group(1).upper(),+ origin=ContractOrigin.CLIENT_CALL if json_form else _ORIGIN,+ )+++def request_message_calls(content: str, names: _FileNames) -> Iterator[ClientCallMatch]:+ if "HttpRequestMessage" not in content:+ return+ for m in _REQUEST_MESSAGE_RE.finditer(content):+ args = call_arguments(content, m.end() - 1)+ if not args or len(args) != 2:+ continue+ method = method_from_argument(args[0])+ if method is None:+ continue # a verb held in a variable is not one we know+ yield ClientCallMatch(+ client="httpclient",+ url=names.url(args[1], m.start()),+ offset=m.start(),+ method=method,+ origin=ContractOrigin.CLIENT_CALL,+ )def unitywebrequest_calls(content: str) -> Iterator[ClientCallMatch]:@@ -93,15 +257,22 @@ def besthttp_calls(content: str) -> Iterator[ClientCallMatch]:class CSharpHttpDialect:name = "csharp-http"extensions = CSHARP- # `GetAsync`, `GetRequest`, `UnityWebRequest`, `HTTPRequest` in any case.+ # `GetAsync`, `GetRequest`, `HttpRequestMessage`, `UnityWebRequest`,+ # `HTTPRequest` in any case.requires = ("Async", "Request", "request", "REQUEST")def extract(self, ctx: ScanContext) -> list[Contract]:content = ctx.content+ names = _FileNames(content)matches = [- *httpclient_calls(content),+ *httpclient_calls(content, names),+ *request_message_calls(content, names),*unitywebrequest_calls(content),*besthttp_calls(content),]+ if not matches:+ return []+ # A plain literal never pays for the file's fields.+ fields = names.fields([m.url for m in matches if not m.url.startswith('"')])# A slash-free string is a key or a name, not a route.- return consumer_contracts(ctx, matches, CSHARP_SYNTAX, path_only=True)+ return consumer_contracts(ctx, matches, CSHARP_SYNTAX, constants=fields, path_only=True)diff --git a/packages/core/src/repowise/core/workspace/extractors/strings.py b/packages/core/src/repowise/core/workspace/extractors/strings.pyindex a200b60ad..957327466 100644--- a/packages/core/src/repowise/core/workspace/extractors/strings.py+++ b/packages/core/src/repowise/core/workspace/extractors/strings.py@@ -589,6 +589,55 @@ _MULTI_ASSIGN_RE = re.compile()+# A typed local declaration (`var uri = `, `string url = `, `Uri? u = `). The+# word before the name is its type, unless it is a keyword (`else uri = x`).+_LOCAL_DECL = r"\b(?P<type>\w+)[>\[\]?]*[ \t]+{name}[ \t]*=(?![=>])"+_NOT_A_TYPE = frozenset({"else", "return", "await", "yield", "throw", "case", "do"})+# A modifier marks a field, whose other assignments may sit in any method.+_MEMBER_MODIFIER_RE = re.compile(r"\b(?:private|public|protected|internal|static|readonly|const)\b")+_BRACE_RE = re.compile(r"[{}]")+# How far above a use :func:`enclosing_local` looks for the declaration.+LOCAL_WINDOW = 8_000+++def enclosing_local(content: str, code: str, name: str, offset: int) -> str | None:+ """The right-hand side of the local *name* a use at *offset* reads, else ``None``.++ For a brace language whose locals are block scoped (C#): the nearest+ declaration of *name* above *offset* counts only when its block still+ encloses the use and nothing reassigns *name* in between, so the same+ ``var uri`` in each of a file's methods reads as each method's own. A+ declaration with a modifier is a field and is not read here.+ *code* is *content* with comments and string bodies blanked at the same+ offsets. Only the :data:`LOCAL_WINDOW` characters above the use are read,+ so a file of many calls stays linear; a declaration further up is refused.+ """+ n = re.escape(name)+ decls = list(re.compile(_LOCAL_DECL.format(name=n)).finditer(code, max(0, offset - LOCAL_WINDOW), offset))+ if not decls or decls[-1].group("type") in _NOT_A_TYPE:+ return None+ decl = decls[-1]+ if _MEMBER_MODIFIER_RE.search(code, code.rfind("\n", 0, decl.start()) + 1, decl.start()):+ return None+ rhs = decl.end()+ semi = code.find(";", rhs, offset)+ if semi < 0:+ return None+ depth = 0+ for brace in _BRACE_RE.finditer(code, semi, offset):+ depth += 1 if brace.group() == "{" else -1+ if depth < 0:+ return None # the declaring block closed before the use+ # Reassigned (`x = `, `x += `, `out x`, `ref x`, `(x, y) = `) or shadowed+ # by a lambda parameter (`x => `) between the declaration and the use.+ rebound = re.compile(+ rf"(?<![\w.]){n}\s*(?:[-+?]*=(?!=)|,[^;=]*\)\s*=(?![=>]))|\b(?:out|ref)\s+(?:var\s+)?{n}\b"+ )+ if rebound.search(code, semi, offset):+ return None+ return content[rhs:semi].strip()++def on_comment_line(content: str, offset: int) -> bool:"""True when the line holding *offset* starts as a line or block comment."""start = max(content.rfind("\n", 0, offset) + 1, offset - 200)@@ -790,12 +839,16 @@ RUST_SYNTAX = StringSyntax(placeholder=_BRACE_PLACEHOLDER_RE,)+# No `assignment`: a C# name is folded only where a dialect checks it is a+# field (see `http/csharp_http.py`), never by file-wide single assignment.CSHARP_SYNTAX = StringSyntax(quotes='"',prefixes="$@",template_prefixes="$",interpolation=_BRACE_INTERP_RE,refuse=_ESCAPED_BRACE_RE,+ unwrap_heads=("new Uri",),+ concat="+",)GO_SYNTAX = StringSyntax(@@ -898,6 +951,7 @@ __all__ = ["ClassFields","StringSyntax","call_arguments",+ "enclosing_local","inline_names","js_class_fields","literal_span",diff --git a/tests/unit/workspace/test_csharp_http.py b/tests/unit/workspace/test_csharp_http.pynew file mode 100644index 000000000..7220805f3--- /dev/null+++ b/tests/unit/workspace/test_csharp_http.py@@ -0,0 +1,139 @@+"""C# HttpClient consumers: the Json forms, request messages, locals and fields."""++from __future__ import annotations++import textwrap++from repowise.core.workspace.extractors.base import ScanContext+from repowise.core.workspace.extractors.http.csharp_http import CSharpHttpDialect, without_query+from repowise.core.workspace.origins import ContractOrigin+++def _consumers(source: str) -> dict[int, tuple[str, str]]:+ content = textwrap.dedent(source)+ ctx = ScanContext("web", "Services/Client.cs", ".cs", content)+ return {c.line: (c.contract_id, c.origin) for c in CSharpHttpDialect().extract(ctx)}+++def test_field_prefix_and_per_method_locals() -> None:+ found = _consumers("""+ public class CatalogService(HttpClient httpClient)+ {+ private readonly string remoteServiceBaseUrl = "api/catalog/";++ public Task<CatalogItem?> GetCatalogItem(int id)+ {+ var uri = $"{remoteServiceBaseUrl}items/{id}";+ return httpClient.GetFromJsonAsync<CatalogItem>(uri);+ }++ public async Task<List<CatalogItem>> GetCatalogItems(IEnumerable<int> ids)+ {+ var uri = $"{remoteServiceBaseUrl}items/by?ids={string.Join("&ids=", ids)}";+ return await httpClient.GetFromJsonAsync<List<CatalogItem>>(uri);+ }++ public async Task<CatalogFacets> GetFacets()+ {+ var uri = $"{remoteServiceBaseUrl}items/facets?{filterQs}".TrimEnd('&', '?');+ return await httpClient.GetFromJsonAsync<CatalogFacets>(uri);+ }+ }+ """)+ assert found == {+ 9: ("http::GET::/api/catalog/items/{param}", ContractOrigin.CLIENT_CALL),+ 15: ("http::GET::/api/catalog/items/by", ContractOrigin.CLIENT_CALL),+ 21: ("http::GET::/api/catalog/items/facets", ContractOrigin.CLIENT_CALL),+ }+++def test_json_string_and_request_message_shapes() -> None:+ found = _consumers("""+ public class OrderingService(HttpClient client)+ {+ private const string Orders = "/api/orders/";++ public Task Create(CreateOrderRequest request)+ {+ var message = new HttpRequestMessage(HttpMethod.Post, Orders);+ return client.SendAsync(message);+ }++ public Task Add(Hook payload) => client.PostAsJsonAsync("/api/webhooks", payload);+ public Task<string> Raw() => client.GetStringAsync("/api/raw/text");+ }+ """)+ assert found == {+ 8: ("http::POST::/api/orders", ContractOrigin.CLIENT_CALL),+ 12: ("http::POST::/api/webhooks", ContractOrigin.CLIENT_CALL),+ # A cache's GetStringAsync(key) shares the name: not a confirmed client.+ 13: ("http::GET::/api/raw/text", ContractOrigin.NAME_HEURISTIC),+ }+++def test_names_that_do_not_settle_are_refused() -> None:+ found = _consumers("""+ public class Client(HttpClient http)+ {+ private string mutableBase = "/api/mutable";+ private readonly string reassigned = "/api/old";++ public Client(string configured) { this.reassigned = configured; }++ public void Declares() { var url = "/api/declared/here"; }++ public Task FromParameter(string url) => http.GetAsync(url);++ public Task Reassigned(bool flag)+ {+ var path = "/api/first";+ if (flag) path = "/api/second";+ return http.GetAsync(path);+ }++ public Task Plain() => http.GetAsync(mutableBase);+ public Task Ctor() => http.GetAsync(reassigned);+ public Task Verb(HttpMethod m) => http.SendAsync(new HttpRequestMessage(m, "/api/v"));+ public Task Replaced() => http.GetAsync($"/a/b?x={y}".Replace("a", "b"));+ }+ """)+ assert found == {}+++def test_a_parameter_or_rebinding_hides_the_declared_value() -> None:+ found = _consumers("""+ class A+ {+ const string url = "/api/field";+ Task M(string url) => c.GetAsync(url);+ Task Out() { var u = "/api/a"; Foo(out u); return c.GetAsync(u); }+ Task Ref() { string r = "/api/r"; Foo(ref r); return c.GetAsync(r); }+ Task Tuple() { var t = "/api/t"; (t, v) = Pair(); return c.GetAsync(t); }+ void Lambda() { var l = "/api/l"; xs.ForEach(l => c.GetAsync(l)); }+ }+ """)+ assert found == {}+++def test_a_local_far_above_the_use_is_not_read() -> None:+ padding = "\n".join(" Noop();" for _ in range(1000))+ found = _consumers(f"""+ class A+ {{+ Task M()+ {{+ var u = "/api/far";+{padding}+ return c.GetAsync(u);+ }}+ }}+ """)+ assert found == {}+++def test_without_query_cuts_only_a_lone_literal() -> None:+ assert without_query('$"{b}items/by?ids={string.Join("&ids=", ids)}"') == '$"{b}items/by"'+ assert without_query("\"/a?x=1\".TrimEnd('&')") == '"/a"'+ assert without_query('$"/a?x={y}".Replace("a", "b")') == '$"/a?x={y}".Replace("a", "b")'+ assert without_query('@"/a/""q""?x"') == '@"/a/""q"""'+ assert without_query("uri") == "uri"