2121
2222use Illuminate \Contracts \Validation \Validator ;
2323use Illuminate \Database \Eloquent \Model ;
24- use Illuminate \Http \Response ;
2524use LaravelJsonApi \Contracts \Auth \Authorizer ;
2625use LaravelJsonApi \Contracts \Query \QueryParameters ;
2726use LaravelJsonApi \Core \Exceptions \JsonApiException ;
2827use LaravelJsonApi \Core \Query \FieldSets ;
2928use LaravelJsonApi \Core \Query \FilterParameters ;
3029use LaravelJsonApi \Core \Query \IncludePaths ;
3130use LaravelJsonApi \Core \Query \SortFields ;
31+ use LaravelJsonApi \Laravel \Exceptions \HttpNotAcceptableException ;
3232use LogicException ;
33- use Symfony \Component \HttpKernel \Exception \HttpException ;
3433use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
3534use function array_key_exists ;
3635
@@ -48,11 +47,11 @@ class ResourceQuery extends FormRequest implements QueryParameters
4847 private static $ queryOneResolver ;
4948
5049 /**
50+ * The media types the resource accepts, in addition to JSON:API.
51+ *
5152 * @var string[]
5253 */
53- protected array $ mediaTypes = [
54- self ::JSON_API_MEDIA_TYPE ,
55- ];
54+ protected array $ mediaTypes = [];
5655
5756 /**
5857 * The include paths to use if the client provides none.
@@ -304,10 +303,24 @@ protected function failedValidation(Validator $validator)
304303 */
305304 protected function isAcceptableMediaType (): bool
306305 {
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+ */
307318 return $ this ->accepts ($ this ->mediaTypes ());
308319 }
309320
310321 /**
322+ * Get the media types the resource accepts, in addition to JSON:API.
323+ *
311324 * @return string[]
312325 */
313326 protected function mediaTypes (): array
@@ -322,10 +335,6 @@ protected function mediaTypes(): array
322335 */
323336 protected function notAcceptable (): HttpExceptionInterface
324337 {
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 ();
330339 }
331340}
0 commit comments