Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Infer BindingSource.FormFile for IEnumerable<IFormFile>
Browse files Browse the repository at this point in the history
Fixes #7770
  • Loading branch information
pranavkm committed May 22, 2018
1 parent 077b1d8 commit 49c653e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ internal static void ConfigureAdditionalModelMetadataDetailsProviders(IList<IMet
modelMetadataDetailsProviders.Add(new BindingSourceMetadataProvider(typeof(IFormFile), BindingSource.FormFile));
modelMetadataDetailsProviders.Add(new BindingSourceMetadataProvider(typeof(IFormCollection), BindingSource.FormFile));
modelMetadataDetailsProviders.Add(new BindingSourceMetadataProvider(typeof(IFormFileCollection), BindingSource.FormFile));
modelMetadataDetailsProviders.Add(new BindingSourceMetadataProvider(typeof(IEnumerable<IFormFile>), BindingSource.FormFile));

// Add types to be excluded from Validation
modelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Type)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,52 @@ public void OnProvidersExecuting_AddsBindingSources_ForActionParameters_WithLega
});
}

[Fact]
public void OnProvidersExecuting_InfersFormFileSourceForTypesAssignableFromIEnumerableOfFormFiles()
{
// Arrange
var builder = new TestApplicationModelProvider(
new MvcOptions { AllowValidatingTopLevelNodes = true },
TestModelMetadataProvider.CreateDefaultProvider());
var typeInfo = typeof(ModelBinderController).GetTypeInfo();

var context = new ApplicationModelProviderContext(new[] { typeInfo });

// Act
builder.OnProvidersExecuting(context);

// Assert
var controllerModel = Assert.Single(context.Result.Controllers);
var action = Assert.Single(controllerModel.Actions, a => a.ActionMethod.Name == nameof(ModelBinderController.FormFilesSequences));
Assert.Collection(
action.Parameters,
parameter =>
{
Assert.Equal("formFileEnumerable", parameter.ParameterName);
Assert.Equal(BindingSource.FormFile, parameter.BindingInfo.BindingSource);
},
parameter =>
{
Assert.Equal("formFileCollection", parameter.ParameterName);
Assert.Equal(BindingSource.FormFile, parameter.BindingInfo.BindingSource);
},
parameter =>
{
Assert.Equal("formFileIList", parameter.ParameterName);
Assert.Equal(BindingSource.FormFile, parameter.BindingInfo.BindingSource);
},
parameter =>
{
Assert.Equal("formFileList", parameter.ParameterName);
Assert.Equal(BindingSource.FormFile, parameter.BindingInfo.BindingSource);
},
parameter =>
{
Assert.Equal("formFileArray", parameter.ParameterName);
Assert.Equal(BindingSource.FormFile, parameter.BindingInfo.BindingSource);
});
}

[Fact]
public void OnProvidersExecuting_AddsBindingSources_ForActionParameters_ReadFromModelMetadata()
{
Expand Down Expand Up @@ -1625,6 +1671,13 @@ public class ModelBinderController

public IActionResult PostAction([FromQuery] string fromQuery, IFormFileCollection formFileCollection, string unbound) => null;

public IActionResult FormFilesSequences(
IEnumerable<IFormFile> formFileEnumerable,
ICollection<IFormFile> formFileCollection,
IList<IFormFile> formFileIList,
List<IFormFile> formFileList,
IFormFile[] formFileArray) => null;

public IActionResult PostAction1(Guid guid) => null;

public IActionResult PostAction2([FromQuery] Guid fromQuery) => null;
Expand Down
7 changes: 7 additions & 0 deletions test/Microsoft.AspNetCore.Mvc.Test/MvcOptionsSetupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -189,6 +190,12 @@ public void Setup_SetsUpMetadataDetailsProviders()
Assert.Equal(BindingSource.FormFile, formFileParameter.BindingSource);
},
provider =>
{
var formFileParameter = Assert.IsType<BindingSourceMetadataProvider>(provider);
Assert.Equal(typeof(IEnumerable<IFormFile>), formFileParameter.Type);
Assert.Equal(BindingSource.FormFile, formFileParameter.BindingSource);
},
provider =>
{
var excludeFilter = Assert.IsType<SuppressChildValidationMetadataProvider>(provider);
Assert.Equal(typeof(Type), excludeFilter.Type);
Expand Down

0 comments on commit 49c653e

Please sign in to comment.