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
Original file line number Diff line number Diff line change
Expand Up @@ -446,21 +446,23 @@ private static JsonElement ParseJsonElement(ReadOnlySpan<byte> utf8Json)
return JsonElement.ParseValue(ref reader);
}

[UnconditionalSuppressMessage("Trimming", "IL2072:Target parameter argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method.",
Justification = "Called conditionally on structs whose default ctor never gets trimmed.")]
private static object? GetDefaultValueNormalized(ParameterInfo parameterInfo)
{
// Taken from https://github.com/dotnet/runtime/blob/eff415bfd667125c1565680615a6f19152645fbf/src/libraries/System.Text.Json/Common/ReflectionExtensions.cs#L288-L317
Type parameterType = parameterInfo.ParameterType;
object? defaultValue = parameterInfo.DefaultValue;

if (defaultValue is null)
if (defaultValue is null || (defaultValue == DBNull.Value && parameterType != typeof(DBNull)))
{
return null;
}

// DBNull.Value is sometimes used as the default value (returned by reflection) of nullable params in place of null.
if (defaultValue == DBNull.Value && parameterType != typeof(DBNull))
{
return null;
return parameterType.IsValueType
#if NET
? RuntimeHelpers.GetUninitializedObject(parameterType)
#else
? System.Runtime.Serialization.FormatterServices.GetUninitializedObject(parameterType)
#endif
: null;
}

// Default values of enums or nullable enums are represented using the underlying type and need to be cast explicitly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,17 @@ public async Task MarshalResult_TypeIsDeclaredTypeEvenWhenDerivedTypeReturned()
Assert.Equal("marshalResultInvoked", result);
}

[Fact]
public async Task AIFunctionFactory_DefaultDefaultParameter()
{
Assert.NotEqual(new StructWithDefaultCtor().Value, default(StructWithDefaultCtor).Value);

AIFunction f = AIFunctionFactory.Create((Guid g = default, StructWithDefaultCtor s = default) => g.ToString() + "," + s.Value.ToString(), serializerOptions: JsonContext.Default.Options);

object? result = await f.InvokeAsync();
Assert.Contains("00000000-0000-0000-0000-000000000000,0", result?.ToString());
}

private sealed class MyService(int value)
{
public int Value => value;
Expand Down Expand Up @@ -871,7 +882,19 @@ private class A;
private class B : A;
private sealed class C : B;

public readonly struct StructWithDefaultCtor
{
public int Value { get; }
public StructWithDefaultCtor()
{
Value = 42;
}
}

[JsonSerializable(typeof(IAsyncEnumerable<int>))]
[JsonSerializable(typeof(int[]))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(Guid))]
[JsonSerializable(typeof(StructWithDefaultCtor))]
private partial class JsonContext : JsonSerializerContext;
}
Loading