Skip to content

Commit 0e812ec

Browse files
jgarciadelanocedaJavier García de la Noceda Argüelles
and
Javier García de la Noceda Argüelles
authored
Propagate PropertyInfo for AsParameters parameters (#57264)
* Propagate PropertyInfo for AsParameters parameters * Apply PR suggestions * Better with previous implementation but taking into account parameter --------- Co-authored-by: Javier García de la Noceda Argüelles <jgarcian@riamoneytransfer.com>
1 parent 987dbfb commit 0e812ec

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
188188
return new ApiParameterDescription
189189
{
190190
Name = name,
191-
ModelMetadata = CreateModelMetadata(paramType),
191+
ModelMetadata = CreateModelMetadata(parameter, paramType),
192192
Source = source,
193193
DefaultValue = parameter.ParameterInfo.DefaultValue,
194194
Type = parameter.ParameterInfo.ParameterType,
@@ -436,6 +436,19 @@ private static ApiResponseType CreateDefaultApiResponseType(Type responseType)
436436
private static EndpointModelMetadata CreateModelMetadata(Type type) =>
437437
new(ModelMetadataIdentity.ForType(type));
438438

439+
private static EndpointModelMetadata CreateModelMetadata(IParameterBindingMetadata parameter, Type type)
440+
{
441+
if (parameter.ParameterInfo is { } parameterInfo)
442+
{
443+
if (parameterInfo.Member is PropertyInfo propertyInfo && propertyInfo.DeclaringType is not null)
444+
{
445+
return new(ModelMetadataIdentity.ForProperty(propertyInfo, type, propertyInfo.DeclaringType));
446+
}
447+
return new(ModelMetadataIdentity.ForParameter(parameterInfo, type));
448+
}
449+
return CreateModelMetadata(type);
450+
}
451+
439452
private static void AddResponseContentTypes(IList<ApiResponseFormat> apiResponseFormats, IReadOnlyList<string> contentTypes)
440453
{
441454
foreach (var contentType in contentTypes)

src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,13 @@ public void AddsMultipleResponseFormatsFromMetadataWithAwaitableResultType()
385385
var apiDescription = GetApiDescription(
386386
[ProducesResponseType(typeof(InferredJsonClass), StatusCodes.Status201Created)]
387387
[ProducesResponseType(StatusCodes.Status400BadRequest)]
388-
async Task<Results<Created<InferredJsonClass>, ProblemHttpResult>> () => {
389-
await Task.CompletedTask;
390-
return Random.Shared.Next() % 2 == 0
391-
? TypedResults.Created<InferredJsonClass>("/", new InferredJsonClass())
392-
: TypedResults.Problem();
393-
});
388+
async Task<Results<Created<InferredJsonClass>, ProblemHttpResult>> () =>
389+
{
390+
await Task.CompletedTask;
391+
return Random.Shared.Next() % 2 == 0
392+
? TypedResults.Created<InferredJsonClass>("/", new InferredJsonClass())
393+
: TypedResults.Problem();
394+
});
394395

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

@@ -706,6 +707,16 @@ public void SupportsRequiredMembersInAsParametersAttribute()
706707
param => Assert.False(param.IsRequired));
707708
}
708709

710+
[Fact]
711+
public void SupportsContainerTypeInAsParametersAttribute()
712+
{
713+
var apiDescription = GetApiDescription(([AsParameters] AsParametersWithRequiredMembers foo) => { });
714+
Assert.Equal(4, apiDescription.ParameterDescriptions.Count);
715+
716+
Assert.NotNull(apiDescription.ParameterDescriptions[0].ModelMetadata.ContainerType);
717+
Assert.Equal(typeof(AsParametersWithRequiredMembers), apiDescription.ParameterDescriptions[0].ModelMetadata.ContainerType);
718+
}
719+
709720
#nullable disable
710721
public class AsParametersWithRequiredMembersObliviousContext
711722
{

0 commit comments

Comments
 (0)