21
21
22
22
use Illuminate \Contracts \Validation \Validator ;
23
23
use Illuminate \Database \Eloquent \Model ;
24
- use Illuminate \Http \Response ;
25
24
use LaravelJsonApi \Contracts \Auth \Authorizer ;
26
25
use LaravelJsonApi \Contracts \Query \QueryParameters ;
27
26
use LaravelJsonApi \Core \Exceptions \JsonApiException ;
28
27
use LaravelJsonApi \Core \Query \FieldSets ;
29
28
use LaravelJsonApi \Core \Query \FilterParameters ;
30
29
use LaravelJsonApi \Core \Query \IncludePaths ;
31
30
use LaravelJsonApi \Core \Query \SortFields ;
31
+ use LaravelJsonApi \Laravel \Exceptions \HttpNotAcceptableException ;
32
32
use LogicException ;
33
- use Symfony \Component \HttpKernel \Exception \HttpException ;
34
33
use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
35
34
use function array_key_exists ;
36
35
@@ -48,11 +47,11 @@ class ResourceQuery extends FormRequest implements QueryParameters
48
47
private static $ queryOneResolver ;
49
48
50
49
/**
50
+ * The media types the resource accepts, in addition to JSON:API.
51
+ *
51
52
* @var string[]
52
53
*/
53
- protected array $ mediaTypes = [
54
- self ::JSON_API_MEDIA_TYPE ,
55
- ];
54
+ protected array $ mediaTypes = [];
56
55
57
56
/**
58
57
* The include paths to use if the client provides none.
@@ -304,10 +303,24 @@ protected function failedValidation(Validator $validator)
304
303
*/
305
304
protected function isAcceptableMediaType (): bool
306
305
{
306
+ /**
307
+ * We expect the JSON:API media type to exactly match.
308
+ */
309
+ foreach ($ this ->getAcceptableContentTypes () as $ contentType ) {
310
+ if (self ::JSON_API_MEDIA_TYPE === $ contentType ) {
311
+ return true ;
312
+ }
313
+ }
314
+
315
+ /**
316
+ * Otherwise we check if any additional media types match.
317
+ */
307
318
return $ this ->accepts ($ this ->mediaTypes ());
308
319
}
309
320
310
321
/**
322
+ * Get the media types the resource accepts, in addition to JSON:API.
323
+ *
311
324
* @return string[]
312
325
*/
313
326
protected function mediaTypes (): array
@@ -322,10 +335,6 @@ protected function mediaTypes(): array
322
335
*/
323
336
protected function notAcceptable (): HttpExceptionInterface
324
337
{
325
- return new HttpException (
326
- Response::HTTP_NOT_ACCEPTABLE ,
327
- __ ("The requested resource is capable of generating only content not acceptable "
328
- . "according to the Accept headers sent in the request. " )
329
- );
338
+ return new HttpNotAcceptableException ();
330
339
}
331
340
}
0 commit comments