semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Bug: CreatePluginFromApiManifestAsync fails with file-based (local) ApiDescriptionUrl

Open AleRoe opened this issue 9 months ago • 3 comments

According to the documentation, the ApiDescriptionUrl value in an apimanifest.json file supports using local filepaths. But it just does not work.

A previous post mentioned that the following line of code determines how to load the openapi-spec:

var (parsedDescriptionUrl, isOnlineDescription) = Uri.TryCreate(apiDescriptionUrl, UriKind.Absolute, out var result) ?
    (result, true) :
    (new Uri(Path.Combine(Path.GetDirectoryName(filePath) ?? string.Empty, apiDescriptionUrl)), false);

So if the apiDescriptionUrl is a local file path, this will work and DocumentLoader.LoadDocumentFromFilePathAsStream(parsedDescriptionUrl.LocalPath, logger). is correctly called.

BUT: The follwing call to the OpenApiStreamReader tries to set the BaseUrl value with new Uri(apiDescriptionUrl), which fails with a local filepath:

 var documentReadResult = await new OpenApiStreamReader(new()
{
    BaseUrl = new(apiDescriptionUrl)
}
).ReadAsync(....)

I tried just about every possible value for a local filepath in apiDescriptionUrl. Either the "isOnlineDescription" fails or the the call to set the BaseUrl for the OpenApiStreamReader.

My thinking is that the OpenApiStreamReader.BaseUrl value may not be set to the local file path when isOnlineDescription is false. Instead, we need to use the parsedDescriptionUrl.

So far, this modification has worked for me:

 var documentReadResult = await new OpenApiStreamReader(new()
{
      BaseUrl = parsedDescriptionUrl
       ///BaseUrl = isOnlineDescription ? new(apiDescriptionUrl) : null
}
).ReadAsync(....)

Thanks for your consideration! Regards Alexander

AleRoe avatar Apr 15 '25 04:04 AleRoe

Hi @AleRoe, your thinking may be right, and the parsedDescriptionUrl should be used as a base URL - BaseUrl = parsedDescriptionUrl.

CC: @baywet, @andrueastman, @musale

SergeyMenshykh avatar Apr 22 '25 19:04 SergeyMenshykh

Hi @SergeyMenshykh Thanks for getting back on this topic. I can create a PR with the fix. Okay? Cheers

AleRoe avatar Apr 22 '25 20:04 AleRoe

@AleRoe, please do so, and add a few tests to check the base URL for both remote and local cases as well.

SergeyMenshykh avatar Apr 23 '25 07:04 SergeyMenshykh

Hi @SergeyMenshykh, any feedback when my PR can be merged? I could really use this fix and would like to use the official packages. Thanks Alexander

AleRoe avatar May 01 '25 16:05 AleRoe

Expect the fix released sometime this week.

SergeyMenshykh avatar May 06 '25 16:05 SergeyMenshykh