4
4
using JsonApiDotNetCore . Configuration ;
5
5
using JsonApiDotNetCore . Diagnostics ;
6
6
using JsonApiDotNetCore . Resources . Annotations ;
7
- using JsonApiDotNetCore . Serialization ;
8
7
using JsonApiDotNetCore . Serialization . Objects ;
9
8
using Microsoft . AspNetCore . Http ;
10
9
using Microsoft . AspNetCore . Http . Extensions ;
@@ -45,7 +44,7 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
45
44
46
45
using ( CodeTimingSessionManager . Current . Measure ( "JSON:API middleware" ) )
47
46
{
48
- if ( ! await ValidateIfMatchHeaderAsync ( httpContext , options . SerializationWriteContext ) )
47
+ if ( ! await ValidateIfMatchHeaderAsync ( httpContext , options . SerializerWriteOptions ) )
49
48
{
50
49
return ;
51
50
}
@@ -55,8 +54,8 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
55
54
56
55
if ( primaryResourceType != null )
57
56
{
58
- if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . MediaType , httpContext , options . SerializationWriteContext ) ||
59
- ! await ValidateAcceptHeaderAsync ( MediaType , httpContext , options . SerializationWriteContext ) )
57
+ if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . MediaType , httpContext , options . SerializerWriteOptions ) ||
58
+ ! await ValidateAcceptHeaderAsync ( MediaType , httpContext , options . SerializerWriteOptions ) )
60
59
{
61
60
return ;
62
61
}
@@ -67,8 +66,8 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
67
66
}
68
67
else if ( IsRouteForOperations ( routeValues ) )
69
68
{
70
- if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . AtomicOperationsMediaType , httpContext , options . SerializationWriteContext ) ||
71
- ! await ValidateAcceptHeaderAsync ( AtomicOperationsMediaType , httpContext , options . SerializationWriteContext ) )
69
+ if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . AtomicOperationsMediaType , httpContext , options . SerializerWriteOptions ) ||
70
+ ! await ValidateAcceptHeaderAsync ( AtomicOperationsMediaType , httpContext , options . SerializerWriteOptions ) )
72
71
{
73
72
return ;
74
73
}
@@ -92,11 +91,11 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
92
91
}
93
92
}
94
93
95
- private async Task < bool > ValidateIfMatchHeaderAsync ( HttpContext httpContext , JsonApiSerializationContext serializationContext )
94
+ private async Task < bool > ValidateIfMatchHeaderAsync ( HttpContext httpContext , JsonSerializerOptions serializerOptions )
96
95
{
97
96
if ( httpContext . Request . Headers . ContainsKey ( HeaderNames . IfMatch ) )
98
97
{
99
- await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . PreconditionFailed )
98
+ await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . PreconditionFailed )
100
99
{
101
100
Title = "Detection of mid-air edit collisions using ETags is not supported." ,
102
101
Source = new ErrorSource
@@ -121,14 +120,13 @@ private async Task<bool> ValidateIfMatchHeaderAsync(HttpContext httpContext, Jso
121
120
: null ;
122
121
}
123
122
124
- private static async Task < bool > ValidateContentTypeHeaderAsync ( string allowedContentType , HttpContext httpContext ,
125
- JsonApiSerializationContext serializationContext )
123
+ private static async Task < bool > ValidateContentTypeHeaderAsync ( string allowedContentType , HttpContext httpContext , JsonSerializerOptions serializerOptions )
126
124
{
127
125
string ? contentType = httpContext . Request . ContentType ;
128
126
129
127
if ( contentType != null && contentType != allowedContentType )
130
128
{
131
- await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . UnsupportedMediaType )
129
+ await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . UnsupportedMediaType )
132
130
{
133
131
Title = "The specified Content-Type header value is not supported." ,
134
132
Detail = $ "Please specify '{ allowedContentType } ' instead of '{ contentType } ' for the Content-Type header value.",
@@ -145,7 +143,7 @@ private static async Task<bool> ValidateContentTypeHeaderAsync(string allowedCon
145
143
}
146
144
147
145
private static async Task < bool > ValidateAcceptHeaderAsync ( MediaTypeHeaderValue allowedMediaTypeValue , HttpContext httpContext ,
148
- JsonApiSerializationContext serializationContext )
146
+ JsonSerializerOptions serializerOptions )
149
147
{
150
148
string [ ] acceptHeaders = httpContext . Request . Headers . GetCommaSeparatedValues ( "Accept" ) ;
151
149
@@ -178,7 +176,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(MediaTypeHeaderValue a
178
176
179
177
if ( ! seenCompatibleMediaType )
180
178
{
181
- await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . NotAcceptable )
179
+ await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . NotAcceptable )
182
180
{
183
181
Title = "The specified Accept header value does not contain any supported media types." ,
184
182
Detail = $ "Please include '{ allowedMediaTypeValue } ' in the Accept header values.",
@@ -194,7 +192,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(MediaTypeHeaderValue a
194
192
return true ;
195
193
}
196
194
197
- private static async Task FlushResponseAsync ( HttpResponse httpResponse , JsonApiSerializationContext serializationContext , ErrorObject error )
195
+ private static async Task FlushResponseAsync ( HttpResponse httpResponse , JsonSerializerOptions serializerOptions , ErrorObject error )
198
196
{
199
197
httpResponse . ContentType = HeaderConstants . MediaType ;
200
198
httpResponse . StatusCode = ( int ) error . StatusCode ;
@@ -204,7 +202,7 @@ private static async Task FlushResponseAsync(HttpResponse httpResponse, JsonApiS
204
202
Errors = error . AsList ( )
205
203
} ;
206
204
207
- await JsonSerializer . SerializeAsync ( httpResponse . Body , errorDocument , serializationContext . Document ) ;
205
+ await JsonSerializer . SerializeAsync ( httpResponse . Body , errorDocument , serializerOptions ) ;
208
206
await httpResponse . Body . FlushAsync ( ) ;
209
207
}
210
208
0 commit comments