1
1
using System ;
2
2
using System . IO ;
3
- using System . Text ;
4
3
using System . Threading . Tasks ;
5
4
using JsonApiDotNetCore . Internal ;
6
5
using JsonApiDotNetCore . Serialization ;
@@ -16,7 +15,7 @@ public class JsonApiReader : IJsonApiReader
16
15
private readonly IJsonApiDeSerializer _deSerializer ;
17
16
private readonly IJsonApiContext _jsonApiContext ;
18
17
private readonly ILogger < JsonApiReader > _logger ;
19
-
18
+
20
19
21
20
public JsonApiReader ( IJsonApiDeSerializer deSerializer , IJsonApiContext jsonApiContext , ILoggerFactory loggerFactory )
22
21
{
@@ -37,26 +36,25 @@ public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
37
36
try
38
37
{
39
38
var body = GetRequestBody ( context . HttpContext . Request . Body ) ;
40
- var model = _jsonApiContext . IsRelationshipPath ?
39
+ var model = _jsonApiContext . IsRelationshipPath ?
41
40
_deSerializer . DeserializeRelationship ( body ) :
42
41
_deSerializer . Deserialize ( body ) ;
43
42
44
- if ( model == null )
43
+ if ( model == null )
45
44
_logger ? . LogError ( "An error occurred while de-serializing the payload" ) ;
46
45
47
46
return InputFormatterResult . SuccessAsync ( model ) ;
48
47
}
49
48
catch ( JsonSerializationException ex )
50
49
{
51
50
_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 ) ;
53
52
return InputFormatterResult . FailureAsync ( ) ;
54
53
}
55
- catch ( JsonApiException jex )
54
+ catch ( JsonApiException jex )
56
55
{
57
56
_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 ) ;
60
58
return InputFormatterResult . FailureAsync ( ) ;
61
59
}
62
60
}
0 commit comments