Skip to content

fix(JsonApiReader): do not write to the response body stream #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/JsonApiDotNetCore/Formatters/JsonApiReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Serialization;
Expand All @@ -16,7 +15,7 @@ public class JsonApiReader : IJsonApiReader
private readonly IJsonApiDeSerializer _deSerializer;
private readonly IJsonApiContext _jsonApiContext;
private readonly ILogger<JsonApiReader> _logger;


public JsonApiReader(IJsonApiDeSerializer deSerializer, IJsonApiContext jsonApiContext, ILoggerFactory loggerFactory)
{
Expand All @@ -37,26 +36,25 @@ public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
try
{
var body = GetRequestBody(context.HttpContext.Request.Body);
var model = _jsonApiContext.IsRelationshipPath ?
var model = _jsonApiContext.IsRelationshipPath ?
_deSerializer.DeserializeRelationship(body) :
_deSerializer.Deserialize(body);

if(model == null)
if (model == null)
_logger?.LogError("An error occurred while de-serializing the payload");

return InputFormatterResult.SuccessAsync(model);
}
catch (JsonSerializationException ex)
{
_logger?.LogError(new EventId(), ex, "An error occurred while de-serializing the payload");
context.HttpContext.Response.StatusCode = 422;
context.ModelState.AddModelError(context.ModelName, ex, context.Metadata);
return InputFormatterResult.FailureAsync();
}
catch(JsonApiException jex)
catch (JsonApiException jex)
{
_logger?.LogError(new EventId(), jex, "An error occurred while de-serializing the payload");
context.HttpContext.Response.StatusCode = jex.GetStatusCode();
context.HttpContext.Response.Body = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jex.GetError())));
context.ModelState.AddModelError(context.ModelName, jex, context.Metadata);
return InputFormatterResult.FailureAsync();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionPrefix>2.1.1</VersionPrefix>
<TargetFrameworks>netstandard1.6</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down