44using JsonApiDotNetCore . Configuration ;
55using JsonApiDotNetCore . Diagnostics ;
66using JsonApiDotNetCore . Resources . Annotations ;
7+ using JsonApiDotNetCore . Serialization ;
78using JsonApiDotNetCore . Serialization . Objects ;
89using Microsoft . AspNetCore . Http ;
910using Microsoft . AspNetCore . Http . Extensions ;
@@ -44,7 +45,7 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
4445
4546 using ( CodeTimingSessionManager . Current . Measure ( "JSON:API middleware" ) )
4647 {
47- if ( ! await ValidateIfMatchHeaderAsync ( httpContext , options . SerializerWriteOptions ) )
48+ if ( ! await ValidateIfMatchHeaderAsync ( httpContext , options . SerializationWriteContext ) )
4849 {
4950 return ;
5051 }
@@ -54,8 +55,8 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
5455
5556 if ( primaryResourceType != null )
5657 {
57- if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . MediaType , httpContext , options . SerializerWriteOptions ) ||
58- ! await ValidateAcceptHeaderAsync ( MediaType , httpContext , options . SerializerWriteOptions ) )
58+ if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . MediaType , httpContext , options . SerializationWriteContext ) ||
59+ ! await ValidateAcceptHeaderAsync ( MediaType , httpContext , options . SerializationWriteContext ) )
5960 {
6061 return ;
6162 }
@@ -66,8 +67,8 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
6667 }
6768 else if ( IsRouteForOperations ( routeValues ) )
6869 {
69- if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . AtomicOperationsMediaType , httpContext , options . SerializerWriteOptions ) ||
70- ! await ValidateAcceptHeaderAsync ( AtomicOperationsMediaType , httpContext , options . SerializerWriteOptions ) )
70+ if ( ! await ValidateContentTypeHeaderAsync ( HeaderConstants . AtomicOperationsMediaType , httpContext , options . SerializationWriteContext ) ||
71+ ! await ValidateAcceptHeaderAsync ( AtomicOperationsMediaType , httpContext , options . SerializationWriteContext ) )
7172 {
7273 return ;
7374 }
@@ -91,11 +92,11 @@ public async Task InvokeAsync(HttpContext httpContext, IControllerResourceMappin
9192 }
9293 }
9394
94- private async Task < bool > ValidateIfMatchHeaderAsync ( HttpContext httpContext , JsonSerializerOptions serializerOptions )
95+ private async Task < bool > ValidateIfMatchHeaderAsync ( HttpContext httpContext , JsonApiSerializationContext serializationContext )
9596 {
9697 if ( httpContext . Request . Headers . ContainsKey ( HeaderNames . IfMatch ) )
9798 {
98- await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . PreconditionFailed )
99+ await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . PreconditionFailed )
99100 {
100101 Title = "Detection of mid-air edit collisions using ETags is not supported." ,
101102 Source = new ErrorSource
@@ -120,13 +121,14 @@ private async Task<bool> ValidateIfMatchHeaderAsync(HttpContext httpContext, Jso
120121 : null ;
121122 }
122123
123- private static async Task < bool > ValidateContentTypeHeaderAsync ( string allowedContentType , HttpContext httpContext , JsonSerializerOptions serializerOptions )
124+ private static async Task < bool > ValidateContentTypeHeaderAsync ( string allowedContentType , HttpContext httpContext ,
125+ JsonApiSerializationContext serializationContext )
124126 {
125127 string ? contentType = httpContext . Request . ContentType ;
126128
127129 if ( contentType != null && contentType != allowedContentType )
128130 {
129- await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . UnsupportedMediaType )
131+ await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . UnsupportedMediaType )
130132 {
131133 Title = "The specified Content-Type header value is not supported." ,
132134 Detail = $ "Please specify '{ allowedContentType } ' instead of '{ contentType } ' for the Content-Type header value.",
@@ -143,7 +145,7 @@ private static async Task<bool> ValidateContentTypeHeaderAsync(string allowedCon
143145 }
144146
145147 private static async Task < bool > ValidateAcceptHeaderAsync ( MediaTypeHeaderValue allowedMediaTypeValue , HttpContext httpContext ,
146- JsonSerializerOptions serializerOptions )
148+ JsonApiSerializationContext serializationContext )
147149 {
148150 string [ ] acceptHeaders = httpContext . Request . Headers . GetCommaSeparatedValues ( "Accept" ) ;
149151
@@ -176,7 +178,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(MediaTypeHeaderValue a
176178
177179 if ( ! seenCompatibleMediaType )
178180 {
179- await FlushResponseAsync ( httpContext . Response , serializerOptions , new ErrorObject ( HttpStatusCode . NotAcceptable )
181+ await FlushResponseAsync ( httpContext . Response , serializationContext , new ErrorObject ( HttpStatusCode . NotAcceptable )
180182 {
181183 Title = "The specified Accept header value does not contain any supported media types." ,
182184 Detail = $ "Please include '{ allowedMediaTypeValue } ' in the Accept header values.",
@@ -192,7 +194,7 @@ private static async Task<bool> ValidateAcceptHeaderAsync(MediaTypeHeaderValue a
192194 return true ;
193195 }
194196
195- private static async Task FlushResponseAsync ( HttpResponse httpResponse , JsonSerializerOptions serializerOptions , ErrorObject error )
197+ private static async Task FlushResponseAsync ( HttpResponse httpResponse , JsonApiSerializationContext serializationContext , ErrorObject error )
196198 {
197199 httpResponse . ContentType = HeaderConstants . MediaType ;
198200 httpResponse . StatusCode = ( int ) error . StatusCode ;
@@ -202,7 +204,7 @@ private static async Task FlushResponseAsync(HttpResponse httpResponse, JsonSeri
202204 Errors = error . AsList ( )
203205 } ;
204206
205- await JsonSerializer . SerializeAsync ( httpResponse . Body , errorDocument , serializerOptions ) ;
207+ await JsonSerializer . SerializeAsync ( httpResponse . Body , errorDocument , serializationContext . Document ) ;
206208 await httpResponse . Body . FlushAsync ( ) ;
207209 }
208210
0 commit comments