microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
20.44k stars 2.96k forks source link

.Net: OpenAPI operationId is optional per spec, function name generation needs a fallback #5598

Open zengin opened 3 months ago

zengin commented 3 months ago

Describe the bug OpenAPI Plugin function generation silently fails if there is no operationId for an operation.

To Reproduce Steps to reproduce the behavior:

  1. Create the following file Example_Nasa.cs in dotnet/samples/KernelSyntaxExamples folder:
    
    // Copyright (c) Microsoft. All rights reserved.

using System; using System.Threading.Tasks; using Microsoft.SemanticKernel; using Xunit; using Xunit.Abstractions; using Microsoft.SemanticKernel.Plugins.OpenApi;

namespace Examples; public class Example_Nasa : BaseTest { [Fact] public async Task RunAsync() { Kernel kernel = new(); await kernel.ImportPluginFromOpenApiAsync( "Nasa", new Uri("https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/nasa.gov/apod/1.0.0/openapi.yaml")); Assert.True(kernel.Plugins.Count > 0); Assert.True(kernel.Plugins.Contains("Nasa")); Assert.NotEmpty(kernel.Plugins["Nasa"].GetFunctionsMetadata()); }

public Example_Nasa(ITestOutputHelper output) : base(output)
{
}

}

2. `cd dotnet/samples/KernelSyntaxExamples`
3. `dotnet test KernelSyntaxExamples.csproj --filter Example_Nasa`
4. See the following error:

[xUnit.net 00:00:00.28] Examples.Example_Nasa.RunAsync [FAIL] Failed Examples.Example_Nasa.RunAsync [158 ms] Error Message: Assert.NotEmpty() Failure: Collection was empty Stack Trace: at Examples.Example_Nasa.RunAsync() in semantic-kernel/dotnet/samples/KernelSyntaxExamples/Example_Nasa.cs:line 22 --- End of stack trace from previous location ---



**Expected behavior**
The collection shouldn't be empty, i.e. it should contain one function corresponding to `/apod` endpoint from the referenced [OpenAPI doc](https://github.com/APIs-guru/openapi-directory/blob/main/APIs/nasa.gov/apod/1.0.0/openapi.yaml). Updating the doc with operationId fixes the issue, however, because operationId is optional per [OpenAPI spec](https://spec.openapis.org/oas/v3.0.3#operation-object), SK needs a fallback in function name generation (e.g. normalized operation.Method + operation.Path)

**Additional context**
Following is the relevant part from OpenAPI spec, required fields are marked with **REQUIRED** and operationId is not marked with that.

operationId | string | Unique string used to identify the operation. The id MUST be unique among all operations described in the API. The operationId value is case-sensitive. Tools and libraries MAY use the operationId to uniquely identify an operation, therefore, it is RECOMMENDED to follow common programming naming conventions.
-- | -- | --
github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open for 90 days with no activity.