Skip to content

Commit 6a5378f

Browse files
committed
fix: handle Object[] in .NET array deserialization
Fixed an issue where array deserialization in Model.cs.twig failed when System.Text.Json creates Object[] instead of List<T>. Added a new ToList<T> extension method that properly handles JsonElement, Object[], List<T>, and IEnumerable<T> types, ensuring robust array deserialization across all scenarios.
1 parent d1f9097 commit 6a5378f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

templates/dotnet/Package/Extensions/Extensions.cs.twig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Text.Json;
56

67
namespace {{ spec.title | caseUcfirst }}.Extensions
@@ -12,6 +13,18 @@ namespace {{ spec.title | caseUcfirst }}.Extensions
1213
return JsonSerializer.Serialize(dict, Client.SerializerOptions);
1314
}
1415

16+
public static List<T> ToList<T>(object value)
17+
{
18+
return value switch
19+
{
20+
JsonElement jsonElement => jsonElement.Deserialize<List<T>>()!,
21+
object[] objArray => objArray.Cast<T>().ToList(),
22+
List<T> list => list,
23+
IEnumerable<T> enumerable => enumerable.ToList(),
24+
_ => throw new InvalidCastException($"Cannot convert {value.GetType()} to List<{typeof(T)}>")
25+
};
26+
}
27+
1528
public static string ToQueryString(this Dictionary<string, object?> parameters)
1629
{
1730
var query = new List<string>();

templates/dotnet/Package/Models/Model.cs.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using System.Collections.Generic;
55
using System.Text.Json;
66
using System.Text.Json.Serialization;
77
using {{ spec.title | caseUcfirst }}.Enums;
8+
using {{ spec.title | caseUcfirst }}.Extensions;
89

910
namespace {{ spec.title | caseUcfirst }}.Models
1011
{
@@ -41,7 +42,7 @@ namespace {{ spec.title | caseUcfirst }}.Models
4142
{{ property.name | caseCamel | escapeKeyword | removeDollarSign }}:{{' '}}
4243
{%- if property.sub_schema %}
4344
{%- if property.type == 'array' -%}
44-
map["{{ property.name }}"] is JsonElement jsonArray{{ loop.index }} ? jsonArray{{ loop.index }}.Deserialize<List<Dictionary<string, object>>>()!.Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList() : ((IEnumerable<Dictionary<string, object>>)map["{{ property.name }}"]).Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList()
45+
Extensions.ToList<Dictionary<string, object>>(map["{{ property.name }}"]).Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList()
4546
{%- else -%}
4647
{{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: map["{{ property.name }}"] is JsonElement jsonObj{{ loop.index }} ? jsonObj{{ loop.index }}.Deserialize<Dictionary<string, object>>()! : (Dictionary<string, object>)map["{{ property.name }}"])
4748
{%- endif %}
@@ -58,7 +59,7 @@ namespace {{ spec.title | caseUcfirst }}.Models
5859
{%- endif %}
5960
{%- else %}
6061
{%- if property.type == 'array' -%}
61-
map["{{ property.name }}"] is JsonElement jsonArrayProp{{ loop.index }} ? jsonArrayProp{{ loop.index }}.Deserialize<{{ property | typeName }}>()! : ({{ property | typeName }})map["{{ property.name }}"]
62+
Extensions.ToList<{{ property | typeName | replace({'List<': '', '>': ''}) }}>(map["{{ property.name }}"])
6263
{%- else %}
6364
{%- if property.type == "integer" or property.type == "number" %}
6465
{%- if not property.required -%}map["{{ property.name }}"] == null ? null :{% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"])

0 commit comments

Comments
 (0)