Skip to content

Commit

Permalink
.Net: ImportPluginFromOpenApiAsync will ignore operations with unsupp…
Browse files Browse the repository at this point in the history
…orted content types (#9736)

### Motivation and Context

Closes #8971 

### Description

Operations with unsupported content types should be ignored rather than
causing the import to fail.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄

---------

Co-authored-by: SergeyMenshykh <[email protected]>
  • Loading branch information
markwallace-microsoft and SergeyMenshykh authored Nov 19, 2024
1 parent f8d7a7e commit f4b6ac1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ internal static List<RestApiOperation> CreateRestApiOperations(OpenApiDocument d
continue;
}

var operation = new RestApiOperation(
try
{
var operation = new RestApiOperation(
id: operationItem.OperationId,
servers: operationServers,
path: path,
Expand All @@ -214,18 +216,23 @@ internal static List<RestApiOperation> CreateRestApiOperations(OpenApiDocument d
responses: CreateRestApiOperationExpectedResponses(operationItem.Responses).ToDictionary(static item => item.Item1, static item => item.Item2),
securityRequirements: CreateRestApiOperationSecurityRequirements(operationItem.Security)
)
{
Extensions = CreateRestApiOperationExtensions(operationItem.Extensions, logger)
};
{
Extensions = CreateRestApiOperationExtensions(operationItem.Extensions, logger)
};

operations.Add(operation);
operations.Add(operation);
}
catch (KernelException ke)
{
logger.LogWarning(ke, "Error occurred creating REST API operation for {OperationId}. Operation will be ignored.", operationItem.OperationId);
}
}

return operations;
}
catch (Exception ex)
{
logger.LogError(ex, "Error occurred during REST API operation creation.");
logger.LogError(ex, "Fatal error occurred during REST API operation creation.");
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<None Remove="OpenApi\TestPlugins\ai-plugin.json" />
<None Remove="OpenApi\TestPlugins\ai-plugin2.json" />
<None Remove="OpenApi\TestPlugins\documentV3_1.yaml" />
<None Remove="OpenApi\TestPlugins\multipart-form-data.json" />
<None Remove="OpenApi\TestPlugins\no-securityV3_0.json" />
<None Remove="OpenApi\TestPlugins\apikey-securityV3_0.json" />
<None Remove="OpenApi\TestPlugins\oauth-securityV3_0.json" />
Expand Down Expand Up @@ -67,4 +68,7 @@
<ProjectReference Include="..\Functions.OpenApi\Functions.OpenApi.csproj" />
<ProjectReference Include="..\Functions.Yaml\Functions.Yaml.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="OpenApi\TestPlugins\multipart-form-data.json" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,19 @@ public static TheoryData<string, IDictionary<string, string[]>> GenerateSecurity
}}
};

[Fact]
public async Task ItShouldCreateFunctionWithMultipartFormDataAsync()
{
// Arrange
var openApiDocument = ResourcePluginsProvider.LoadFromResource("multipart-form-data.json");

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

// Assert
Assert.False(plugin.TryGetFunction("createItem", out var _));
}

[Fact]
public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"openapi": "3.0.1",
"info": {
"title": "API with Multipart Form Data",
"version": "1.0.0",
"description": "API with Multipart Form Data"
},
"servers": [
{
"url": "https://api.example.com"
}
],
"paths": {
"/api/items": {
"post": {
"operationId": "createItem",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"Value": {
"type": "string"
}
}
},
"encoding": {
"Value": {
"style": "form"
}
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/GenericResult"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/GenericResult"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/GenericResult"
}
}
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
}
}
}
}
},
"components": {
"schemas": {
"GenericResult": {
"type": "object",
"required": [ "type" ],
"properties": {
"type": {
"type": "string"
}
},
"discriminator": {
"propertyName": "type"
}
}
}
}
}

0 comments on commit f4b6ac1

Please sign in to comment.