Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Handle media types with parameters #10000

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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 @@ -350,7 +350,10 @@ private async Task<RestApiOperationResponse> ReadContentAndCreateOperationRespon
mediaType = mediaTypeFallback;
}

if (!this._payloadFactoryByMediaType.TryGetValue(mediaType!, out var payloadFactory))
// Remove media type parameters, such as x-api-version, from the "text/plain; x-api-version=2.0" media type string.
mediaType = mediaType!.Split(';').First();

if (!this._payloadFactoryByMediaType.TryGetValue(mediaType, out var payloadFactory))
{
throw new KernelException($"The media type {mediaType} of the {operation.Id} operation is not supported by {nameof(RestApiOperationRunner)}.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.OpenApi;
Expand Down Expand Up @@ -517,6 +518,41 @@ public void ItCreatesPluginFromOpenApiSpecificationModel()
Assert.Same(operations[0], function.Metadata.AdditionalProperties["operation"]);
}

[Fact]
public async Task ItShouldWorkWithPayloadMediaTypesDeclaredWithParametersAsync()
{
// Arrange
using var messageHandlerStub = new HttpMessageHandlerStub();
using var httpClient = new HttpClient(messageHandlerStub, false);

this._executionParameters.EnableDynamicPayload = true;
this._executionParameters.HttpClient = httpClient;

var arguments = new KernelArguments
{
["scenario"] = "fake-scenario",
};

var kernel = new Kernel();

var openApiDocument = ResourcePluginsProvider.LoadFromResource("documentV3_0.json");

var plugin = await OpenApiKernelPluginFactory.CreateFromOpenApiAsync("fakePlugin", openApiDocument, this._executionParameters);

// Act
// The media type string for the joke operation is declared with the x-api-version parameter: "application/json; x-api-version=2.0."
var result = await kernel.InvokeAsync(plugin["Joke"], arguments);

// Assert
var messageContent = messageHandlerStub.RequestContent;
Assert.NotNull(messageContent);

var deserializedPayload = await JsonNode.ParseAsync(new MemoryStream(messageContent));
Assert.NotNull(deserializedPayload);

Assert.Equal("fake-scenario", deserializedPayload["scenario"]!.GetValue<string>());
}

/// <summary>
/// Generate theory data for ItAddSecurityMetadataToOperationAsync
/// </summary>
Expand Down
Loading