Skip to content

[deep-review] A5: Eight overlapping JSON-pull helpers across the codebase (+ 527 inline TryGetProperty walks) #358

Closed

Description

@codemonkeychris

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:

  1. src/OpenClaw.Shared/NodeCapabilities.cs:113 — NodeCapabilityBase.GetStringArg / GetIntArg / GetBoolArg / GetArg<T>
  2. src/OpenClaw.Shared/OpenClawGatewayClient.cs:3093 — gateway-client local helpers
  3. src/OpenClaw.Shared/Models.cs:221 — model parser helpers
  4. src/OpenClaw.Shared/Models.cs:742 — second model parser helper block
  5. src/OpenClaw.Shared/WindowsNodeClient.cs:872 — node-client local helpers
  6. src/OpenClaw.Shared/ExecApprovals/ExecApprovalV2InputValidator.cs:129 — V2 exec validation helpers
  7. (a sibling helper next to one of the above — see code-duplication review for details)
  8. Plus inline patterns in capability handlers

Why it matters

Beyond the obvious maintenance cost:

  • Semantic drift: the three GetString implementations 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 becomes null in 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 TryGetProperty again. The 527 inline walks grow.
  • Error reporting: the helpers' error messages vary. Some throw InvalidOperationException, some return null, 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:

  1. Land the new helper.
  2. Migrate the eight helper sites one PR at a time; remove the local helpers.
  3. Add an analyzer (or grep CI check) that flags new TryGetProperty calls 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.

Activity

  1. linked a pull request that will close this issueConsolidate JSON element extraction helpers #404on May 14, 2026
  2. shanselman commented on May 18, 2026

    @shanselman
    Collaborator

    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.

  3. locked as resolved and limited conversation to collaborators on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions