Repository navigation
Closed
Labels
Description
Summary
Eight near-duplicate JSON-extraction helpers exist across the codebase. Three different GetString(JsonElement, string) are in scope at any time, with subtly different semantics (two return non-nullable, one returns nullable; some treat missing-property as empty string, others as null). Plus ~527 inline TryGetProperty walks across 38 files doing the same patterns ad-hoc.
Where
The eight helper sites:
src/OpenClaw.Shared/NodeCapabilities.cs:113—NodeCapabilityBase.GetStringArg/GetIntArg/GetBoolArg/GetArg<T>src/OpenClaw.Shared/OpenClawGatewayClient.cs:3093— gateway-client local helperssrc/OpenClaw.Shared/Models.cs:221— model parser helperssrc/OpenClaw.Shared/Models.cs:742— second model parser helper blocksrc/OpenClaw.Shared/WindowsNodeClient.cs:872— node-client local helperssrc/OpenClaw.Shared/ExecApprovals/ExecApprovalV2InputValidator.cs:129— V2 exec validation helpers- (a sibling helper next to one of the above — see code-duplication review for details)
- Plus inline patterns in capability handlers
Why it matters
Beyond the obvious maintenance cost:
- Semantic drift: the three
GetStringimplementations differ in null handling. A capability author moving code between handlers picks up an incompatible helper without noticing. Result: subtle bugs where a missing property becomesnullin one path and""in another. - Discoverability: when an author needs to read a new field, they don't know which helper to use, so they inline
TryGetPropertyagain. The 527 inline walks grow. - Error reporting: the helpers' error messages vary. Some throw
InvalidOperationException, some returnnull, some default-fallback silently.
Proposed fix
Create src/OpenClaw.Shared/Json/JsonElementExtensions.cs with a single canonical set:
public static class JsonElementExtensions
{
public static string? GetStringOrNull(this JsonElement el, string property);
public static string GetStringOrDefault(this JsonElement el, string property, string @default = "");
public static int? GetInt32OrNull(this JsonElement el, string property);
public static int GetInt32OrDefault(this JsonElement el, string property, int @default);
public static bool GetBoolOrDefault(this JsonElement el, string property, bool @default);
public static T? GetObject<T>(this JsonElement el, string property, JsonSerializerOptions? opts = null);
public static string[] GetStringArray(this JsonElement el, string property);
// ... etc
}Migration:
- Land the new helper.
- Migrate the eight helper sites one PR at a time; remove the local helpers.
- Add an analyzer (or grep CI check) that flags new
TryGetPropertycalls in capability files — they should use the extension methods instead.
This was identified independently by both the architecture review and the code-duplication review as the highest-leverage refactor.
Severity
High — silent semantic drift; 527-site cost-of-change.
Reacted by Copilot
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
None yet
Activity
shanselman commentedon May 18, 2026
Closing during the post-refactor deep-review cleanup. JSON helper duplication needs a bounded fresh audit rather than the stale generated implementation; consolidated into #372. This keeps the signal while reducing stale one-off issue noise.