Skip to content
Closed
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
4 changes: 3 additions & 1 deletion src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ private async Task<OpenApiResponse> GetResponseAsync(
continue;
}

// Execute 'schemaTransformers' ahead of time to apply modifications made by 'schemaTransformers' to the parameter.
var schema = await _componentService.GetOrCreateSchemaAsync(document, GetTargetType(description, parameter), scopedServiceProvider, schemaTransformers, parameter, cancellationToken: cancellationToken);
var openApiParameter = new OpenApiParameter
{
Name = parameter.Name,
Expand All @@ -455,7 +457,7 @@ private async Task<OpenApiResponse> GetResponseAsync(
_ => ParameterLocation.Query
},
Required = IsRequired(parameter),
Schema = await _componentService.GetOrCreateSchemaAsync(document, GetTargetType(description, parameter), scopedServiceProvider, schemaTransformers, parameter, cancellationToken: cancellationToken),
Schema = schema,
Description = GetParameterDescriptionFromAttribute(parameter)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,29 @@ public async Task SchemaTransformer_ExecutesAsynchronously()
Assert.Equal([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3], transformerOrder);
}

[Fact]
public async Task SchemaTransformer_TransformsParameterNameToUppercase()
{
var builder = CreateBuilder();

builder.MapGet("/todo", (int todo) => { });

var options = new OpenApiOptions();
options.AddSchemaTransformer((schema, context, cancellationToken) =>
{
context.ParameterDescription.Name = context.ParameterDescription.Name.ToUpper(CultureInfo.CurrentCulture);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The descriptor from the Api Explorer is modified here. Should this not be done on an Operation Transformer?

I would add a warning to not modify the result coming from the Api Explorer.

I think you can set the parameter name with Metadata on minimal apis. Or it's maybe which is missing and should be added as a feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this was my feedback as well in #62893 (comment).

In an ideal world, the ParameterDescription APIs would've been immutable so things like this don't happen but mutating the ParameterDescription is not the correct way to implement this change and we shouldn't change schema transformer application to resolve this issue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I found that using Operation Transformer can solve my problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to add warnings or restrictions not to modify parameters in Schema Transformer.

return Task.CompletedTask;
});

await VerifyOpenApiDocument(builder, options, document =>
{
var path = Assert.Single(document.Paths.Values);
var getOperation = path.Operations[HttpMethod.Get];
var parameterName = getOperation.Parameters[0].Name;
Assert.Equal("TODO", parameterName);
});
}

private class PolymorphicContainer
{
public string Name { get; }
Expand Down
Loading