Skip to content

Commit

Permalink
Add context class
Browse files Browse the repository at this point in the history
  • Loading branch information
markwallace-microsoft committed Dec 5, 2024
1 parent dc57cda commit d035ed1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ await DocumentLoader.LoadDocumentFromUriAsStreamAsync(parsedDescriptionUrl,
{
EnableDynamicPayload = true,
EnablePayloadNamespacing = true,
ParameterFilter = (RestApiParameter parameter, RestApiOperation operation, object? parent) => parameter.Name == "@odata.type" ? null : parameter,
ParameterFilter = (RestApiParameterFilterContext context) => context.Parameter.Name == "@odata.type" ? null : context.Parameter,
};

#pragma warning disable CA2000 // Dispose objects before losing scope. No need to dispose the Http client here. It can either be an internal client using NonDisposableHttpClientHandler or an external client managed by the calling code, which should handle its disposal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class OpenApiFunctionExecutionParameters
/// A custom REST API parameter filter.
/// </summary>
[Experimental("SKEXP0040")]
public RestApiParameterFilter? ParameterFilter { get; set; }
internal RestApiParameterFilter? ParameterFilter { get; set; }

/// <summary>
/// The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static IReadOnlyList<RestApiParameter> GetParameters(
RestApiParameterFilter? parameterFilter = null)
{
var parameters = new List<RestApiParameter>(parameterFilter is null ?
operation.Parameters :
operation.Parameters.Select(p => parameterFilter(p, operation)).Where(p => p != null).Cast<RestApiParameter>().ToList());
operation.Parameters :
operation.Parameters.Select(p => parameterFilter(new(operation, p))).Where(p => p != null).Cast<RestApiParameter>().ToList());

// Add payload parameters
if (operation.Payload is not null)
Expand Down Expand Up @@ -197,7 +197,7 @@ private static List<RestApiParameter> GetParametersFromPayloadMetadata(RestApiOp
{
ArgumentName = property.ArgumentName
};
parameter = parameterFilter is null ? parameter : parameterFilter(parameter, operation, parent);
parameter = parameterFilter is null ? parameter : parameterFilter(new(operation, parameter) { Parent = parent });
if (parameter is not null)
{
parameters.Add(parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace Microsoft.SemanticKernel.Plugins.OpenApi;
/// to be removed from the REST API or return a new instance of <see cref="RestApiParameter"/>
/// which will replace the original parameter.
/// </remarks>
/// <param name="parameter">The REST API parameter to filter.</param>
/// <param name="operation">The REST API operation</param>
/// <param name="parent">The parent object of the parameter, can be either an instance of <see cref="RestApiPayload"/> or <see cref="RestApiPayloadProperty"/> or null if the parameter belongs to the operation.</param>
/// <param name="context">Instance of <see cref="RestApiParameterFilterContext"/> containing details of the parametr to filter.</param>

Check warning on line 15 in dotnet/src/Functions/Functions.OpenApi/RestApiParameterFilter.cs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"parametr" should be "parameter".
[Experimental("SKEXP0040")]
public delegate RestApiParameter? RestApiParameterFilter(RestApiParameter parameter, RestApiOperation operation, object? parent = null);
internal delegate RestApiParameter? RestApiParameterFilter(RestApiParameterFilterContext context);
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.SemanticKernel.Plugins.OpenApi;

/// <summary>
/// Initializes a new instance of the <see cref="RestApiParameterFilterContext"/> class.
/// </summary>
[Experimental("SKEXP0040")]
internal sealed class RestApiParameterFilterContext
{
/// <summary>
/// The instance of <see cref="RestApiOperation"/> this parameter belongs to.
/// </summary>
public RestApiOperation Operation { get; set; }

/// <summary>
/// The instance of <see cref="RestApiParameter"/> to filter.
/// </summary>
public RestApiParameter Parameter { get; set; }

/// <summary>
/// The parent object of the parameter, can be either an instance
/// of <see cref="RestApiPayload"/> or <see cref="RestApiPayloadProperty"/>
/// null if the parameter belongs to the operation.
/// </summary>
public object? Parent { get; set; }

/// <summary>
/// Creates a new instance of the <see cref="RestApiParameterFilterContext"/> class.
/// </summary>
/// <param name="operation">The REST API operation</param>
/// <param name="parameter">The REST API parameter to filter.</param>
internal RestApiParameterFilterContext(RestApiOperation operation, RestApiParameter parameter)
{
this.Operation = operation;
this.Parameter = parameter;
}
}

0 comments on commit d035ed1

Please sign in to comment.