1
- using System . Diagnostics ;
2
1
using System . Net ;
3
2
using System . Reflection ;
4
3
using JsonApiDotNetCore . Configuration ;
@@ -97,20 +96,56 @@ private static bool IsEndpointAvailable(JsonApiEndpoints endpoint, ResourceType
97
96
98
97
// For an overridden JSON:API action method in a partial class to show up, it's flag must be turned on in [Resource].
99
98
// Otherwise, it is considered to be an action method that throws because the endpoint is unavailable.
100
- return endpoint switch
101
- {
102
- JsonApiEndpoints . GetCollection => availableEndpoints . HasFlag ( JsonApiEndpoints . GetCollection ) ,
103
- JsonApiEndpoints . GetSingle => availableEndpoints . HasFlag ( JsonApiEndpoints . GetSingle ) ,
104
- JsonApiEndpoints . GetSecondary => availableEndpoints . HasFlag ( JsonApiEndpoints . GetSecondary ) ,
105
- JsonApiEndpoints . GetRelationship => availableEndpoints . HasFlag ( JsonApiEndpoints . GetRelationship ) ,
106
- JsonApiEndpoints . Post => availableEndpoints . HasFlag ( JsonApiEndpoints . Post ) ,
107
- JsonApiEndpoints . PostRelationship => availableEndpoints . HasFlag ( JsonApiEndpoints . PostRelationship ) ,
108
- JsonApiEndpoints . Patch => availableEndpoints . HasFlag ( JsonApiEndpoints . Patch ) ,
109
- JsonApiEndpoints . PatchRelationship => availableEndpoints . HasFlag ( JsonApiEndpoints . PatchRelationship ) ,
110
- JsonApiEndpoints . Delete => availableEndpoints . HasFlag ( JsonApiEndpoints . Delete ) ,
111
- JsonApiEndpoints . DeleteRelationship => availableEndpoints . HasFlag ( JsonApiEndpoints . DeleteRelationship ) ,
112
- _ => throw new UnreachableException ( )
113
- } ;
99
+ return IncludesEndpoint ( endpoint , availableEndpoints ) ;
100
+ }
101
+
102
+ private static bool IncludesEndpoint ( JsonApiEndpoints endpoint , JsonApiEndpoints availableEndpoints )
103
+ {
104
+ bool ? isIncluded = null ;
105
+
106
+ if ( endpoint == JsonApiEndpoints . GetCollection )
107
+ {
108
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . GetCollection ) ;
109
+ }
110
+ else if ( endpoint == JsonApiEndpoints . GetSingle )
111
+ {
112
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . GetSingle ) ;
113
+ }
114
+ else if ( endpoint == JsonApiEndpoints . GetSecondary )
115
+ {
116
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . GetSecondary ) ;
117
+ }
118
+ else if ( endpoint == JsonApiEndpoints . GetRelationship )
119
+ {
120
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . GetRelationship ) ;
121
+ }
122
+ else if ( endpoint == JsonApiEndpoints . Post )
123
+ {
124
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . Post ) ;
125
+ }
126
+ else if ( endpoint == JsonApiEndpoints . PostRelationship )
127
+ {
128
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . PostRelationship ) ;
129
+ }
130
+ else if ( endpoint == JsonApiEndpoints . Patch )
131
+ {
132
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . Patch ) ;
133
+ }
134
+ else if ( endpoint == JsonApiEndpoints . PatchRelationship )
135
+ {
136
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . PatchRelationship ) ;
137
+ }
138
+ else if ( endpoint == JsonApiEndpoints . Delete )
139
+ {
140
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . Delete ) ;
141
+ }
142
+ else if ( endpoint == JsonApiEndpoints . DeleteRelationship )
143
+ {
144
+ isIncluded = availableEndpoints . HasFlag ( JsonApiEndpoints . DeleteRelationship ) ;
145
+ }
146
+
147
+ ConsistencyGuard . ThrowIf ( isIncluded == null ) ;
148
+ return isIncluded . Value ;
114
149
}
115
150
116
151
private static JsonApiEndpoints GetGeneratedControllerEndpoints ( ResourceType resourceType )
@@ -158,29 +193,40 @@ private static HttpStatusCode[] GetSuccessStatusCodesForEndpoint(JsonApiEndpoint
158
193
] ;
159
194
}
160
195
161
- return endpoint . Value switch
196
+ HttpStatusCode [ ] ? statusCodes = null ;
197
+
198
+ if ( endpoint . Value is JsonApiEndpoints . GetCollection or JsonApiEndpoints . GetSingle or JsonApiEndpoints . GetSecondary or JsonApiEndpoints . GetRelationship )
162
199
{
163
- JsonApiEndpoints . GetCollection or JsonApiEndpoints . GetSingle or JsonApiEndpoints . GetSecondary or JsonApiEndpoints . GetRelationship =>
200
+ statusCodes =
164
201
[
165
202
HttpStatusCode . OK ,
166
203
HttpStatusCode . NotModified
167
- ] ,
168
- JsonApiEndpoints . Post =>
204
+ ] ;
205
+ }
206
+ else if ( endpoint . Value == JsonApiEndpoints . Post )
207
+ {
208
+ statusCodes =
169
209
[
170
210
HttpStatusCode . Created ,
171
211
HttpStatusCode . NoContent
172
- ] ,
173
- JsonApiEndpoints . Patch =>
212
+ ] ;
213
+ }
214
+ else if ( endpoint . Value == JsonApiEndpoints . Patch )
215
+ {
216
+ statusCodes =
174
217
[
175
218
HttpStatusCode . OK ,
176
219
HttpStatusCode . NoContent
177
- ] ,
178
- JsonApiEndpoints . Delete or JsonApiEndpoints . PostRelationship or JsonApiEndpoints . PatchRelationship or JsonApiEndpoints . DeleteRelationship =>
179
- [
180
- HttpStatusCode . NoContent
181
- ] ,
182
- _ => throw new UnreachableException ( )
183
- } ;
220
+ ] ;
221
+ }
222
+ else if ( endpoint . Value is JsonApiEndpoints . Delete or JsonApiEndpoints . PostRelationship or JsonApiEndpoints . PatchRelationship or
223
+ JsonApiEndpoints . DeleteRelationship )
224
+ {
225
+ statusCodes = [ HttpStatusCode . NoContent ] ;
226
+ }
227
+
228
+ ConsistencyGuard . ThrowIf ( statusCodes == null ) ;
229
+ return statusCodes ;
184
230
}
185
231
186
232
private HttpStatusCode [ ] GetErrorStatusCodesForEndpoint ( JsonApiEndpointWrapper endpoint , ResourceType ? resourceType )
@@ -200,46 +246,58 @@ private HttpStatusCode[] GetErrorStatusCodesForEndpoint(JsonApiEndpointWrapper e
200
246
// Condition doesn't apply to atomic operations, because Forbidden is also used when an operation is not accessible.
201
247
ClientIdGenerationMode clientIdGeneration = resourceType ? . ClientIdGeneration ?? _options . ClientIdGeneration ;
202
248
203
- return endpoint . Value switch
249
+ HttpStatusCode [ ] ? statusCodes = null ;
250
+
251
+ if ( endpoint . Value == JsonApiEndpoints . GetCollection )
252
+ {
253
+ statusCodes = [ HttpStatusCode . BadRequest ] ;
254
+ }
255
+ else if ( endpoint . Value is JsonApiEndpoints . GetSingle or JsonApiEndpoints . GetSecondary or JsonApiEndpoints . GetRelationship )
204
256
{
205
- JsonApiEndpoints . GetCollection => [ HttpStatusCode . BadRequest ] ,
206
- JsonApiEndpoints . GetSingle or JsonApiEndpoints . GetSecondary or JsonApiEndpoints . GetRelationship =>
257
+ statusCodes =
207
258
[
208
259
HttpStatusCode . BadRequest ,
209
260
HttpStatusCode . NotFound
210
- ] ,
211
- JsonApiEndpoints . Post when clientIdGeneration == ClientIdGenerationMode . Forbidden =>
261
+ ] ;
262
+ }
263
+ else if ( endpoint . Value == JsonApiEndpoints . Post && clientIdGeneration == ClientIdGenerationMode . Forbidden )
264
+ {
265
+ statusCodes =
212
266
[
213
267
HttpStatusCode . BadRequest ,
214
268
HttpStatusCode . Forbidden ,
215
269
HttpStatusCode . NotFound ,
216
270
HttpStatusCode . Conflict ,
217
271
HttpStatusCode . UnprocessableEntity
218
- ] ,
219
- JsonApiEndpoints . Post =>
220
- [
221
- HttpStatusCode . BadRequest ,
222
- HttpStatusCode . NotFound ,
223
- HttpStatusCode . Conflict ,
224
- HttpStatusCode . UnprocessableEntity
225
- ] ,
226
- JsonApiEndpoints . Patch =>
272
+ ] ;
273
+ }
274
+ else if ( endpoint . Value is JsonApiEndpoints . Post or JsonApiEndpoints . Patch )
275
+ {
276
+ statusCodes =
227
277
[
228
278
HttpStatusCode . BadRequest ,
229
279
HttpStatusCode . NotFound ,
230
280
HttpStatusCode . Conflict ,
231
281
HttpStatusCode . UnprocessableEntity
232
- ] ,
233
- JsonApiEndpoints . Delete => [ HttpStatusCode . NotFound ] ,
234
- JsonApiEndpoints . PostRelationship or JsonApiEndpoints . PatchRelationship or JsonApiEndpoints . DeleteRelationship =>
282
+ ] ;
283
+ }
284
+ else if ( endpoint . Value == JsonApiEndpoints . Delete )
285
+ {
286
+ statusCodes = [ HttpStatusCode . NotFound ] ;
287
+ }
288
+ else if ( endpoint . Value is JsonApiEndpoints . PostRelationship or JsonApiEndpoints . PatchRelationship or JsonApiEndpoints . DeleteRelationship )
289
+ {
290
+ statusCodes =
235
291
[
236
292
HttpStatusCode . BadRequest ,
237
293
HttpStatusCode . NotFound ,
238
294
HttpStatusCode . Conflict ,
239
295
HttpStatusCode . UnprocessableEntity
240
- ] ,
241
- _ => throw new UnreachableException ( )
242
- } ;
296
+ ] ;
297
+ }
298
+
299
+ ConsistencyGuard . ThrowIf ( statusCodes == null ) ;
300
+ return statusCodes ;
243
301
}
244
302
245
303
private void SetRequestMetadata ( ActionModel action , JsonApiEndpointWrapper endpoint )
0 commit comments