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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: During OpenAPI import use payload parameter if specified #5874

Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -2,7 +2,6 @@

using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;

namespace Microsoft.SemanticKernel.Plugins.OpenApi;
Expand Down Expand Up @@ -34,7 +33,7 @@ internal static class RestApiOperationExtensions
var parameters = new List<RestApiOperationParameter>(operation.Parameters);

// Add payload parameters
if (operation.Method == HttpMethod.Put || operation.Method == HttpMethod.Post)
if (operation.Payload is not null)
{
parameters.AddRange(GetPayloadParameters(operation, addPayloadParamsFromMetadata, enablePayloadNamespacing));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<EmbeddedResource Include="OpenApi\TestPlugins\nonCompliant_documentV3_0.json" />
<EmbeddedResource Include="OpenApi\TestPlugins\documentV3_1.yaml" />
<EmbeddedResource Include="OpenApi\TestPlugins\documentV3_0.json" />
<EmbeddedResource Include="OpenApi\TestPlugins\repair-service.json" />
<EmbeddedResource Include="OpenApi\TestResponses\ObjectResponseSchema.json" />
<EmbeddedResource Include="OpenApi\TestResponses\ProductResponseSchema.json" />
<EmbeddedResource Include="OpenApi\TestResponses\ValidProductContent.json" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,39 @@ public async Task ItShouldSanitizeOperationNameAsync()
Assert.True(plugin.TryGetFunction("IssuesCreatemilestone", out var _));
}

[Fact]
public async Task ItCanIncludeOpenApiDeleteAndPatchOperationsAsync()
{
// Arrange
var openApiDocument = ResourcePluginsProvider.LoadFromResource("repair-service.json");

// Act
var plugin = await this._kernel.ImportPluginFromOpenApiAsync("repairServicePlugin", openApiDocument, this._executionParameters);

// Assert
Assert.NotNull(plugin);
var functionsMetadata = plugin.GetFunctionsMetadata();
Assert.Equal(4, functionsMetadata.Count);
AssertPayloadParameters(plugin, "updateRepair");
AssertPayloadParameters(plugin, "deleteRepair");
}

public void Dispose()
{
this._openApiDocument.Dispose();
}

#region private ================================================================================

private static void AssertPayloadParameters(KernelPlugin plugin, string functionName)
{
Assert.True(plugin.TryGetFunction(functionName, out var function));
Assert.NotNull(function.Metadata.Parameters);
Assert.Equal(2, function.Metadata.Parameters.Count);
Assert.Equal("payload", function.Metadata.Parameters[0].Name);
Assert.Equal("content_type", function.Metadata.Parameters[1].Name);
}

private KernelArguments GetFakeFunctionArguments()
{
return new KernelArguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Linq;
using System.Net.Http;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.OpenApi;
using Xunit;

Expand Down Expand Up @@ -225,18 +224,6 @@ public void ItShouldAddNamespaceToParametersDeclaredInPayloadMetadata(string met
Assert.Null(hasMagicWards.Description);
}

[Theory]
[InlineData("PUT")]
[InlineData("POST")]
public void ItShouldThrowExceptionIfPayloadMetadataDescribingParametersIsMissing(string method)
{
//Arrange
var operation = CreateTestOperation(method, null);

//Act
Assert.Throws<KernelException>(() => operation.GetParameters(addPayloadParamsFromMetadata: true, enablePayloadNamespacing: true));
}

[Theory]
[InlineData("PUT")]
[InlineData("POST")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
锘縶
"openapi": "3.0.0",
"info": {
"title": "Repair Service",
"description": "A simple service to manage repairs for various items",
"version": "1.0.0"
},
"servers": [
{
"url": "https://fakerepairsapi.azurewebsites.net/"
}
],
"paths": {
"/repairs": {
"get": {
"operationId": "listRepairs",
"summary": "List all repairs",
"description": "Returns a list of repairs with their details and images",
"parameters": [
{
"name": "assignedTo",
"in": "query",
"description": "Filter repairs by who they're assigned to",
"schema": {
"type": "string"
},
"required": false
}
],
"responses": {
"200": {
"description": "A successful response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": "The unique identifier of the repair"
},
"title": {
"type": "string",
"description": "The short summary of the repair"
},
"description": {
"type": "string",
"description": "The detailed description of the repair"
},
"assignedTo": {
"type": "string",
"description": "The user who is responsible for the repair"
},
"date": {
"type": "string",
"format": "date-time",
"description": "The date and time when the repair is scheduled or completed"
},
"image": {
"type": "string",
"format": "uri",
"description": "The URL of the image of the item to be repaired or the repair process"
}
}
}
}
}
}
}
}
},
"post": {
"operationId": "createRepair",
"summary": "Create a new repair",
"description": "Adds a new repair to the list with the given details and image URL",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "The short summary of the repair"
},
"description": {
"type": "string",
"description": "The detailed description of the repair"
},
"assignedTo": {
"type": "string",
"description": "The user who is responsible for the repair"
},
"date": {
"type": "string",
"format": "date-time",
"description": "The optional date and time when the repair is scheduled or completed"
},
"image": {
"type": "string",
"format": "uri",
"description": "The URL of the image of the item to be repaired or the repair process"
}
},
"required": [
"title",
"description",
"assignedTo"
]
}
}
}
},
"responses": {
"201": {
"description": "A successful response indicating that the repair was created"
}
}
},
"patch": {
"operationId": "updateRepair",
"summary": "Update an existing repair",
"description": "Update an existing repair to the list with the new updated details and image URL",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "integer",
"description": "The unique identifier of the repair to update"
},
"title": {
"type": "string",
"description": "The short summary of the repair"
},
"description": {
"type": "string",
"description": "The detailed description of the repair"
},
"assignedTo": {
"type": "string",
"description": "The user who is responsible for the repair"
},
"date": {
"type": "string",
"format": "date-time",
"description": "The date and time when the repair is scheduled or completed"
},
"image": {
"type": "string",
"format": "uri",
"description": "The URL of the image of the item to be repaired or the repair process"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Repair updated"
},
"404": {
"description": "Repair not found"
}
}
},
"delete": {
"operationId": "deleteRepair",
"summary": "Delete an existing repair",
"description": "Delete an existing repair from the list using its ID",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "integer",
"description": "The unique identifier of the repair to delete"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Repair deleted"
},
"404": {
"description": "Repair not found"
}
}
}
}
}
}
Loading