Skip to content

Fix support for parameter names sourced from attributes (#45572) #45591

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2023
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
30 changes: 15 additions & 15 deletions src/OpenApi/src/OpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static void GenerateDefaultResponses(Dictionary<int, (Type?, MediaTypeCo
var parameters = PropertyAsParameterInfo.Flatten(methodInfo.GetParameters(), ParameterBindingMethodCache);
foreach (var parameter in parameters)
{
var (bodyOrFormParameter, _) = GetOpenApiParameterLocation(parameter, pattern, false);
var (bodyOrFormParameter, _, _) = GetOpenApiParameterLocation(parameter, pattern, false);
hasFormOrBodyParameter |= bodyOrFormParameter;
if (hasFormOrBodyParameter)
{
Expand Down Expand Up @@ -368,7 +368,7 @@ private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, Route
throw new InvalidOperationException($"Encountered a parameter of type '{parameter.ParameterType}' without a name. Parameters must have a name.");
}

var (_, parameterLocation) = GetOpenApiParameterLocation(parameter, pattern, disableInferredBody);
var (_, parameterLocation, attributeName) = GetOpenApiParameterLocation(parameter, pattern, disableInferredBody);

// if the parameter doesn't have a valid location
// then we should ignore it
Expand All @@ -379,7 +379,7 @@ private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, Route
var nullabilityContext = new NullabilityInfoContext();
var nullability = nullabilityContext.Create(parameter);
var isOptional = parameter.HasDefaultValue || nullability.ReadState != NullabilityState.NotNull;
var name = pattern.GetParameter(parameter.Name) is { } routeParameter ? routeParameter.Name : parameter.Name;
var name = attributeName ?? (pattern.GetParameter(parameter.Name) is { } routeParameter ? routeParameter.Name : parameter.Name);
var openApiParameter = new OpenApiParameter()
{
Name = name,
Expand All @@ -393,29 +393,29 @@ private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, Route
return openApiParameters;
}

private (bool isBodyOrForm, ParameterLocation? locatedIn) GetOpenApiParameterLocation(ParameterInfo parameter, RoutePattern pattern, bool disableInferredBody)
private (bool isBodyOrForm, ParameterLocation? locatedIn, string? name) GetOpenApiParameterLocation(ParameterInfo parameter, RoutePattern pattern, bool disableInferredBody)
{
var attributes = parameter.GetCustomAttributes();

if (attributes.OfType<IFromRouteMetadata>().FirstOrDefault() is { } routeAttribute)
{
return (false, ParameterLocation.Path);
return (false, ParameterLocation.Path, routeAttribute.Name);
}
else if (attributes.OfType<IFromQueryMetadata>().FirstOrDefault() is { } queryAttribute)
{
return (false, ParameterLocation.Query);
return (false, ParameterLocation.Query, queryAttribute.Name);
}
else if (attributes.OfType<IFromHeaderMetadata>().FirstOrDefault() is { } headerAttribute)
{
return (false, ParameterLocation.Header);
return (false, ParameterLocation.Header, headerAttribute.Name);
}
else if (attributes.OfType<IFromBodyMetadata>().FirstOrDefault() is { } fromBodyAttribute)
{
return (true, null);
return (true, null, null);
}
else if (attributes.OfType<IFromFormMetadata>().FirstOrDefault() is { } fromFormAttribute)
{
return (true, null);
return (true, null, null);
}
else if (parameter.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType)) ||
parameter.ParameterType == typeof(HttpContext) ||
Expand All @@ -426,7 +426,7 @@ private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, Route
ParameterBindingMethodCache.HasBindAsyncMethod(parameter) ||
_serviceProviderIsService?.IsService(parameter.ParameterType) == true)
{
return (false, null);
return (false, null, null);
}
else if (parameter.ParameterType == typeof(string) || ParameterBindingMethodCache.HasTryParseMethod(parameter.ParameterType))
{
Expand All @@ -436,27 +436,27 @@ private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, Route
// Path vs query cannot be determined by RequestDelegateFactory at startup currently because of the layering, but can be done here.
if (parameter.Name is { } name && pattern.GetParameter(name) is not null)
{
return (false, ParameterLocation.Path);
return (false, ParameterLocation.Path, null);
}
else
{
return (false, ParameterLocation.Query);
return (false, ParameterLocation.Query, null);
}
}
else if (parameter.ParameterType == typeof(IFormFile) || parameter.ParameterType == typeof(IFormFileCollection))
{
return (true, null);
return (true, null, null);
}
else if (disableInferredBody && (
(parameter.ParameterType.IsArray && ParameterBindingMethodCache.HasTryParseMethod(parameter.ParameterType.GetElementType()!)) ||
parameter.ParameterType == typeof(string[]) ||
parameter.ParameterType == typeof(StringValues)))
{
return (false, ParameterLocation.Query);
return (false, ParameterLocation.Query, null);
}
else
{
return (true, null);
return (true, null, null);
}
}
}
15 changes: 15 additions & 0 deletions src/OpenApi/test/OpenApiGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,21 @@ public void HandlesEndpointWithNoRequestBody()
Assert.Null(operationWithNoBodyParams.RequestBody);
}

[Fact]
public void HandlesParameterWithNameInAttribute()
{
static void ValidateParameter(OpenApiOperation operation, string expectedName)
{
var parameter = Assert.Single(operation.Parameters);
Assert.Equal(expectedName, parameter.Name);
}

ValidateParameter(GetOpenApiOperation(([FromRoute(Name = "routeName")] string param) => ""), "routeName");
ValidateParameter(GetOpenApiOperation(([FromRoute(Name = "routeName")] string param) => "", "/{param}"), "routeName");
ValidateParameter(GetOpenApiOperation(([FromQuery(Name = "queryName")] string param) => ""), "queryName");
ValidateParameter(GetOpenApiOperation(([FromHeader(Name = "headerName")] string param) => ""), "headerName");
}

private static OpenApiOperation GetOpenApiOperation(
Delegate action,
string pattern = null,
Expand Down