From e306be796f6c3ebf84451a61a8e7a8f714befd92 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Sun, 6 Jul 2025 20:15:53 +0200 Subject: [PATCH] Pythonify responses from JetBrains plugin server (camelCase -> snake_case) --- src/serena/tools/jetbrains_plugin_client.py | 27 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/serena/tools/jetbrains_plugin_client.py b/src/serena/tools/jetbrains_plugin_client.py index db87894..81476e4 100644 --- a/src/serena/tools/jetbrains_plugin_client.py +++ b/src/serena/tools/jetbrains_plugin_client.py @@ -3,10 +3,12 @@ Client for the Serena JetBrains Plugin """ import json -from typing import Any, Optional, Self +from typing import Any, Optional, Self, TypeVar import requests +T = TypeVar("T") + class SerenaClientError(Exception): """Base exception for Serena client errors.""" @@ -49,7 +51,7 @@ class JetBrainsPluginClient: # Try to parse JSON response try: - return response.json() + return self._pythonify_response(response.json()) except json.JSONDecodeError: # If response is not JSON, return raw text return {"response": response.text} @@ -63,6 +65,25 @@ class JetBrainsPluginClient: except requests.exceptions.RequestException as e: raise SerenaClientError(f"Request failed: {e}") + @staticmethod + def _pythonify_response(response: T) -> T: + """ + Converts dictionary keys from camelCase to snake_case recursively. + + :response: the response in which to convert keys (dictionary or list) + """ + to_snake_case = lambda s: "".join(["_" + c.lower() if c.isupper() else c for c in s]) + + def convert(x): + if isinstance(x, dict): + return {to_snake_case(k): convert(v) for k, v in x.items()} + elif isinstance(x, list): + return [convert(item) for item in x] + else: + return x + + return convert(response) + def heartbeat(self) -> dict[str, Any]: return self._make_request("GET", "/heartbeat") @@ -127,5 +148,5 @@ if __name__ == "__main__": # find references if symbols: first_symbol = symbols[0] - refs_response = client.find_references(name_path=first_symbol["namePath"], relative_path=first_symbol["relativePath"]) + refs_response = client.find_references(name_path=first_symbol["name_path"], relative_path=first_symbol["relative_path"]) pprint(refs_response)