Skip to content

Propagate PropertyInfo for AsParameters parameters #57264

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
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 @@ -188,7 +188,7 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
return new ApiParameterDescription
{
Name = name,
ModelMetadata = CreateModelMetadata(paramType),
ModelMetadata = CreateModelMetadata(parameter, paramType),
Source = source,
DefaultValue = parameter.ParameterInfo.DefaultValue,
Type = parameter.ParameterInfo.ParameterType,
Expand Down Expand Up @@ -436,6 +436,19 @@ private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
private static EndpointModelMetadata CreateModelMetadata(Type type) =>
new(ModelMetadataIdentity.ForType(type));

private static EndpointModelMetadata CreateModelMetadata(IParameterBindingMetadata parameter, Type type)
{
if (parameter.ParameterInfo is { } parameterInfo)
{
if (parameterInfo.Member is PropertyInfo propertyInfo && propertyInfo.DeclaringType is not null)
{
return new(ModelMetadataIdentity.ForProperty(propertyInfo, type, propertyInfo.DeclaringType));
}
return new(ModelMetadataIdentity.ForParameter(parameterInfo, type));
}
return CreateModelMetadata(type);
}

private static void AddResponseContentTypes(IList<ApiResponseFormat> apiResponseFormats, IReadOnlyList<string> contentTypes)
{
foreach (var contentType in contentTypes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,13 @@ public void AddsMultipleResponseFormatsFromMetadataWithAwaitableResultType()
var apiDescription = GetApiDescription(
[ProducesResponseType(typeof(InferredJsonClass), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
async Task<Results<Created<InferredJsonClass>, ProblemHttpResult>> () => {
await Task.CompletedTask;
return Random.Shared.Next() % 2 == 0
? TypedResults.Created<InferredJsonClass>("/", new InferredJsonClass())
: TypedResults.Problem();
});
async Task<Results<Created<InferredJsonClass>, ProblemHttpResult>> () =>
{
await Task.CompletedTask;
return Random.Shared.Next() % 2 == 0
? TypedResults.Created<InferredJsonClass>("/", new InferredJsonClass())
: TypedResults.Problem();
});

Assert.Equal(2, apiDescription.SupportedResponseTypes.Count);

Expand Down Expand Up @@ -706,6 +707,16 @@ public void SupportsRequiredMembersInAsParametersAttribute()
param => Assert.False(param.IsRequired));
}

[Fact]
public void SupportsContainerTypeInAsParametersAttribute()
{
var apiDescription = GetApiDescription(([AsParameters] AsParametersWithRequiredMembers foo) => { });
Assert.Equal(4, apiDescription.ParameterDescriptions.Count);

Assert.NotNull(apiDescription.ParameterDescriptions[0].ModelMetadata.ContainerType);
Assert.Equal(typeof(AsParametersWithRequiredMembers), apiDescription.ParameterDescriptions[0].ModelMetadata.ContainerType);
}

#nullable disable
public class AsParametersWithRequiredMembersObliviousContext
{
Expand Down
Loading