-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: [OpenApi][DataTypeSupport][Part2] Headers serialization (#4133)
This is the second PR to add header serialization. The first PR can be found at #4122. ### Description This PR introduces the following changes: - A new `SimpleStyleParameterSerializer` is added to serialize headers in accordance with the OpenAPI specification - https://swagger.io/docs/specification/serialization/ <img width="526" alt="image" src="https://github.com/microsoft/semantic-kernel/assets/68852919/b2fc55a1-ac55-40e1-8b32-2f2decda4bc3"> - The `RestApiOperation.RestARenderHeaders` method has been updated to use the serializer. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] 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 - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
- Loading branch information
1 parent
e5de7e7
commit 87f1475
Showing
14 changed files
with
292 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
dotnet/src/Functions/Functions.OpenApi/Serialization/SimpleStyleParameterSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Text.Json.Nodes; | ||
using Microsoft.SemanticKernel.Plugins.OpenApi.Model; | ||
|
||
namespace Microsoft.SemanticKernel.Plugins.OpenApi.Serialization; | ||
|
||
/// <summary> | ||
/// Serializes REST API operation parameter of the 'Simple' style. | ||
/// </summary> | ||
internal static class SimpleStyleParameterSerializer | ||
{ | ||
/// <summary> | ||
/// Serializes a REST API operation `Simple` style parameter. | ||
/// </summary> | ||
/// <param name="parameter">The REST API operation parameter to serialize.</param> | ||
/// <param name="argument">The parameter argument.</param> | ||
/// <returns>The serialized parameter.</returns> | ||
public static string Serialize(RestApiOperationParameter parameter, string argument) | ||
{ | ||
const string ArrayType = "array"; | ||
|
||
Verify.NotNull(parameter); | ||
|
||
if (parameter.Style != RestApiOperationParameterStyle.Simple) | ||
{ | ||
throw new ArgumentException($"Unexpected Rest API operation parameter style - `{parameter.Style}`", nameof(parameter)); | ||
} | ||
|
||
// Serializing parameters of array type. | ||
if (parameter.Type == ArrayType) | ||
{ | ||
return SerializeArrayParameter(parameter, argument); | ||
} | ||
|
||
// Serializing parameters of primitive - integer, string, etc type. | ||
return argument; | ||
} | ||
|
||
/// <summary> | ||
/// Serializes an array-type parameter. | ||
/// </summary> | ||
/// <param name="parameter">The REST API operation parameter to serialize.</param> | ||
/// <param name="argument">The argument value.</param> | ||
/// <returns>The serialized parameter string.</returns> | ||
private static string SerializeArrayParameter(RestApiOperationParameter parameter, string argument) | ||
{ | ||
if (JsonNode.Parse(argument) is not JsonArray array) | ||
{ | ||
throw new KernelException($"Can't deserialize parameter name '{parameter.Name}' argument '{argument}' to JSON array."); | ||
} | ||
|
||
return ArrayParameterValueSerializer.SerializeArrayAsDelimitedValues(array, delimiter: ",", encode: false); //1,2,3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../Functions.UnitTests/OpenApi/Builders/Serialization/FormStyleParametersSerializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s.UnitTests/OpenApi/Builders/Serialization/PipeDelimitedStyleParametersSerializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...unctions.UnitTests/OpenApi/Builders/Serialization/SimpleStyleParametersSerializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using Microsoft.SemanticKernel.Plugins.OpenApi.Model; | ||
using Microsoft.SemanticKernel.Plugins.OpenApi.Serialization; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Functions.UnitTests.OpenApi.Builders.Serialization; | ||
|
||
public class SimpleStyleParametersSerializerTests | ||
{ | ||
[Fact] | ||
public void ItShouldCreateParameterWithCommaSeparatedValuePerArrayItem() | ||
{ | ||
// Arrange | ||
var parameter = new RestApiOperationParameter(name: "id", type: "array", isRequired: true, expand: false, location: RestApiOperationParameterLocation.Header, style: RestApiOperationParameterStyle.Simple, arrayItemType: "integer"); | ||
|
||
// Act | ||
var result = SimpleStyleParameterSerializer.Serialize(parameter, "[1,2,3]"); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
|
||
Assert.Equal("1,2,3", result); | ||
} | ||
|
||
[Fact] | ||
public void ItShouldCreateParameterForPrimitiveValue() | ||
{ | ||
// Arrange | ||
var parameter = new RestApiOperationParameter(name: "id", type: "integer", isRequired: true, expand: false, location: RestApiOperationParameterLocation.Header, style: RestApiOperationParameterStyle.Simple); | ||
|
||
// Act | ||
var result = SimpleStyleParameterSerializer.Serialize(parameter, "28"); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
|
||
Assert.Equal("28", result); | ||
} | ||
|
||
[Theory] | ||
[InlineData(":", ":")] | ||
[InlineData("/", "/")] | ||
[InlineData("?", "?")] | ||
[InlineData("#", "#")] | ||
public void ItShouldNotEncodeSpecialSymbolsInPrimitiveParameterValues(string specialSymbol, string expectedSymbol) | ||
{ | ||
// Arrange | ||
var parameter = new RestApiOperationParameter(name: "id", type: "string", isRequired: true, expand: false, location: RestApiOperationParameterLocation.Header, style: RestApiOperationParameterStyle.Simple); | ||
|
||
// Act | ||
var result = SimpleStyleParameterSerializer.Serialize(parameter, $"fake_query_param_value{specialSymbol}"); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
|
||
Assert.EndsWith(expectedSymbol, result, StringComparison.Ordinal); | ||
} | ||
|
||
[Theory] | ||
[InlineData(":", ":")] | ||
[InlineData("/", "/")] | ||
[InlineData("?", "?")] | ||
[InlineData("#", "#")] | ||
public void ItShouldEncodeSpecialSymbolsInCommaSeparatedParameterValues(string specialSymbol, string expectedSymbol) | ||
{ | ||
// Arrange | ||
var parameter = new RestApiOperationParameter(name: "id", type: "array", isRequired: true, expand: false, location: RestApiOperationParameterLocation.Header, style: RestApiOperationParameterStyle.Simple); | ||
|
||
// Act | ||
var result = SimpleStyleParameterSerializer.Serialize(parameter, $"[\"{specialSymbol}\"]"); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
|
||
Assert.EndsWith(expectedSymbol, result, StringComparison.Ordinal); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
....UnitTests/OpenApi/Builders/Serialization/SpaceDelimitedStyleParametersSerializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.