The view preview section sends a POST request, but REST exports routes only accept GET requests

Created on 8 July 2024, 4 months ago
Updated 2 August 2024, 4 months ago

Problem/Motivation

When trying to preview the results of a rest export in the view edit screen, an error is thrown in the console because the preview could not be retrieved:

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /en/admin/structure/views/view/content_collections_index_rest/preview/organisation_rest_export
StatusText: 500 Service unavailable (with message)
ResponseText: The website encountered an unexpected error. Try again later.Symfony\Component\Routing\Exception\MethodNotAllowedException:  in Drupal\Core\Routing\Router->matchCollection() (line 165 of core/lib/Drupal/Core/Routing/Router.php).

[update]
[update 2] The issue happens on REST exports that use the Facets serializer.

Steps to reproduce

Go to any rest export without any contextual filters and the error appears.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

3.0

Component
Views UIΒ  β†’

Last updated 3 days ago

Created by

πŸ‡·πŸ‡΄Romania rares badita Bucharest

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @rares badita
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    @Rares Badita, thanks for the issue.

    Issues are solve on the main development branch first, 11.x. Also restoring the template so we can keep track of any work done here.

  • πŸ‡·πŸ‡΄Romania rares badita Bucharest

    I've tinkered a bit with the issue, and I think the behaviour used for processing the form when contextual filter are present should be also used when there are no contextual filters.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Given this is a user-facing PHP error (and the preview just doesn't work in this case), bumping to major.

  • πŸ‡·πŸ‡΄Romania rares badita Bucharest

    The issue arises when creating a URL from a request using the Url::createFromRequest($request) method.
    When the Router class tries to match the url of the $request parameter to a route, we reach this code:

    // Check HTTP method requirement.
    if ($requiredMethods = $route->getMethods()) {
      // HEAD and GET are equivalent as per RFC.
      if ('HEAD' === $method = $this->context->getMethod()) {
        $method = 'GET';
      }
    
      if (!in_array($method, $requiredMethods)) {
        $this->allow = array_merge($this->allow, $requiredMethods);
        $routes->remove($name);
        continue;
      }
    }
    

    The problem is that the $method used to check against $requiredMethods is not obtained from the current $request, but from $context, which is the original request from the request stack.

    This leads to an issue with REST exports that use the Facets serializer view style. When building the facets results, FacetsUrlGenerator::getUrlForRequest() wants to create a URL from a request to the actual view display(made with GET), not to the preview form(made with POST), thus resulting in a method mismatch and an exception being thrown.

    I've added a patch for the Facets module to catch this exception.

    I've also changed the issue project from views-ui to facets.

  • First commit to issue fork.
  • Pipeline finished with Failed
    4 months ago
    Total: 646s
    #241818
Production build 0.71.5 2024