11using System ;
22using System . IO ;
3- using System . Text ;
43using System . Threading . Tasks ;
54using JsonApiDotNetCore . Internal ;
65using JsonApiDotNetCore . Serialization ;
@@ -16,7 +15,7 @@ public class JsonApiReader : IJsonApiReader
1615 private readonly IJsonApiDeSerializer _deSerializer ;
1716 private readonly IJsonApiContext _jsonApiContext ;
1817 private readonly ILogger < JsonApiReader > _logger ;
19-
18+
2019
2120 public JsonApiReader ( IJsonApiDeSerializer deSerializer , IJsonApiContext jsonApiContext , ILoggerFactory loggerFactory )
2221 {
@@ -37,26 +36,25 @@ public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
3736 try
3837 {
3938 var body = GetRequestBody ( context . HttpContext . Request . Body ) ;
40- var model = _jsonApiContext . IsRelationshipPath ?
39+ var model = _jsonApiContext . IsRelationshipPath ?
4140 _deSerializer . DeserializeRelationship ( body ) :
4241 _deSerializer . Deserialize ( body ) ;
4342
44- if ( model == null )
43+ if ( model == null )
4544 _logger ? . LogError ( "An error occurred while de-serializing the payload" ) ;
4645
4746 return InputFormatterResult . SuccessAsync ( model ) ;
4847 }
4948 catch ( JsonSerializationException ex )
5049 {
5150 _logger ? . LogError ( new EventId ( ) , ex , "An error occurred while de-serializing the payload" ) ;
52- context . HttpContext . Response . StatusCode = 422 ;
51+ context . ModelState . AddModelError ( context . ModelName , ex , context . Metadata ) ;
5352 return InputFormatterResult . FailureAsync ( ) ;
5453 }
55- catch ( JsonApiException jex )
54+ catch ( JsonApiException jex )
5655 {
5756 _logger ? . LogError ( new EventId ( ) , jex , "An error occurred while de-serializing the payload" ) ;
58- context . HttpContext . Response . StatusCode = jex . GetStatusCode ( ) ;
59- context . HttpContext . Response . Body = new MemoryStream ( Encoding . UTF8 . GetBytes ( JsonConvert . SerializeObject ( jex . GetError ( ) ) ) ) ;
57+ context . ModelState . AddModelError ( context . ModelName , jex , context . Metadata ) ;
6058 return InputFormatterResult . FailureAsync ( ) ;
6159 }
6260 }
0 commit comments