Skip to content
Merged
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 @@ -8,6 +8,8 @@
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -295,7 +297,7 @@ async Task<RestApiOperationResponse> ExecuteAsync(Kernel kernel, KernelFunction
DefaultValue = p.DefaultValue ?? string.Empty,
IsRequired = p.IsRequired,
ParameterType = ConvertParameterDataType(p),
Schema = p.Schema ?? (p.Type is null ? null : KernelJsonSchema.Parse($$"""{"type":"{{p.Type}}"}""")),
Schema = GetSchema(p)
})
.ToList();

Expand Down Expand Up @@ -329,6 +331,28 @@ async Task<RestApiOperationResponse> ExecuteAsync(Kernel kernel, KernelFunction
});
}

private static KernelJsonSchema? GetSchema(RestApiParameter p)
{
// Add description to the schema.
if (p.Schema is not null && !string.IsNullOrEmpty(p.Description))
{
const string DescriptionPropertyName = "description";

// If the schema does not already have a description, add it.
if (!p.Schema.RootElement.TryGetProperty(DescriptionPropertyName, out var _))
{
var originalSchema = JsonSerializer.Serialize(p.Schema.RootElement);
if (JsonNode.Parse(originalSchema) is JsonObject obj)
{
obj.Add(DescriptionPropertyName, p.Description);
p.Schema = KernelJsonSchema.Parse(obj.ToString());
}
}
}

return p.Schema ?? (p.Type is null ? null : KernelJsonSchema.Parse($$"""{"type":"{{p.Type}}"}"""));
}

#region private

/// <summary>The metadata property bag key to use when storing the method of an operation.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,27 @@ public async Task ItShouldCreateFunctionWithMultipartFormDataAsync()
Assert.False(plugin.TryGetFunction("createItem", out var _));
}

[Fact]
public async Task ItCanAddPropertyDescriptionToSchemaAsync()
{
// Act
var plugin = await OpenApiKernelPluginFactory.CreateFromOpenApiAsync("fakePlugin", this._openApiDocument, this._executionParameters);

// Assert
var setSecretFunction = plugin["SetSecret"];
Assert.NotNull(setSecretFunction);

var functionView = setSecretFunction.Metadata;
Assert.NotNull(functionView);

// Check if description is added to the parameter schema
var secretNameParameter = functionView.Parameters.First(p => p.Name == "secret_name");
Assert.Equal("The name of the secret", secretNameParameter.Description);

Assert.True(secretNameParameter.Schema!.RootElement.TryGetProperty("description", out var description));
Assert.Equal("The name of the secret", description.GetString());
}

[Fact]
public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"in": "path",
"name": "secret-name",
"required": true,
"description": "The name of the secret",
"type": "string"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"name": "secret-name",
"in": "path",
"required": true,
"description": "The name of the secret",
"schema": {
"type": "string"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ paths:
- name: secret-name
in: path
required: true
description: The name of the secret
schema:
type: string
- name: api-version
Expand Down
Loading