Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
[Fixes #5413] JsonOutputFormatter adds all closing brackets when exce…
Browse files Browse the repository at this point in the history
…ptions are thrown
  • Loading branch information
kichalla committed Mar 28, 2017
1 parent 9c5b33d commit 921e4cb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class JsonResultExecutor
/// <param name="options">The <see cref="IOptions{MvcJsonOptions}"/>.</param>
/// <param name="charPool">The <see cref="ArrayPool{Char}"/> for creating <see cref="T:char[]"/> buffers.</param>
public JsonResultExecutor(
IHttpResponseStreamWriterFactory writerFactory,
ILogger<JsonResultExecutor> logger,
IHttpResponseStreamWriterFactory writerFactory,
ILogger<JsonResultExecutor> logger,
IOptions<MvcJsonOptions> options,
ArrayPool<char> charPool)
{
Expand Down Expand Up @@ -124,6 +124,7 @@ public Task ExecuteAsync(ActionContext context, JsonResult result)
{
jsonWriter.ArrayPool = _charPool;
jsonWriter.CloseOutput = false;
jsonWriter.AutoCompleteOnClose = false;

var jsonSerializer = JsonSerializer.Create(serializerSettings);
jsonSerializer.Serialize(jsonWriter, result.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected virtual JsonWriter CreateJsonWriter(TextWriter writer)
{
ArrayPool = _charPool,
CloseOutput = false,
AutoCompleteOnClose = false
};

return jsonWriter;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.Buffers;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -156,6 +157,25 @@ public async Task ExecuteAsync_UsesPassedInSerializerSettings()
Assert.Equal("application/json; charset=utf-8", context.HttpContext.Response.ContentType);
}

[Fact]
public async Task ExecuteAsync_ErrorDuringSerialization_DoesNotCloseTheBrackets()
{
// Arrange
var expected = Encoding.UTF8.GetBytes("{\"Name\":\"Robert\"");

var context = GetActionContext();

var result = new JsonResult(new ModelWithSerializationError());
var executor = CreateExcutor();

// Act
await executor.ExecuteAsync(context, result);

// Assert
var written = GetWrittenBytes(context.HttpContext);
Assert.Equal(expected, written);
}

private static JsonResultExecutor CreateExcutor()
{
return new JsonResultExecutor(
Expand Down Expand Up @@ -188,5 +208,17 @@ private static byte[] GetWrittenBytes(HttpContext context)
context.Response.Body.Seek(0, SeekOrigin.Begin);
return Assert.IsType<MemoryStream>(context.Response.Body).ToArray();
}

private class ModelWithSerializationError
{
public string Name { get; } = "Robert";
public int Age
{
get
{
throw new NotImplementedException($"Property {Age} has not been implemented");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public static TheoryData<string, string, bool> WriteCorrectCharacterEncoding
{ "This is a test 激光這兩個字是甚麼意思 string written using shift_jis", "shift_jis", false },
#elif NETCOREAPP2_0
#else
#error target frameworks needs to be updated.
#error target frameworks needs to be updated.
#endif
{ "This is a test æøå string written using iso-8859-1", "iso-8859-1", false },
};
Expand All @@ -327,7 +327,7 @@ public static TheoryData<string, string, bool> WriteCorrectCharacterEncoding
}
#elif NETCOREAPP2_0
#else
#error target frameworks needs to be updated.
#error target frameworks needs to be updated.
#endif

return data;
Expand Down Expand Up @@ -369,6 +369,31 @@ public async Task WriteToStreamAsync_UsesCorrectCharacterEncoding(
Assert.Equal(expectedData, actualData);
}

[Fact]
public async Task ErrorDuringSerialization_DoesNotCloseTheBrackets()
{
// Arrange
var expectedOutput = "{\"Name\":\"Robert\"";
var outputFormatterContext = GetOutputFormatterContext(
new ModelWithSerializationError(),
typeof(ModelWithSerializationError));

var serializerSettings = JsonSerializerSettingsProvider.CreateSerializerSettings();
var jsonFormatter = new JsonOutputFormatter(serializerSettings, ArrayPool<char>.Shared);

// Act
await jsonFormatter.WriteResponseBodyAsync(outputFormatterContext, Encoding.UTF8);

// Assert
var body = outputFormatterContext.HttpContext.Response.Body;

Assert.NotNull(body);
body.Position = 0;

var content = new StreamReader(body, Encoding.UTF8).ReadToEnd();
Assert.Equal(expectedOutput, content);
}

private static Encoding CreateOrGetSupportedEncoding(
JsonOutputFormatter formatter,
string encodingAsString,
Expand Down Expand Up @@ -467,5 +492,17 @@ private class UserWithJsonObject

public string FullName { get; set; }
}

private class ModelWithSerializationError
{
public string Name { get; } = "Robert";
public int Age
{
get
{
throw new NotImplementedException($"Property {Age} has not been implemented");
}
}
}
}
}

0 comments on commit 921e4cb

Please sign in to comment.