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