[Fixes #2715] Using default SerializerSettings from MvcJsonOptions

This commit is contained in:
Ajay Bhargav Baaskaran 2015-06-30 11:15:41 -07:00
parent 15cb2ce709
commit d2d4d1d1d3
4 changed files with 70 additions and 10 deletions

View File

@ -3,8 +3,9 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
@ -27,8 +28,8 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
/// <param name="value">The value to format as JSON.</param>
public JsonResult(object value)
: this(value, serializerSettings: SerializerSettingsProvider.CreateSerializerSettings())
{
Value = value;
}
/// <summary>
@ -86,12 +87,23 @@ namespace Microsoft.AspNet.Mvc
response.StatusCode = StatusCode.Value;
}
var serializerSettings = _serializerSettings;
if (serializerSettings == null)
{
serializerSettings = context
.HttpContext
.RequestServices
.GetRequiredService<IOptions<MvcJsonOptions>>()
.Options
.SerializerSettings;
}
using (var writer = new HttpResponseStreamWriter(response.Body, contentTypeHeader.Encoding))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
jsonWriter.CloseOutput = false;
var jsonSerializer = JsonSerializer.Create(_serializerSettings);
var jsonSerializer = JsonSerializer.Create(serializerSettings);
jsonSerializer.Serialize(jsonWriter, Value);
}
}

View File

@ -2,8 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
using Newtonsoft.Json;
namespace Microsoft.AspNet.Mvc
@ -20,8 +21,8 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
/// <param name="value">The value to format as JSON text.</param>
public JsonViewComponentResult(object value)
: this(value, serializerSettings: SerializerSettingsProvider.CreateSerializerSettings())
{
Value = value;
}
/// <summary>
@ -47,10 +48,22 @@ namespace Microsoft.AspNet.Mvc
/// <param name="context">The <see cref="ViewComponentContext"/>.</param>
public void Execute([NotNull] ViewComponentContext context)
{
var serializerSettings = _serializerSettings;
if (serializerSettings == null)
{
serializerSettings = context
.ViewContext
.HttpContext
.RequestServices
.GetRequiredService<IOptions<MvcJsonOptions>>()
.Options
.SerializerSettings;
}
using (var jsonWriter = new JsonTextWriter(context.Writer))
{
jsonWriter.CloseOutput = false;
var jsonSerializer = JsonSerializer.Create(_serializerSettings);
var jsonSerializer = JsonSerializer.Create(serializerSettings);
jsonSerializer.Serialize(jsonWriter, Value);
}
}

View File

@ -1,12 +1,14 @@
// 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.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Xunit;
@ -109,15 +111,18 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal("application/json; charset=utf-8", context.Response.ContentType);
}
private HttpContext GetHttpContext()
private static HttpContext GetHttpContext()
{
var httpContext = new DefaultHttpContext();
httpContext.Response.Body = new MemoryStream();
var services = new ServiceCollection();
services.AddOptions();
httpContext.RequestServices = services.BuildServiceProvider();
return httpContext;
}
private byte[] GetWrittenBytes(HttpContext context)
private static byte[] GetWrittenBytes(HttpContext context)
{
context.Response.Body.Seek(0, SeekOrigin.Begin);
return Assert.IsType<MemoryStream>(context.Response.Body).ToArray();

View File

@ -3,11 +3,13 @@
using System.IO;
using System.Text;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewComponents;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Moq;
using Newtonsoft.Json;
using Xunit;
@ -17,7 +19,7 @@ namespace Microsoft.AspNet.Mvc
public class JsonViewComponentResultTest
{
[Fact]
public void Execute_UsesFormatter_WithSpecifiedSerializerSettings()
public void Execute_UsesSerializer_WithSpecifiedSerializerSettings()
{
// Arrange
var view = Mock.Of<IView>();
@ -37,9 +39,27 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal("{\r\n \"foo\": \"abcd\"\r\n}", Encoding.UTF8.GetString(buffer.ToArray()));
}
[Fact]
public void Execute_UsesSerializerSettingsFromOptions_IfNotProvided()
{
// Arrange
var view = Mock.Of<IView>();
var buffer = new MemoryStream();
var viewComponentContext = GetViewComponentContext(view, buffer);
var result = new JsonViewComponentResult(new { foo = "abcd" });
viewComponentContext.ViewContext.HttpContext.Response.Body = buffer;
// Act
result.Execute(viewComponentContext);
// Assert
Assert.Equal("{\"foo\":\"abcd\"}", Encoding.UTF8.GetString(buffer.ToArray()));
}
private static ViewComponentContext GetViewComponentContext(IView view, Stream stream)
{
var actionContext = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
var actionContext = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var viewContext = new ViewContext(
actionContext,
@ -59,5 +79,15 @@ namespace Microsoft.AspNet.Mvc
var viewComponentContext = new ViewComponentContext(viewComponentDescriptor, new object[0], viewContext, writer);
return viewComponentContext;
}
private static HttpContext GetHttpContext()
{
var httpContext = new DefaultHttpContext();
var services = new ServiceCollection();
services.AddOptions();
httpContext.RequestServices = services.BuildServiceProvider();
return httpContext;
}
}
}