Skip to content

Commit 10993e5

Browse files
committed
Refactor array handling in DotNet code generation
Introduces a ToEnumerable extension method to unify array and enumerable conversions in generated .NET code. Updates code generation logic to use ToEnumerable for array properties, simplifying and improving type safety. Also adds necessary using statement for Extensions in generated model files.
1 parent 995ba87 commit 10993e5

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/SDK/Language/DotNet.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,8 @@ public function getFunctions(): array
512512
$subSchema = \ucfirst($property['sub_schema']);
513513

514514
if ($property['type'] === 'array') {
515-
$arraySource = $required
516-
? "((IEnumerable<object>){$mapAccess})"
517-
: "({$v} as IEnumerable<object>)?";
518-
return "{$arraySource}.Select(it => {$subSchema}.From(map: (Dictionary<string, object>)it)).ToList()";
515+
$src = $required ? $mapAccess : $v;
516+
return "{$src}.ToEnumerable().Select(it => {$subSchema}.From(map: (Dictionary<string, object>)it)).ToList()";
519517
} else {
520518
if ($required) {
521519
return "{$subSchema}.From(map: (Dictionary<string, object>){$mapAccess})";
@@ -539,9 +537,6 @@ public function getFunctions(): array
539537
if ($property['type'] === 'array') {
540538
$itemsType = $property['items']['type'] ?? 'object';
541539
$src = $required ? $mapAccess : $v;
542-
$arraySource = $required
543-
? "((IEnumerable<object>){$src})"
544-
: "({$src} as IEnumerable<object>)?";
545540

546541
$selectExpression = match ($itemsType) {
547542
'string' => 'x.ToString()',
@@ -551,7 +546,7 @@ public function getFunctions(): array
551546
default => 'x'
552547
};
553548

554-
return "{$arraySource}.Select(x => {$selectExpression}).ToList()";
549+
return "{$src}.ToEnumerable().Select(x => {$selectExpression}).ToList()";
555550
}
556551

557552
// Handle integer/number

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ namespace {{ spec.title | caseUcfirst }}.Extensions
1212
return JsonSerializer.Serialize(dict, Client.SerializerOptions);
1313
}
1414

15+
public static IEnumerable<object> ToEnumerable(this object value)
16+
{
17+
return value switch
18+
{
19+
object[] array => array,
20+
IEnumerable<object> enumerable => enumerable,
21+
IEnumerable nonGeneric => nonGeneric.Cast<object>(),
22+
_ => throw new InvalidCastException($"Cannot convert {value?.GetType().Name ?? "null"} to IEnumerable<object>")
23+
};
24+
}
25+
1526
public static string ToQueryString(this Dictionary<string, object?> parameters)
1627
{
1728
var query = new List<string>();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using System.Text.Json.Serialization;
77
{% if definition.properties | filter(p => p.enum) | length > 0 %}
88
using {{ spec.title | caseUcfirst }}.Enums;
99
{% endif %}
10+
using {{ spec.title | caseUcfirst }}.Extensions;
1011

1112
namespace {{ spec.title | caseUcfirst }}.Models
1213
{

0 commit comments

Comments
 (0)