44using JsonApiDotNetCore . Configuration ;
55using JsonApiDotNetCore . Diagnostics ;
66using JsonApiDotNetCore . Resources . Annotations ;
7- using JsonApiDotNetCore . Serialization ;
87using JsonApiDotNetCore . Serialization . Objects ;
98using Microsoft . AspNetCore . Http ;
109using Microsoft . AspNetCore . Http . Extensions ;
@@ -45,7 +44,7 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
4544
4645 using ( CodeTimingSessionManager . Current . Measure ( "JSON:API middleware" ) )
4746 {
48- if ( ! await ValidateIfMatchHeaderAsync ( httpContext , options . SerializationWriteContext ) )
47+ if ( ! await ValidateIfMatchHeaderAsync ( httpContext , options . SerializerWriteOptions ) )
4948 {
5049 return ;
5150 }
@@ -55,8 +54,8 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
5554
5655 if ( primaryResourceType != null )
5756 {
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 ) )
6059 {
6160 return ;
6261 }
@@ -67,8 +66,8 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
6766 }
6867 else if ( IsRouteForOperations ( routeValues ) )
6968 {
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 ) )
7271 {
7372 return ;
7473 }
@@ -92,11 +91,11 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
9291 }
9392 }
9493
95- private async Task < bool > ValidateIfMatchHeaderAsync ( HttpContext httpContext , JsonApiSerializationContext serializationContext )
94+ private async Task < bool > ValidateIfMatchHeaderAsync ( HttpContext httpContext , JsonSerializerOptions serializerOptions )
9695 {
9796 if ( httpContext . Request . Headers . ContainsKey ( HeaderNames . IfMatch ) )
9897 {
99- await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . PreconditionFailed )
98+ await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . PreconditionFailed )
10099 {
101100 Title = "Detection of mid-air edit collisions using ETags is not supported." ,
102101 Source = new ErrorSource
@@ -121,14 +120,13 @@ private async Task<bool> ValidateIfMatchHeaderAsync(HttpContext httpContext, Jso
121120 : null ;
122121 }
123122
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 )
126124 {
127125 string ? contentType = httpContext . Request . ContentType ;
128126
129127 if ( contentType != null && contentType != allowedContentType )
130128 {
131- await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . UnsupportedMediaType )
129+ await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . UnsupportedMediaType )
132130 {
133131 Title = "The specified Content-Type header value is not supported." ,
134132 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
145143 }
146144
147145 private static async Task < bool > ValidateAcceptHeaderAsync ( MediaTypeHeaderValue allowedMediaTypeValue , HttpContext httpContext ,
148- JsonApiSerializationContext serializationContext )
146+ JsonSerializerOptions serializerOptions )
149147 {
150148 string [ ] acceptHeaders = httpContext . Request . Headers . GetCommaSeparatedValues ( "Accept" ) ;
151149
@@ -178,7 +176,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(MediaTypeHeaderValue a
178176
179177 if ( ! seenCompatibleMediaType )
180178 {
181- await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . NotAcceptable )
179+ await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . NotAcceptable )
182180 {
183181 Title = "The specified Accept header value does not contain any supported media types." ,
184182 Detail = $ "Please include '{ allowedMediaTypeValue } ' in the Accept header values.",
@@ -194,7 +192,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(MediaTypeHeaderValue a
194192 return true ;
195193 }
196194
197- private static async Task FlushResponseAsync ( HttpResponse httpResponse , JsonApiSerializationContext serializationContext , ErrorObject error )
195+ private static async Task FlushResponseAsync ( HttpResponse httpResponse , JsonSerializerOptions serializerOptions , ErrorObject error )
198196 {
199197 httpResponse . ContentType = HeaderConstants . MediaType ;
200198 httpResponse . StatusCode = ( int ) error . StatusCode ;
@@ -204,7 +202,7 @@ private static async Task FlushResponseAsync(HttpResponse httpResponse, JsonApiS
204202 Errors = error . AsList ( )
205203 } ;
206204
207- await JsonSerializer . SerializeAsync ( httpResponse . Body , errorDocument , serializationContext . Document ) ;
205+ await JsonSerializer . SerializeAsync ( httpResponse . Body , errorDocument , serializerOptions ) ;
208206 await httpResponse . Body . FlushAsync ( ) ;
209207 }
210208
0 commit comments