Page MenuHomePhabricator

Create a generic redirect handler for the REST API
Closed, ResolvedPublic

Description

Rationale:
In the light of T359652 it would be useful to have an easy way to declare routes that will redirect to other routes, for convenience and deprecation.

Requirements (step 1):

  • When the redirect handler is execute it triggers a redirect with status 308 to the specified target.
  • The redirect handler takes the redirect target path from the route file.
  • The redirect target is specified by a target key in an object that is located under the redirect key in the route declaration.
  • The REST root path is applied to the target path when generating the Location header in the response.
  • The Location header should contain a relative URL with a full path (that is, the path starting with "/", but no domain or protocol)
{
    "path": "/v1/some/path",
    "class": "MediaWiki\\Rest\\Handler\\RedirectHandler",
    "redirect": {
        "target": "/v1/other/path"
    }
}

Requirements (step 2):

  • Parameters are preserved when generating redirects. This includes path and query parameters. Body parameters are handled by the client.
{
    "path": "/v1/some/path/{param}",
    "class": "MediaWiki\\Rest\\Handler\\RedirectHandler",
    "redirect": {
        "target": "/v1/other/path/{param}"
    }
}

Requirements (step 3, nice to have):

  • It should not be necessary to specify the class explicitly:
{
    "path": "/v1/some/path/{param}",
    "redirect": {
        "target": "/v1/other/path/{param}"
    }
}

Implementation hints:

  • The redirect handler should be MediaWiki\\Rest\\Handler\\RedirectHandler
  • In the hadnler's code, the "redirect" structure from the path specification can be accessed using $redirect = $this->getConfig()['redirect'].
  • The full target path, including the handler root, can be obtained from $locationPath = $this->router->getRoutePath( $redirect['target'] )
  • A redirect response can be generated using $this->responseFactory->createTemporaryRedirect( $locationPath ). The resulting Response object can be returned by the handler's execute() method.

Event Timeline

This might be obvious but just noting it here so it's part of these notes "Path parameters are preserved when generating redirects." includes both param value and order i.e

{
    "path": "/v1/some/path/{param1}/{nextParam}",
    "class": "MediaWiki\\Rest\\Handler\\RedirectHandler",
    "redirect": {
        "target": "/v1/other/path/{param1}/{nextParam}"
    }
}

This might be obvious but just noting it here so it's part of these notes "Path parameters are preserved when generating redirects." includes both param value and order i.e

Ther parameters are named, so they should not be assigned by order. The order may actually be changed. E.g.:

{
    "path": "/v1/some/path/{foo}/{bar}",
    "class": "MediaWiki\\Rest\\Handler\\RedirectHandler",
    "redirect": {
        "target": "/v1/other/path/{bar}/{foo}"
    }
}
daniel renamed this task from Greate a generic redirect handler for the REST API to Create a generic redirect handler for the REST API.Apr 4 2024, 8:02 PM

Change #1017454 had a related patch set uploaded (by Atieno; author: Atieno):

[mediawiki/core@master] handler: Create generic redirect handler for the REST API

https://gerrit.wikimedia.org/r/1017454

Change #1017870 had a related patch set uploaded (by WQuarshie; author: WQuarshie):

[mediawiki/core@master] rest: test for path redirection

https://gerrit.wikimedia.org/r/1017870

Change #1020696 had a related patch set uploaded (by Daniel Kinzler; author: Daniel Kinzler):

[mediawiki/core@master] REST: allow redirects to be defined without specifying a handler

https://gerrit.wikimedia.org/r/1020696

Change #1017454 merged by jenkins-bot:

[mediawiki/core@master] handler: Create generic redirect handler for the REST API

https://gerrit.wikimedia.org/r/1017454

Change #1017870 merged by jenkins-bot:

[mediawiki/core@master] rest: test for path redirection

https://gerrit.wikimedia.org/r/1017870

Change #1020696 merged by jenkins-bot:

[mediawiki/core@master] REST: allow redirects to be defined without specifying a handler

https://gerrit.wikimedia.org/r/1020696

Change #1021512 had a related patch set uploaded (by Daniel Kinzler; author: Daniel Kinzler):

[mediawiki/core@master] REST: Document new redirect feature.

https://gerrit.wikimedia.org/r/1021512

Change #1021512 merged by jenkins-bot:

[mediawiki/core@master] REST: Document new redirect feature.

https://gerrit.wikimedia.org/r/1021512