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).
This commit is contained in:
@@ -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);
|
||||
<div class="col-lg-6 col-12" @key="s.Id">
|
||||
<div class="card h-100">
|
||||
<div class="card-body">
|
||||
@@ -87,14 +88,9 @@
|
||||
|
||||
<div>
|
||||
<span class="badge bg-light text-dark me-1">@paramCount params</span>
|
||||
@if (!string.IsNullOrWhiteSpace(s.ReturnDefinition))
|
||||
{
|
||||
<span class="badge bg-light text-dark">returns</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="badge bg-light text-dark">void</span>
|
||||
}
|
||||
<span class="badge bg-light text-dark">
|
||||
@(returnLabel == "void" ? "void" : $"returns {returnLabel}")
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -174,19 +170,54 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// (<c>{"type":"object","properties":{...}}</c>) or the legacy flat array.
|
||||
/// Returns 0 for null/empty/malformed input.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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".
|
||||
/// </summary>
|
||||
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"; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user