Skip to content

.Net: Automatic conversion of enums is not working as expected #5045

Closed

Description

Describe the bug
Automatic conversion of enums is not working as expected.

To Reproduce

  1. Function has a default value provided for the pipelineType argument
  2. Trying to invoke the function without pipelineType defined, but this doesn't work for the current GetConverter implementation. When I called the plugin again and provided a value for pipelineType, the EnumConverter was loaded and invoked just fine. Part of me feels like this code should be able to check if the argument is defined with a default value, and if it is trust the compiler and just return the converter instead of returning null and throwing the "Unable to find type converter" function.

Expected behavior
Update: ok, I discovered that by excluding the pipelineType argument from the KernelArguments entirely since the value being added was an empty string, that this worked. So I wonder if the logic should follow "if the input is empty, but the parameter has a default value, and there can be a converter for the parameter type, trust the compiler runtime"

Update #2: I just did a check and removed the default value from the function definition, when I tried to invoke it without a value, the Kernel successfully got angry with me "InvokePlugin Failed with an error. Missing argument for function parameter 'pipelineType'"

Activity

  1. SergeyMenshykh commented on Feb 23, 2024

    @SergeyMenshykh
    Contributor

    The issue is caused by a try to set null to not nullable parameter with default value:

    [Fact]
    public async Task ItCanWorkWithEnumsAsync()
    {
        DayOfWeek? actualDay = null;
    
        // Arrange
        void DoSmth(DayOfWeek day = DayOfWeek.Wednesday)
        {
            actualDay = day;
        }
    
        var function = KernelFunctionFactory.CreateFromMethod(DoSmth);
    
        // Scenario #1 - no argument supplied - success
        var result = await function.InvokeAsync(this._kernel, new KernelArguments());
        Assert.Equal(DayOfWeek.Wednesday, actualDay);
    
        // Scenario #2 - null argument supplied - failure
        await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => function.InvokeAsync(this._kernel, new KernelArguments() { ["day"] = null }));
    }

    SK follows the C# function-calling semantic, which does not allow calling functions with null arguments for non-nullable parameters.

    There are two ways to solve this issue, both of which are aligned with C# function-calling semantics:

    1. Either avoid passing null arguments for non-nullable parameters.
    2. Or, make the parameter nullable.

    Therefore, closing the issue for now.

  2. moved this from Sprint: Planned to Sprint: Done in Semantic Kernelon Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETIssue or Pull requests regarding .NET codetriage

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions