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 2c053ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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
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 2c053ef

Please sign in to comment.