Merge branch 'merge/release/2.2-to-master'

This commit is contained in:
Kiran Challa 2018-08-01 10:04:56 -07:00
commit 1431413c56
3 changed files with 45 additions and 15 deletions

View File

@ -15,10 +15,12 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
// return shared resolver by default for perf so slow reflection logic is cached once
// developers can set their own resolver after the settings are returned if desired
private static readonly DefaultContractResolver SharedContractResolver = new DefaultContractResolver
private static readonly DefaultContractResolver SharedContractResolver;
static JsonSerializerSettingsProvider()
{
NamingStrategy = new CamelCaseNamingStrategy(),
};
SharedContractResolver = CreateContractResolver();
}
/// <summary>
/// Creates default <see cref="JsonSerializerSettings"/>.
@ -41,5 +43,14 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
TypeNameHandling = TypeNameHandling.None,
};
}
// To enable unit testing
internal static DefaultContractResolver CreateContractResolver()
{
return new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy(),
};
}
}
}

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Formatters.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
@ -37,7 +38,8 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseCamelCasing_WillNot_OverrideSpecifiedNames()
{
// Arrange
var options = new MvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var options = CreateDefaultMvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var annotatedFoo = new AnnotatedFoo()
{
HelloWorld = "Hello"
@ -55,7 +57,7 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseCamelCasing_WillChange_PropertyNames()
{
// Arrange
var options = new MvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var options = CreateDefaultMvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var foo = new { TestName = "TestFoo", TestValue = 10 };
var expected = "{\"testName\":\"TestFoo\",\"testValue\":10}";
@ -70,7 +72,7 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseCamelCasing_WillChangeFirstPartBeforeSeparator_InPropertyName()
{
// Arrange
var options = new MvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var options = CreateDefaultMvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var foo = new { TestFoo_TestValue = "Test" };
var expected = "{\"testFoo_TestValue\":\"Test\"}";
@ -85,7 +87,7 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseCamelCasing_ProcessDictionaryKeys_WillChange_DictionaryKeys_IfTrue()
{
// Arrange
var options = new MvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var options = CreateDefaultMvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var dictionary = new Dictionary<string, int>
{
["HelloWorld"] = 1,
@ -104,7 +106,7 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseCamelCasing_ProcessDictionaryKeys_WillChangeFirstPartBeforeSeparator_InDictionaryKey_IfTrue()
{
// Arrange
var options = new MvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var options = CreateDefaultMvcJsonOptions().UseCamelCasing(processDictionaryKeys: true);
var dictionary = new Dictionary<string, int>()
{
["HelloWorld_HelloWorld"] = 1
@ -123,7 +125,7 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseCamelCasing_ProcessDictionaryKeys_WillNotChangeDictionaryKeys_IfFalse()
{
// Arrange
var options = new MvcJsonOptions().UseCamelCasing(processDictionaryKeys: false);
var options = CreateDefaultMvcJsonOptions().UseCamelCasing(processDictionaryKeys: false);
var dictionary = new Dictionary<string, int>
{
["HelloWorld"] = 1,
@ -142,7 +144,7 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseMemberCasing_WillNotChange_OverrideSpecifiedNames()
{
// Arrange
var options = new MvcJsonOptions().UseMemberCasing();
var options = CreateDefaultMvcJsonOptions().UseMemberCasing();
var annotatedFoo = new AnnotatedFoo()
{
HelloWorld = "Hello"
@ -180,8 +182,8 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseMemberCasing_WillNotChange_PropertyNames()
{
// Arrange
var options = new MvcJsonOptions().UseMemberCasing();
var foo = new { fooName = "Test", FooValue = "Value"};
var options = CreateDefaultMvcJsonOptions().UseMemberCasing();
var foo = new { fooName = "Test", FooValue = "Value" };
var expected = "{\"fooName\":\"Test\",\"FooValue\":\"Value\"}";
// Act
@ -195,7 +197,7 @@ namespace Microsoft.Extensions.DependencyInjection
public void UseMemberCasing_WillNotChange_DictionaryKeys()
{
// Arrange
var options = new MvcJsonOptions().UseMemberCasing();
var options = CreateDefaultMvcJsonOptions().UseMemberCasing();
var dictionary = new Dictionary<string, int>()
{
["HelloWorld"] = 1,
@ -239,6 +241,15 @@ namespace Microsoft.Extensions.DependencyInjection
Assert.Equal(expectedMessage, actual: exception.Message);
}
// NOTE: This method was created to make sure to create a different instance of contract resolver as by default
// MvcJsonOptions uses a static shared instance of resolver which when changed causes other tests to fail.
private MvcJsonOptions CreateDefaultMvcJsonOptions()
{
var options = new MvcJsonOptions();
options.SerializerSettings.ContractResolver = JsonSerializerSettingsProvider.CreateContractResolver();
return options;
}
private static string SerializeToJson(MvcJsonOptions options, object value)
{
return JsonConvert.SerializeObject(

View File

@ -32,15 +32,23 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.True(result);
}
[Fact(Skip = "Link generation issue in global routing. Need to fix - https://github.com/aspnet/Routing/issues/590")]
[Fact]
public override Task AttributeRoutedAction_InArea_StaysInArea_ActionDoesntExist()
{
// By design, this test cannot work in EndpointRouting world. This is because in case of old routing test
// when a link generation to an attribute routed controller with a non-existing action does not succeeed,
// the next route in the route collection is considered and since the next route in the route collection is
// a conventional area route, the old routing test succeeds. But this cannot happen in case of endpoint
// routing as the action does not exist to begin with.
return Task.CompletedTask;
}
[Fact(Skip = "Link generation issue in global routing. Need to fix - https://github.com/aspnet/Routing/issues/590")]
[Fact]
public override Task ConventionalRoutedAction_InArea_StaysInArea()
{
// By design, this test cannot work in EndpointRouting world. In old routing test a link is being generated
// to a non-existing action on a controller which is in an area. In case of endpoint routing, we cannot
// generate links as the action does not exist to begin with.
return Task.CompletedTask;
}