Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions templates/dotnet/Package/Extensions/Extensions.cs.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;

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

public static List<T> ConvertToList<T>(this object value)
{
return value switch
{
JsonElement jsonElement => jsonElement.Deserialize<List<T>>() ?? throw new InvalidCastException($"Cannot deserialize {jsonElement} to List<{typeof(T)}>."),
object[] objArray => objArray.Cast<T>().ToList(),
List<T> list => list,
IEnumerable<T> enumerable => enumerable.ToList(),
_ => throw new InvalidCastException($"Cannot convert {value.GetType()} to List<{typeof(T)}>")
};
}

public static string ToQueryString(this Dictionary<string, object?> parameters)
{
var query = new List<string>();
Expand Down
5 changes: 3 additions & 2 deletions templates/dotnet/Package/Models/Model.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using {{ spec.title | caseUcfirst }}.Enums;
using {{ spec.title | caseUcfirst }}.Extensions;

namespace {{ spec.title | caseUcfirst }}.Models
{
Expand Down Expand Up @@ -41,7 +42,7 @@ namespace {{ spec.title | caseUcfirst }}.Models
{{ property.name | caseCamel | escapeKeyword | removeDollarSign }}:{{' '}}
{%- if property.sub_schema %}
{%- if property.type == 'array' -%}
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()
map["{{ property.name }}"].ConvertToList<Dictionary<string, object>>().Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList()
{%- else -%}
{{ 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 }}"])
{%- endif %}
Expand All @@ -58,7 +59,7 @@ namespace {{ spec.title | caseUcfirst }}.Models
{%- endif %}
{%- else %}
{%- if property.type == 'array' -%}
map["{{ property.name }}"] is JsonElement jsonArrayProp{{ loop.index }} ? jsonArrayProp{{ loop.index }}.Deserialize<{{ property | typeName }}>()! : ({{ property | typeName }})map["{{ property.name }}"]
map["{{ property.name }}"].ConvertToList<{{ property | typeName | replace({'List<': '', '>': ''}) }}>()
{%- else %}
{%- if property.type == "integer" or property.type == "number" %}
{%- if not property.required -%}map["{{ property.name }}"] == null ? null :{% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"])
Expand Down
Loading