Merge branch 'release/2.1' into dev

This commit is contained in:
Kiran Challa 2018-03-27 10:06:51 -07:00
commit 1186fa66ab
10 changed files with 118 additions and 30 deletions

View File

@ -65,6 +65,7 @@
<MicrosoftExtensionsLoggingTestingPackageVersion>2.1.0-preview3-32037</MicrosoftExtensionsLoggingTestingPackageVersion>
<MicrosoftExtensionsObjectMethodExecutorSourcesPackageVersion>2.1.0-preview3-32037</MicrosoftExtensionsObjectMethodExecutorSourcesPackageVersion>
<MicrosoftExtensionsOptionsPackageVersion>2.1.0-preview3-32037</MicrosoftExtensionsOptionsPackageVersion>
<MicrosoftExtensionsParameterDefaultValueSourcesPackageVersion>2.1.0-preview2-30355</MicrosoftExtensionsParameterDefaultValueSourcesPackageVersion>
<MicrosoftExtensionsPrimitivesPackageVersion>2.1.0-preview3-32037</MicrosoftExtensionsPrimitivesPackageVersion>
<MicrosoftExtensionsPropertyActivatorSourcesPackageVersion>2.1.0-preview3-32037</MicrosoftExtensionsPropertyActivatorSourcesPackageVersion>
<MicrosoftExtensionsPropertyHelperSourcesPackageVersion>2.1.0-preview3-32037</MicrosoftExtensionsPropertyHelperSourcesPackageVersion>

View File

@ -4,6 +4,7 @@
using System;
using System.ComponentModel;
using System.Reflection;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Mvc.Internal
{
@ -21,34 +22,25 @@ namespace Microsoft.AspNetCore.Mvc.Internal
for (var i = 0; i < parameters.Length; i++)
{
var parameterInfo = parameters[i];
object defaultValue;
if (parameterInfo.HasDefaultValue)
{
defaultValue = parameterInfo.DefaultValue;
}
else
{
var defaultValueAttribute = parameterInfo
.GetCustomAttribute<DefaultValueAttribute>(inherit: false);
if (defaultValueAttribute?.Value == null)
{
defaultValue = parameterInfo.ParameterType.GetTypeInfo().IsValueType
? Activator.CreateInstance(parameterInfo.ParameterType)
: null;
}
else
{
defaultValue = defaultValueAttribute.Value;
}
}
values[i] = defaultValue;
values[i] = GetParameterDefaultValue(parameters[i]);
}
return values;
}
private static object GetParameterDefaultValue(ParameterInfo parameterInfo)
{
if (!ParameterDefaultValue.TryGetDefaultValue(parameterInfo, out var defaultValue))
{
var defaultValueAttribute = parameterInfo.GetCustomAttribute<DefaultValueAttribute>(inherit: false);
defaultValue = defaultValueAttribute?.Value;
if (defaultValue == null && parameterInfo.ParameterType.IsValueType)
{
defaultValue = Activator.CreateInstance(parameterInfo.ParameterType);
}
}
return defaultValue;
}
}
}

View File

@ -34,6 +34,7 @@ Microsoft.AspNetCore.Mvc.RouteAttribute</Description>
<PackageReference Include="Microsoft.Extensions.HashCodeCombiner.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsHashCodeCombinerSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsLoggingAbstractionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.ObjectMethodExecutor.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsObjectMethodExecutorSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.ParameterDefaultValue.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsParameterDefaultValueSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.PropertyActivator.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsPropertyActivatorSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.PropertyHelper.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsPropertyHelperSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.SecurityHelper.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsSecurityHelperSourcesPackageVersion)" />

View File

@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
@ -194,11 +195,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{
// Do nothing, already set the value.
}
else if (parameter.ParameterInfo.HasDefaultValue)
{
value = parameter.ParameterInfo.DefaultValue;
}
else if (parameter.ParameterInfo.ParameterType.IsValueType)
else if (!ParameterDefaultValue.TryGetDefaultValue(parameter.ParameterInfo, out value) &&
parameter.ParameterInfo.ParameterType.IsValueType)
{
value = Activator.CreateInstance(parameter.ParameterInfo.ParameterType);
}

View File

@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.Extensions.ClosedGenericMatcher.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsClosedGenericMatcherSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.CopyOnWriteDictionary.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsCopyOnWriteDictionarySourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.ParameterDefaultValue.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsParameterDefaultValueSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.PropertyActivator.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsPropertyActivatorSourcesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.PropertyHelper.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsPropertyHelperSourcesPackageVersion)" />
</ItemGroup>

View File

@ -1,6 +1,7 @@
// 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.ComponentModel;
using Xunit;
@ -25,6 +26,21 @@ namespace Microsoft.AspNetCore.Mvc.Internal
Assert.Equal(expectedValues, actualValues);
}
[Fact]
public void GetParameterDefaultValues_ReturnsExpectedValues_ForStructTypes()
{
// Arrange
var methodInfo = typeof(TestObject).GetMethod("DefaultValuesOfStructTypes");
// Act
var actualValues = ParameterDefaultValues.GetParameterDefaultValues(methodInfo);
// Assert
Assert.Equal(
new object[] { default(Guid), default(TimeSpan), default(DateTime), default(DateTimeOffset) },
actualValues);
}
private class TestObject
{
public void DefaultAttributes(
@ -54,6 +70,16 @@ namespace Microsoft.AspNetCore.Mvc.Internal
TestObject input4)
{
}
// Note that default value for DateTime currently throws a FormatException
// https://github.com/dotnet/corefx/issues/12338
public void DefaultValuesOfStructTypes(
Guid guid = default(Guid),
TimeSpan timeSpan = default(TimeSpan),
DateTime dateTime = default(DateTime),
DateTimeOffset dateTimeOffset = default(DateTimeOffset))
{
}
}
}
}

View File

@ -1,6 +1,7 @@
// 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.Net.Http;
using System.Threading.Tasks;
using Xunit;
@ -71,5 +72,35 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
// Assert
Assert.Equal(expected, response);
}
[Fact]
public async Task Controller_WithDefaultParameterValues_ForStructs_ReturnsDefaults()
{
// Arrange
var expected = $"{default(Guid)}, {default(TimeSpan)}";
var url = "http://localhost/DefaultValues/EchoValue_DefaultParameterValue_ForStructs";
// Act
var response = await Client.GetStringAsync(url);
// Assert
Assert.Equal(expected, response);
}
[Fact]
public async Task Controller_WithDefaultParameterValues_ForStructs_ReturnsBoundValues()
{
// Arrange
Guid guid = Guid.NewGuid();
TimeSpan timeSpan = new TimeSpan(10, 10, 10);
var expected = $"{guid}, {timeSpan}";
var url = $"http://localhost/DefaultValues/EchoValue_DefaultParameterValue_ForStructs?guid={guid}&timespan={timeSpan}";
// Act
var response = await Client.GetStringAsync(url);
// Assert
Assert.Equal(expected, response);
}
}
}

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
@ -10,6 +11,7 @@ using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Testing;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
@ -1251,6 +1253,23 @@ Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary`1[AspNetCore.InjectedPa
Assert.Equal("<p>Hey, it's Mr. totally custom here!</p>", content.Trim());
}
[Fact]
public async Task Page_Handler_BindsToDefaultValues()
{
// Arrange
string expected;
using (new CultureReplacer(CultureInfo.InvariantCulture, CultureInfo.InvariantCulture))
{
expected = $"id: 10, guid: {default(Guid)}, boolean: {default(bool)}, dateTime: {default(DateTime)}";
}
// Act
var content = await Client.GetStringAsync("http://localhost/ModelHandlerTestPage/DefaultValues");
// Assert
Assert.Equal(expected, content);
}
private async Task AddAntiforgeryHeaders(HttpRequestMessage request)
{
var getResponse = await Client.GetAsync(request.RequestUri);

View File

@ -1,6 +1,7 @@
// 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.ComponentModel;
using Microsoft.AspNetCore.Mvc;
@ -19,5 +20,13 @@ namespace BasicWebSite.Controllers
{
return input;
}
[HttpGet]
public string EchoValue_DefaultParameterValue_ForStructs(
Guid guid = default(Guid),
TimeSpan timeSpan = default(TimeSpan))
{
return $"{guid}, {timeSpan}";
}
}
}

View File

@ -1,6 +1,7 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
@ -35,6 +36,15 @@ namespace RazorPagesWebSite
MethodName = nameof(OnGetViewCustomerAsync);
}
public IActionResult OnGetDefaultValues(
bool boolean,
int id = 10,
Guid guid = default(Guid),
DateTime dateTime = default(DateTime))
{
return Content($"id: {id}, guid: {guid}, boolean: {boolean}, dateTime: {dateTime}");
}
public async Task<CustomActionResult> OnPostCustomActionResult()
{
await Task.CompletedTask;