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
10 changes: 10 additions & 0 deletions .changesets/1794266812-8cefc478.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 1794266812-8cefc478
features:
- core
targets:
- csharp
type: fix
bump: patch
description: send union-typed fields in form-urlencoded and multipart request bodies
author: 2ynn
date: "2026-11-09"
1 change: 1 addition & 0 deletions templates/perms.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ var fileMode = map[string]fs.FileMode{
"templates/csharp/tests/primary/ErrorsShould.cs": 0o644,
"templates/csharp/tests/primary/EventStreamAdditionalShould.cs": 0o644,
"templates/csharp/tests/primary/FlatteningShould.cs": 0o644,
"templates/csharp/tests/primary/FormUnionFieldsAdditionalShould.cs": 0o644,
"templates/csharp/tests/primary/GlobalsAdditionalShould.cs": 0o644,
"templates/csharp/tests/primary/HeadersShould.cs": 0o644,
"templates/csharp/tests/primary/Helpers.cs": 0o644,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace {{getScopeNamespace "utils"}}
using System.Net.Http;
using System.Reflection;
using System.Text;
using Newtonsoft.Json.Linq;


internal class RequestBodySerializer
Expand Down Expand Up @@ -195,7 +196,7 @@ namespace {{getScopeNamespace "utils"}}
form.Add(key, new List<string>());
}

form[key].Add(Utilities.ValueToString(((IDictionary)request)[key]));
form[key].Add(Utilities.ValueToString(UnwrapUnion(((IDictionary)request)[key])));
}
}
else if (Utilities.IsList(request))
Expand All @@ -207,7 +208,7 @@ namespace {{getScopeNamespace "utils"}}
form.Add(requestFieldName, new List<string>());
}

form[requestFieldName].Add(Utilities.ValueToString(item));
form[requestFieldName].Add(Utilities.ValueToString(UnwrapUnion(item)));
}
}
else
Expand Down Expand Up @@ -303,26 +304,35 @@ namespace {{getScopeNamespace "utils"}}
metadata.Name ?? prop.Name
);
}
else if (Utilities.IsList(value))
else
{
var values = new List<string>();

foreach (var item in (IList)value)
var formValue = UnwrapUnion(value);
if (formValue == null)
{
values.Add(Utilities.ValueToString(item));
continue;
}

foreach (var val in values)
if (Utilities.IsList(formValue))
{
formData.Add(new StringContent(val), metadata.Name ?? prop.Name);
var values = new List<string>();

foreach (var item in (IList)formValue)
{
values.Add(Utilities.ValueToString(UnwrapUnion(item)));
}

foreach (var val in values)
{
formData.Add(new StringContent(val), metadata.Name ?? prop.Name);
}
}
else
{
formData.Add(
new StringContent(Utilities.ValueToString(formValue)),
metadata.Name ?? prop.Name
);
}
}
else
{
formData.Add(
new StringContent(Utilities.ValueToString(value)),
metadata.Name ?? prop.Name
);
}
}

Expand All @@ -348,7 +358,15 @@ namespace {{getScopeNamespace "utils"}}
ref Dictionary<string, List<string>> form
)
{
if (Utilities.IsClass(value))
if (Utilities.IsUnionWrapper(value.GetType()))
{
var variant = UnwrapUnion(value);
if (variant != null)
{
SerializeFormValue(fieldName, explode, variant, ref form);
}
}
else if (Utilities.IsClass(value))
{
if (Utilities.IsDate(value))
{
Expand All @@ -367,8 +385,8 @@ namespace {{getScopeNamespace "utils"}}

foreach (var prop in props)
{
var val = {{ templateGetPropValue "value" }};
if (val == null)
var propValue = {{ templateGetPropValue "value" }};
if (propValue == null)
{
continue;
}
Expand All @@ -379,6 +397,32 @@ namespace {{getScopeNamespace "utils"}}
continue;
}

if (metadata.Json)
{
var json = Utilities.SerializeJSON(propValue);
if (explode)
{
if (!form.ContainsKey(metadata.Name))
{
form[metadata.Name] = new List<string>();
}

form[metadata.Name].Add(json);
}
else
{
items.Add($"{metadata.Name},{json}");
}

continue;
}

var val = UnwrapUnion(propValue);
if (val == null)
{
continue;
}

if (explode)
{
if (!form.ContainsKey(metadata.Name))
Expand All @@ -390,7 +434,7 @@ namespace {{getScopeNamespace "utils"}}
{
foreach(var item in (IEnumerable)val)
{
form[metadata.Name].Add(Utilities.ValueToString(item));
form[metadata.Name].Add(Utilities.ValueToString(UnwrapUnion(item)));
}
}
else
Expand All @@ -404,7 +448,7 @@ namespace {{getScopeNamespace "utils"}}
{
foreach(var item in (IEnumerable)val)
{
items.Add($"{metadata.Name},{Utilities.ValueToString(item)}");
items.Add($"{metadata.Name},{Utilities.ValueToString(UnwrapUnion(item))}");
}
}
else
Expand Down Expand Up @@ -446,12 +490,12 @@ namespace {{getScopeNamespace "utils"}}
}

form[key].Add(
Utilities.ValueToString(((IDictionary)value)[key])
Utilities.ValueToString(UnwrapUnion(((IDictionary)value)[key]))
);
}
else
{
items.Add($"{key},{Utilities.ValueToString(((IDictionary)value)[key])}");
items.Add($"{key},{Utilities.ValueToString(UnwrapUnion(((IDictionary)value)[key]))}");
}
}

Expand All @@ -474,11 +518,11 @@ namespace {{getScopeNamespace "utils"}}
{
if (explode)
{
values.Add(Utilities.ValueToString(item));
values.Add(Utilities.ValueToString(UnwrapUnion(item)));
}
else
{
items.Add(Utilities.ValueToString(item));
items.Add(Utilities.ValueToString(UnwrapUnion(item)));
}
}

Expand Down Expand Up @@ -508,6 +552,48 @@ namespace {{getScopeNamespace "utils"}}
}
}

private static object? UnwrapUnion(object? value)
{
while (value != null && Utilities.IsUnionWrapper(value.GetType()))
{
value = GetUnionVariantValue(value);
}

return value;
}

private static object? GetUnionVariantValue(object union)
{
object? unknownValue = null;

foreach (var prop in union.GetType().GetProperties())
{
var metadata = prop.GetCustomAttribute<SpeakeasyMetadata>()?.GetFormMetadata();
if (metadata == null)
{
if (prop.PropertyType == typeof(JToken) && prop.GetValue(union) is JValue raw)
{
unknownValue = raw.Value;
}

continue;
}

if (metadata.Name != "")
{
continue;
}

var val = {{ templateGetPropValue "union" }};
if (val != null)
{
return val;
}
}

return unknownValue;
}

private static (string fileName, byte[] content) ExtractFileProperties(PropertyInfo[] fileProps, object fileObject)
{
byte[]? content = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ namespace {{getScopeNamespace "utils"}}
private static bool PreCheck(JToken json, Type type)
{
if (json.Type != JTokenType.Object) return true;
if (IsUnionWrapper(type)) return true;
if (Utilities.IsUnionWrapper(type)) return true;
var jo = (JObject)json;

foreach (var (prop, attr) in EnumerateJsonProperties(type))
Expand Down Expand Up @@ -202,7 +202,7 @@ namespace {{getScopeNamespace "utils"}}
return;
}

if (IsUnionWrapper(type) && value != null)
if (Utilities.IsUnionWrapper(type) && value != null)
{
var (variantType, variantValue) = FindActiveVariant(type, value);
if (variantValue != null)
Expand Down Expand Up @@ -392,23 +392,12 @@ namespace {{getScopeNamespace "utils"}}
return t;
}

// A union wrapper is a class generated by speakeasy that carries the
// [JsonConverter(typeof(Self.SelfConverter))] attribute. Its variant
// properties carry no [JsonProperty] (only [SpeakeasyMetadata]).
private static bool IsUnionWrapper(Type type)
{
var attr = type.GetCustomAttribute<JsonConverterAttribute>();
if (attr == null) return false;
return attr.ConverterType != null
&& attr.ConverterType.DeclaringType == type;
}

{{- if hasAnyOpenUnion}}

// True when the value is an open union wrapper that fell back to its Unknown sentinel
private static bool IsUnknownUnionValue(Type type, object? value)
{
if (value == null || !IsUnionWrapper(type)) return false;
if (value == null || !Utilities.IsUnionWrapper(type)) return false;
return value is IOpenUnion openUnion && openUnion.IsUnknown();
}
{{- end}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ namespace {{getScopeNamespace "utils"}}

public static bool IsOpenEnum(object? o) => o is IOpenEnum;

// A union wrapper is a class generated by speakeasy that carries the
// [JsonConverter(typeof(Self.SelfConverter))] attribute. Its variant
// properties carry no [JsonProperty] (only [SpeakeasyMetadata]).
public static bool IsUnionWrapper(Type type)
{
var attr = Attribute.GetCustomAttribute(type, typeof(JsonConverterAttribute)) as JsonConverterAttribute;
if (attr == null) return false;
return attr.ConverterType != null
&& attr.ConverterType.DeclaringType == type;
}

public static bool IsClass(object? o)
{
if (o == null)
Expand Down
Loading
Loading