Skip to content
Open
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 @@ -697,8 +697,10 @@ private static (Func<KernelFunction, Kernel, KernelArguments, CancellationToken,

object? parameterFunc(KernelFunction _, Kernel kernel, KernelArguments arguments, CancellationToken __)
{
// 1. Use the value of the variable if it exists.
if (arguments.TryGetValue(name, out object? value))
// 1. Use the value of the variable if it exists. A null value can't be assigned to a non-nullable value type,
// so skip it if the parameter has a default value, e.g. when the AI model sends null for an optional parameter.
if (arguments.TryGetValue(name, out object? value) &&
(value is not null || !parameter.HasDefaultValue || !type.IsValueType || Nullable.GetUnderlyingType(type) is not null))
{
return Process(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,20 @@ public async Task ItShouldMarshalArgumentsOfValueTypeAsync()
Assert.Null(actual);
}

[Fact]
public async Task ItShouldUseDefaultValueWhenNullIsPassedForValueTypeParameterWithDefaultValueAsync()
{
// Arrange
object? actual = null;
var sut = KernelFunctionFactory.CreateFromMethod((int val = 5) => { actual = val; });

// Act
await sut.InvokeAsync(this._kernel, new() { ["val"] = null });

// Assert
Assert.Equal(5, actual);
}

[Fact]
public async Task ItShouldMarshalArgumentsOfReferenceTypeAsync()
{
Expand Down
Loading