fix: handle wrapped JSON object from /api/ml/models endpoint

The API returns {"models":[...]} rather than a bare array.
Added MlModelsResponse wrapper class and updated LoadModelHistory
to unwrap the list before iterating, preventing the deserialization
exception in TrainingController.
This commit is contained in:
KoalaTrapLord committed 2026-03-19 02:57:46 -04:00
1 parent c6c3bfabf8
commit 85224f082e
1 file changed
+20 -1
@@ -108,7 +108,21 @@ namespace CommanderAILab.UI
{
try
{
var models = JsonConvert.DeserializeObject<List<MlModel>>(json);
// The API returns a wrapped object {"models":[...]} rather than
// a bare array. Try the wrapped form first, then fall back to
// a bare array so either shape works without a backend change.
List<MlModel> models;
if (json.TrimStart().StartsWith("{"))
{
var wrapper = JsonConvert.DeserializeObject<MlModelsResponse>(json);
models = wrapper?.models ?? new List<MlModel>();
}
else
{
models = JsonConvert.DeserializeObject<List<MlModel>>(json)
?? new List<MlModel>();
}
foreach (var m in models)
{
var row = Instantiate(historyRowPrefab, historyParent);
@@ -298,6 +312,11 @@ namespace CommanderAILab.UI
public string last_trained;
}
[Serializable] private class MlModelsResponse
{
public List<MlModel> models;
}
[Serializable] private class MlModel
{
public string version;