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"; } + } }