Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers created an issue. See original summary.

Wim Leers’s picture

Title: Why 'api' as the path prefix? Why not 'jsonapi'? » Remove configurable 'prefix'. Also: why 'api' as the path prefix? Why not 'jsonapi'?
Parent issue: » #2829398: Clean up JsonApiResource: the annotation, plugin type, plugin manager, plugin implementations, the dynamic routes generator and the request handler

Repurposing this for removing the prefix configuration.

Wim Leers’s picture

Status: Active » Needs review
FileSize
13.08 KB

This just removes the configurable 'prefix'.

Status: Needs review » Needs work

The last submitted patch, 3: prefix_2838580-3.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
12.36 KB
6.61 KB

Hah and this one failed because it builds on top of #2838630: Remove the concept of "enabledness" from the JSON API module, rely on the Entity Access API instead. Rebasing on HEAD instead. That means those patches will conflict.

Wim Leers’s picture

FileSize
16.08 KB
7.86 KB

And this reroll now also changes the non-configurable prefix from /api to /jsonapi.

Wim Leers’s picture

Title: Remove configurable 'prefix'. Also: why 'api' as the path prefix? Why not 'jsonapi'? » Remove configurable 'prefix' + change the non-configurable prefix from '/api' to '/jsonapi'

Updating issue title to reflect scope of #6. If you don't want to rename this path, use the patch in #5. But if we're ever going to rename this, it's now.

Status: Needs review » Needs work

The last submitted patch, 6: prefix_2838580-6.patch, failed testing.

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
23.18 KB
7.81 KB

Missed one spot for #6.

Wim Leers’s picture

Category: Feature request » Task
Priority: Normal » Major
Issue tags: +blocker, +maintainability

This is blocking #2831134: Remove configurable 'id_field': always use UUID, because they will conflict and this one is ready, the other one is not.

Wim Leers’s picture

@e0ipso: you can choose between #5 (only remove configuration) and #9 (also change URL/path).

GrandmaGlassesRopeMan’s picture

@Wim Leers

Thanks for brining this up. This change feels necessary to make the api more accessible to outside/first time users. I am +1 on changing the prefix to jsonapi from api. I would however prefer /{entity type}/{bundle}/{uuid} with an Accept header, but I understand that it may not be currently feasible due to a bunch of outside issues.

killua99’s picture

I like the idea to change the default prefix to /jsonapi rather /api. But I still feel the need to keep it as a config value, to be able to change it if someone want to use something more custom / project related. The this will come back at core when the module hit Experimental State.

hampercm’s picture

+1 for changing the prefix from 'api' to 'jsonapi' And yes, having the "Accept" header work would be wonderful.

GrandmaGlassesRopeMan’s picture

@killua99

I'm not totally opposed to having this as a config value. However keeping it fixed will provide a more consistent experience for users of a JSON-API based API across a number of different Drupal sites.

hampercm’s picture

@killua99 @drpal I, too, like the idea of removing the option to change the prefix. Using the /jsonapi URL prefix follows Drupal best-practices of using the module's name as the prefix for its custom routes, so collisions will hopefully be minimal. And, if someone really needs to change the route prefix on a particular site, it is possible to alter existing routes.

Wim Leers’s picture

@killua99: if you want to change the prefix: use a \Drupal\Core\Routing\RoutingEvents::ALTER event subscriber! :)

hampercm’s picture

Assigned: Unassigned » hampercm
Status: Needs review » Reviewed & tested by the community

I got the OK from e0ipso on IRC to move ahead with #9: removing config and changing the URL prefix to /jsonapi.

This looks very good; the hard-coded prefix makes route names much cleaner! Two small issues found in review:
1. The README.md file still refers to URLs with the /api prefix
2. jsonapi/tests/src/Unit/Context/CurrentContextTest.php:71 uses a route for '/api/articles' in its tests. Not sure fi this makes any difference, but may want to change that to use something like /jsonapi/node/article.

Once those are fixed, I think this can be RTBC.

hampercm’s picture

Status: Reviewed & tested by the community » Needs work
hampercm’s picture

Assigned: hampercm » Unassigned

Ugh, sorry for the extraneous comments :P

Wim Leers’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
25.1 KB
1.96 KB

Done!

  • e0ipso committed f2d2807 on 8.x-1.x authored by Wim Leers
    Hard code API prefix to /jsonapi (#2838580 by Wim Leers, hampercm)
    
e0ipso’s picture

Status: Reviewed & tested by the community » Fixed

I did a quick review and all looks good. So much easier when you get stuff RTBCed :_)

This is merged.

Wim Leers’s picture

Thanks! Already unpostponed #2831134 and uploaded the patch for testing that should come back green: #2831134: Remove configurable 'id_field': always use UUID.

dagmar’s picture

This change requires an update at the docs: https://www.drupal.org/node/2804085

I think would be nice to document how to change the prefix like Wim indicated in #17.

hampercm’s picture

Documentation updated! It might make sense to refactor the documentation and remove the page at https://www.drupal.org/node/2804085 completely, or reuse it to give a detailed example of altering the routes.

e0ipso’s picture

It's worth noting that you can detect if a route is managed by JSON API by checking:

$route->getOption('_is_jsonapi');

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gerzenstl’s picture

I tryied the following implementation change the URL prefix and works fine:

namespace Drupal\custom_example\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Listens to the dynamic route events.
 */
class CustomRouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {

    $routesCollection = $collection->all();
    foreach ($routesCollection as $key => $route) {
      if ($route->getOption('_is_jsonapi')) {
        $path = $route->getPath();
        $newPath = str_replace('/jsonapi', '/api/v1', $path);
        $route->setPath($newPath);
      }
    }
  }
}

any suggestion is welcome.

DanBerk4’s picture

@ #29 This works for the collection/list request, but breaks for individual item request. i.e. /api/v1/node/11 will return a 404 instead of the node with id 11

bojanz’s picture

Note that JSON API nowadays has a jsonapi.base_path container parameter.

I too wanted to replace /jsonapi with /api/v1, and all it took was this in my service provider:

namespace Drupal\bojan_test;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;

class BojanTestServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function alter(ContainerBuilder $container) {
    $container->setParameter('jsonapi.base_path', '/api/v1');
  }

}

(This issue is ranked high in a Google search, so I figured I'd leave a note)

Wim Leers’s picture

#31: thanks! :)

Note that if you want to customize this at the site level rather than in a (site-specific) module, you can do so by providing a services.yml file in your sites/yoursite.com/:

parameters:
  jsonapi.base_path: /api/v1