This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add BindingSourceMetadataProvider #5750
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1742ad9
Add SpecialParametersBindingMetadataProvider
jbagga ef2ee29
Formatting
jbagga 8b9acbb
Add FormFileBindingSourceMetadataProvider
jbagga d475f00
Added unit tests
jbagga 5e13ef1
PR feedback and integration tests
jbagga 1b4c30d
PR feedback addressed
jbagga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Microsoft.AspNetCore.Mvc.Abstractions/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Metadata/BindingSourceMetadataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Threading; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata | ||
{ | ||
public class BindingSourceMetadataProvider : IBindingMetadataProvider | ||
{ | ||
/// <summary> | ||
/// Creates a new <see cref="BindingSourceMetadataProvider"/> for the given <paramref name="type"/>. | ||
/// </summary> | ||
/// <param name="type"> | ||
/// The <see cref="Type"/>. The provider sets <see cref="BindingSource"/> of the given <see cref="Type"/> or | ||
/// anything assignable to the given <see cref="Type"/>. | ||
/// </param> | ||
/// <param name="bindingSource"> | ||
/// The <see cref="BindingSource"/> to assign to the given <paramref name="type"/>. | ||
/// </param> | ||
public BindingSourceMetadataProvider(Type type, BindingSource bindingSource) | ||
{ | ||
if (type == null) | ||
{ | ||
throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
Type = type; | ||
BindingSource = bindingSource; | ||
} | ||
|
||
public Type Type { get; } | ||
public BindingSource BindingSource { get; } | ||
|
||
/// <inheritdoc /> | ||
public void CreateBindingMetadata(BindingMetadataProviderContext context) | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (Type.IsAssignableFrom(context.Key.ModelType)) | ||
{ | ||
context.BindingMetadata.BindingSource = BindingSource; | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...soft.AspNetCore.Mvc.Core.Test/ModelBinding/Metadata/BindingSourceMetadataProviderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata | ||
{ | ||
public class BindingSourceMetadataProviderTests | ||
{ | ||
[Fact] | ||
public void CreateBindingMetadata_ForMatchingType_SetsBindingSource() | ||
{ | ||
// Arrange | ||
var provider = new BindingSourceMetadataProvider(typeof(Test), BindingSource.Special); | ||
|
||
var key = ModelMetadataIdentity.ForType(typeof(Test)); | ||
|
||
var context = new BindingMetadataProviderContext(key, new ModelAttributes(new object[0], new object[0])); | ||
|
||
// Act | ||
provider.CreateBindingMetadata(context); | ||
|
||
// Assert | ||
Assert.Equal(BindingSource.Special, context.BindingMetadata.BindingSource); | ||
} | ||
|
||
private class Test | ||
{ | ||
} | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
...Microsoft.AspNetCore.Mvc.IntegrationTests/BindingSourceMetadataProviderIntegrationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Internal; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Internal; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; | ||
using Microsoft.Extensions.Primitives; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.IntegrationTests | ||
{ | ||
public class BindingSourceMetadataProviderIntegrationTest | ||
{ | ||
[Fact] | ||
public async Task BindParameter_WithCancellationToken_BindingSourceSpecial() | ||
{ | ||
// Arrange | ||
var options = new MvcOptions(); | ||
var setup = new MvcCoreMvcOptionsSetup(new TestHttpRequestStreamReaderFactory()); | ||
|
||
options.ModelBinderProviders.Insert(0, new CancellationTokenModelBinderProvider()); | ||
|
||
setup.Configure(options); | ||
|
||
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(options); | ||
var parameter = new ParameterDescriptor() | ||
{ | ||
Name = "Parameter1", | ||
BindingInfo = new BindingInfo(), | ||
ParameterType = typeof(CancellationTokenBundle), | ||
}; | ||
|
||
var testContext = ModelBindingTestHelper.GetTestContext(request => | ||
{ | ||
request.Form = new FormCollection(new Dictionary<string, StringValues> | ||
{ | ||
{ "name", new[] { "Fred" } } | ||
}); | ||
}); | ||
|
||
var modelState = testContext.ModelState; | ||
var token = testContext.HttpContext.RequestAborted; | ||
|
||
// Act | ||
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext); | ||
|
||
// Assert | ||
// ModelBindingResult | ||
Assert.True(modelBindingResult.IsModelSet); | ||
|
||
// Model | ||
var boundPerson = Assert.IsType<CancellationTokenBundle>(modelBindingResult.Model); | ||
Assert.NotNull(boundPerson); | ||
Assert.Equal("Fred", boundPerson.Name); | ||
Assert.Equal(token, boundPerson.Token); | ||
|
||
// ModelState | ||
Assert.True(modelState.IsValid); | ||
} | ||
|
||
private class CancellationTokenBundle | ||
{ | ||
public string Name { get; set; } | ||
|
||
public CancellationToken Token { get; set; } | ||
} | ||
|
||
[Fact] | ||
public async Task BindParameter_WithFormFile_BindingSourceFormFile() | ||
{ | ||
// Arrange | ||
var options = new MvcOptions(); | ||
var setup = new MvcCoreMvcOptionsSetup(new TestHttpRequestStreamReaderFactory()); | ||
|
||
options.ModelBinderProviders.Insert(0, new FormFileModelBinderProvider()); | ||
|
||
setup.Configure(options); | ||
|
||
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(options); | ||
var parameter = new ParameterDescriptor() | ||
{ | ||
Name = "Parameter1", | ||
BindingInfo = new BindingInfo(), | ||
ParameterType = typeof(FormFileBundle), | ||
}; | ||
|
||
var data = "Some Data Is Better Than No Data."; | ||
var testContext = ModelBindingTestHelper.GetTestContext( | ||
request => | ||
{ | ||
request.QueryString = QueryString.Create("Name", "Fred"); | ||
UpdateRequest(request, data, "File"); | ||
}); | ||
|
||
var modelState = testContext.ModelState; | ||
|
||
// Act | ||
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext); | ||
|
||
// Assert | ||
// ModelBindingResult | ||
Assert.True(modelBindingResult.IsModelSet); | ||
|
||
// Model | ||
var boundPerson = Assert.IsType<FormFileBundle>(modelBindingResult.Model); | ||
Assert.Equal("Fred", boundPerson.Name); | ||
Assert.Equal("text.txt", boundPerson.File.FileName); | ||
|
||
// ModelState | ||
Assert.True(modelState.IsValid); | ||
} | ||
|
||
private class FormFileBundle | ||
{ | ||
public string Name { get; set; } | ||
|
||
public IFormFile File { get; set; } | ||
} | ||
|
||
private void UpdateRequest(HttpRequest request, string data, string name) | ||
{ | ||
const string fileName = "text.txt"; | ||
var fileCollection = new FormFileCollection(); | ||
var formCollection = new FormCollection(new Dictionary<string, StringValues>(), fileCollection); | ||
|
||
request.Form = formCollection; | ||
request.ContentType = "multipart/form-data; boundary=----WebKitFormBoundarymx2fSWqWSd0OxQqq"; | ||
|
||
if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(name)) | ||
{ | ||
// Leave the submission empty. | ||
return; | ||
} | ||
|
||
request.Headers["Content-Disposition"] = $"form-data; name={name}; filename={fileName}"; | ||
|
||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(data)); | ||
fileCollection.Add(new FormFile(memoryStream, 0, data.Length, name, fileName) | ||
{ | ||
Headers = request.Headers | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert.Same