jsonapi.services.yml | 12 +++++----- src/Routing/EarlyFormatSetter.php | 40 ++++++++++++++++++++++++++++++++ src/StackMiddleware/FormatSetter.php | 44 ------------------------------------ 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/jsonapi.services.yml b/jsonapi.services.yml index fc9904b..87d93ce 100644 --- a/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -150,13 +150,13 @@ services: # This way it can invalidate the data in here based on tags. tags: [{ name: cache.bin }] - # Middleware. - jsonapi.http_middleware.format_setter: - class: Drupal\jsonapi\StackMiddleware\FormatSetter + # Route filter. + jsonapi.route_filter.format_setter: + class: Drupal\jsonapi\Routing\EarlyFormatSetter tags: - # Set priority to 201 so it happens right before the page cache - # middleware (priority 200) has the opportunity to respond. - - { name: http_middleware, priority: 201 } + # Set to a high priority so it runs before content_type_header_matcher + # and other filters that might throw exceptions. + - { name: route_filter, priority: 100 } # Access Control jsonapi.entity_access_checker: diff --git a/src/Routing/EarlyFormatSetter.php b/src/Routing/EarlyFormatSetter.php new file mode 100644 index 0000000..61f714e --- /dev/null +++ b/src/Routing/EarlyFormatSetter.php @@ -0,0 +1,40 @@ +getRequestFormat(NULL))) { + $possible_formats = static::getAvailableFormats($collection); + if ($possible_formats === ['api_json']) { + $request->setRequestFormat('api_json'); + } + } + return $collection; + } + +} diff --git a/src/StackMiddleware/FormatSetter.php b/src/StackMiddleware/FormatSetter.php deleted file mode 100644 index 4896e9e..0000000 --- a/src/StackMiddleware/FormatSetter.php +++ /dev/null @@ -1,44 +0,0 @@ -httpKernel = $http_kernel; - } - - /** - * {@inheritdoc} - */ - public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) { - $accepted = AcceptHeader::fromString($request->headers->get('Accept')); - if ($accepted->get('application/vnd.api+json')) { - $request->setRequestFormat('api_json'); - } - return $this->httpKernel->handle($request, $type, $catch); - } - -}