From d9caa3dd7e2605b9aa8182e6b7a0361c6bed3e97 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Wed, 13 May 2026 05:52:53 -0400 Subject: [PATCH] fix(ui/shared-scripts): show real param count and return type on cards The card badges were stuck on the pre-migration data shape: the param counter only handled flat arrays (now JSON Schema objects), and the return badge said "returns" regardless of the actual type. Count `properties` for object schemas with array fallback, and label the return badge with the schema's `type` (or `T[]` for arrays). --- .../Pages/Design/SharedScripts.razor | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/ScadaLink.CentralUI/Components/Pages/Design/SharedScripts.razor b/src/ScadaLink.CentralUI/Components/Pages/Design/SharedScripts.razor index 18630b7..be65534 100644 --- a/src/ScadaLink.CentralUI/Components/Pages/Design/SharedScripts.razor +++ b/src/ScadaLink.CentralUI/Components/Pages/Design/SharedScripts.razor @@ -55,7 +55,8 @@ var preview = s.Code.Length > 80 ? s.Code[..80] + "…" : s.Code; - var paramCount = CountJsonArrayEntries(s.ParameterDefinitions); + var paramCount = CountSchemaProperties(s.ParameterDefinitions); + var returnLabel = DescribeReturnType(s.ReturnDefinition);
@@ -87,14 +88,9 @@
@paramCount params - @if (!string.IsNullOrWhiteSpace(s.ReturnDefinition)) - { - returns - } - else - { - void - } + + @(returnLabel == "void" ? "void" : $"returns {returnLabel}") +
@@ -174,19 +170,54 @@ } /// - /// Best-effort count of JSON array entries by tallying top-level objects. - /// Returns 0 if the parameter definition is null/empty/malformed. + /// Counts the parameters declared in either format: a JSON Schema object + /// ({"type":"object","properties":{...}}) or the legacy flat array. + /// Returns 0 for null/empty/malformed input. /// - private static int CountJsonArrayEntries(string? json) + private static int CountSchemaProperties(string? json) { if (string.IsNullOrWhiteSpace(json)) return 0; try { using var doc = System.Text.Json.JsonDocument.Parse(json); - if (doc.RootElement.ValueKind == System.Text.Json.JsonValueKind.Array) - return doc.RootElement.GetArrayLength(); + var root = doc.RootElement; + if (root.ValueKind == System.Text.Json.JsonValueKind.Object + && root.TryGetProperty("properties", out var props) + && props.ValueKind == System.Text.Json.JsonValueKind.Object) + { + return props.EnumerateObject().Count(); + } + if (root.ValueKind == System.Text.Json.JsonValueKind.Array) + return root.GetArrayLength(); } catch { /* fall through */ } return 0; } + + /// + /// Produces a short human label for a script's return type from its JSON + /// Schema definition: "string", "integer", "object", "string[]", etc. + /// Treats null/empty/malformed input as "void". + /// + private static string DescribeReturnType(string? json) + { + if (string.IsNullOrWhiteSpace(json)) return "void"; + try + { + using var doc = System.Text.Json.JsonDocument.Parse(json); + var root = doc.RootElement; + if (root.ValueKind != System.Text.Json.JsonValueKind.Object) return "void"; + if (!root.TryGetProperty("type", out var typeEl)) return "object"; + var type = typeEl.GetString() ?? "object"; + if (type == "array" + && root.TryGetProperty("items", out var items) + && items.ValueKind == System.Text.Json.JsonValueKind.Object + && items.TryGetProperty("type", out var itemTypeEl)) + { + return $"{itemTypeEl.GetString() ?? "object"}[]"; + } + return type; + } + catch { return "void"; } + } }