From 7cb1dca467b4bcc177d0669329d7452f9af38309 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 4 Apr 2016 22:07:23 -0700 Subject: [PATCH] JsonInputFormatter fails to use custom SerializerSettings Fixes #4270 --- .../JsonInputFormatter.cs | 2 +- .../JsonInputFormatterTest.cs | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs index 41415d5310..502cab2d4e 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs @@ -212,7 +212,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters { if (_jsonSerializerPool == null) { - _jsonSerializerPool = _objectPoolProvider.Create(); + _jsonSerializerPool = _objectPoolProvider.Create(new JsonSerializerObjectPolicy(SerializerSettings)); } return _jsonSerializerPool.Get(); diff --git a/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs b/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs index dcbcfe2650..be68e67f73 100644 --- a/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs @@ -9,7 +9,6 @@ using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; -using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Testing; using Moq; @@ -21,7 +20,6 @@ namespace Microsoft.AspNetCore.Mvc.Formatters { public class JsonInputFormatterTest { - [Theory] [InlineData("application/json", true)] [InlineData("application/*", false)] @@ -358,6 +356,37 @@ namespace Microsoft.AspNetCore.Mvc.Formatters Assert.Contains("Required property 'Password' not found in JSON", modelErrorMessage); } + [Fact] + public void CreateJsonSerializer_UsesJsonSerializerSettings() + { + // Arrange + var settings = new JsonSerializerSettings + { + ContractResolver = Mock.Of(), + MaxDepth = 2, + DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind, + }; + var formatter = new TestableJsonInputFormatter(GetLogger(), settings); + + // Act + var actual = formatter.CreateJsonSerializer(); + + // Assert + Assert.Same(settings.ContractResolver, actual.ContractResolver); + Assert.Equal(settings.MaxDepth, actual.MaxDepth); + Assert.Equal(settings.DateTimeZoneHandling, actual.DateTimeZoneHandling); + } + + private class TestableJsonInputFormatter : JsonInputFormatter + { + public TestableJsonInputFormatter(ILogger logger, JsonSerializerSettings settings) + : base(logger, settings) + { + } + + public new JsonSerializer CreateJsonSerializer() => base.CreateJsonSerializer(); + } + private static ILogger GetLogger() { return NullLogger.Instance;