Problem/Motivation

Currently (and also after #2308745: Remove rest.settings.yml, use rest_resource config entities) the rest configuration is quite flexible / verbose.

+  GET:
+    supported_formats:
+      - hal_json
+    supported_auth:
+      - basic_auth
+  POST:
+    supported_formats:
+      - hal_json
+    supported_auth:
+      - basic_auth
+  PATCH:
+    supported_formats:
+      - hal_json
+    supported_auth:
+      - basic_auth
+  DELETE:
+    supported_formats:
+      - hal_json
+    supported_auth:
+      - basic_auth

Problems

  • Realistically you don't want different formats for different HTTP methods
  • You also don't want different authentication for different HTTP methods

Proposed resolution

Make it possible (optionally_ on the config entity level to just specify

a) the list of formats of your entire resource
b) the list of authentication methods of your resource
c) the exposed HTTP methods

At the end it could then look like the following:

<code>
methods:
  - GET
  - POST
  - PATCH
  - DELETE
formats:
  - hal_json
authentication:
  - basic_auth

Remaining tasks

  1. #2308745: Remove rest.settings.yml, use rest_resource config entities
  2. #2721595: Simplify REST configuration

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#66 interdiff.txt7.84 KBwim leers
#66 rest_config_simplify-2721595-66.patch38.88 KBwim leers
#60 rest_config_simplify-2721595-60.patch36.13 KBwim leers
#55 interdiff.txt5.43 KBwim leers
#55 rest_config_simplify-2721595-55.patch36.14 KBwim leers
#51 2721595-51.patch35.02 KBdawehner
#48 2721595-48.patch32.63 KBdawehner
#48 interdiff.txt1.49 KBdawehner
#45 interdiff.txt10.28 KBwim leers
#45 rest_config_simplify-2721595-45.patch36.84 KBwim leers
#38 interdiff-post_update.txt5.26 KBwim leers
#35 interdiff.txt4.23 KBwim leers
#35 rest_config_simplify-2721595-35.patch34.57 KBwim leers
#34 rest_config_simplify-2721595-32.patch33.76 KBwim leers
#32 interdiff.txt14.52 KBwim leers
#32 rest_config_simplify-2721595-32.patch31.86 KBwim leers
#24 interdiff.txt2.18 KBwim leers
#24 rest_config_simplify-2721595-24.patch25.86 KBwim leers
#23 rest_config_simplify-2721595-23.patch23.62 KBwim leers
#21 rest_config_simplify-2721595-21.patch23.62 KBwim leers
#19 rest_config_simplify-2721595-19.patch21.68 KBwim leers
#16 rest_config_simplify-2721595-16--do-not-test.patch21.9 KBwim leers
#16 rest_config_simplify-2721595-16.patch89.14 KBwim leers
#13 rest_config_simplify-2721595-13--do-not-test.patch21.9 KBwim leers
#13 rest_config_simplify-2721595-13.patch88.59 KBwim leers
#12 interdiff.txt1.19 KBwim leers
#12 rest_config_simplify-2721595-12--do-not-test.patch20.4 KBwim leers
#12 rest_config_simplify-2721595-12.patch87.45 KBwim leers
#7 rest_config_simplify-2721595-7--do-not-test.patch18.49 KBwim leers
#7 rest_config_simplify-2721595-7.patch79.54 KBwim leers
#9 rest_config_simplify-2721595-9--do-not-test.patch20.42 KBwim leers
#9 rest_config_simplify-2721595-9.patch81.47 KBwim leers

Comments

dawehner created an issue. See original summary.

wim leers’s picture

Title: Simplify rest configuration? » Simplify REST configuration?
Related issues: +#2308745: Remove rest.settings.yml, use rest_resource config entities

+1 for principle. But we need to better understand whether we would do this before #2308745: Remove rest.settings.yml, use rest_resource config entities, or after, or only do this.

dawehner’s picture

+1 for principle. But we need to better understand whether we would do this before #2308745: Remove rest.settings.yml, use rest_resource config entities, or after, or only do this.

I would be totally for after this, because entities allow us to introduce BC layers much easier.

wim leers’s picture

dawehner’s picture

Oh actually the other way round, so this issue should be postponed on #2308745: Remove rest.settings.yml, use rest_resource config entities.

wim leers’s picture

Title: Simplify REST configuration? » [PP-1] Simplify REST configuration
Issue summary: View changes
Status: Active » Postponed

Ok.

wim leers’s picture

Priority: Normal » Major
Status: Postponed » Needs review
Issue tags: +API-First Initiative, +DX (Developer Experience)
StatusFileSize
new18.49 KB
new79.54 KB

This patch fully implements this simplified REST configuration as discussed & agreed by @klausi, @dawehner and I at #2308745: Remove rest.settings.yml, use rest_resource config entities, see comments #170–#202.

This patch is built on top of #2308745-208: Remove rest.settings.yml, use rest_resource config entities. It already includes upgrade path test coverage (with an upgrade path relative to that of #2308745).

Status: Needs review » Needs work

The last submitted patch, 7: rest_config_simplify-2721595-7.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new81.47 KB
new20.42 KB

git is stupid, it really really wants to consider that PHP file as binary, even though .gitattributes already tells/forces it otherwise…

So, rolling the patch with --binary, because I can't figure out how to force git to be sane.

The last submitted patch, 9: rest_config_simplify-2721595-9.patch, failed testing.

dawehner’s picture

Just a quick comment.

+++ b/core/modules/rest/rest.install
@@ -51,5 +49,39 @@ function rest_update_8201() {
+  $resource_config_entities = $resource_config_storage->loadMultiple();
* - In particular, loading, saving, or performing any other CRUD operation on
* an entity is never safe to do (they always involve hooks and services).

(from core/lib/Drupal/Core/Extension/module.api.php:566)
but

* - Using \Drupal::configFactory()->getEditable() and \Drupal::config(), as
* long as you make sure that your update data matches the schema, and you
* use the $has_trusted_data argument in the save operation.

wim leers’s picture

StatusFileSize
new87.45 KB
new20.4 KB
new1.19 KB

First, rebased on top of #2308745-213: Remove rest.settings.yml, use rest_resource config entities. Should have the same fails.

wim leers’s picture

StatusFileSize
new88.59 KB
new21.9 KB

#11: fixed. That will hopefully fix the first fail. I couldn't reproduce that fail locally, so I can't be certain.

The second fail — which is reproducible — is now also fixed. But I fixed it in a perhaps unexpected way. The test itself is bizarre: it installs the hal and rest module, which means we don't even need to enable json. We just need to enable cookie authentication on the existing resource. And we just need to request it in HAL+JSON, rather than JSON. That seems closer to the original intent.


Self-review:

  1. +++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity__node_2721595.yml
    @@ -0,0 +1,29 @@
    +    - node
    

    Should be user.

  2. +++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity__user_2721595.yml
    @@ -0,0 +1,30 @@
    +      - oauth
    

    Specifying cookie would be saner, since that actually exists.

  3. +++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity__user_2721595.yml
    @@ -0,0 +1,30 @@
    +    - node
    

    Should be user.

The last submitted patch, 12: rest_config_simplify-2721595-12.patch, failed testing.

The last submitted patch, 13: rest_config_simplify-2721595-13.patch, failed testing.

wim leers’s picture

StatusFileSize
new89.14 KB
new21.9 KB

#2308745-213: Remove rest.settings.yml, use rest_resource config entities introduced 5 new failures. #2308745-216: Remove rest.settings.yml, use rest_resource config entities fixed them. No interdiff because this is just a rebase on top of 216.

The last submitted patch, 16: rest_config_simplify-2721595-16.patch, failed testing.

wim leers’s picture

Title: [PP-1] Simplify REST configuration » Simplify REST configuration
Assigned: Unassigned » wim leers
wim leers’s picture

StatusFileSize
new21.68 KB

Rebased.

Status: Needs review » Needs work

The last submitted patch, 19: rest_config_simplify-2721595-19.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new23.62 KB
error: cannot apply binary patch to 'core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_update_8202.php' without full index line

SIGH. git diff --binary it is.

Status: Needs review » Needs work

The last submitted patch, 21: rest_config_simplify-2721595-21.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new23.62 KB

One small change in ResourceGranularityRestResourcesConfigEntitiesUpdateTest wasn't committed. Can't provide an interdiff because git insists on interpreting this file as binary — see #21.

wim leers’s picture

Issue tags: +Needs tests
StatusFileSize
new25.86 KB
new2.18 KB

And this fixes the fails in ConfigDependenciesTest. This also made me realize we need expanded test coverage for that. But first, let's see whether this is green already.

The last submitted patch, 23: rest_config_simplify-2721595-23.patch, failed testing.

wim leers’s picture

YAY! Green! Now working on tests and clean-up.

dawehner’s picture

+++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity.user_2721595.yml
index b9a9062..cd2d28b 100644
--- a/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php

--- a/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php
+++ b/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php

+++ b/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php
+++ b/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php
@@ -21,7 +21,6 @@ class ConfigDependenciesTest extends KernelTestBase {

@@ -21,7 +21,6 @@ class ConfigDependenciesTest extends KernelTestBase {
 

So I guess we have to expand the test coverage for the non granular case here?

wim leers’s picture

Yep, that's what I'm working on :)

dawehner’s picture

Update numbers are a curse. This issue will conflict with #2228141: Add authentication support to REST views, that's for sure.

wim leers’s picture

I don't mind rerolling, don't worry :)

dawehner’s picture

Oh yeah I just wanted to trigger a review :) Thanks a ton.

wim leers’s picture

Issue tags: -Needs tests
StatusFileSize
new31.86 KB
new14.52 KB

Nicely triggered :D

Here's the missing test coverage. It only made sense to update 2 test existing methods to accept providers, the rest requires separate test methods for "method" vs "resource" to be able to sanely test things. In doing so, updated the onDependencyRemovalForResourceGranularity() code to be in sync with changes made to the parent issue after it was split off.

Now hunting for inconsistencies.

Status: Needs review » Needs work

The last submitted patch, 32: rest_config_simplify-2721595-32.patch, failed testing.

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new33.76 KB

GAAAHHHHHH git diff --binary!!!!

wim leers’s picture

StatusFileSize
new34.57 KB
new4.23 KB
  1. +++ b/core/modules/rest/rest.install
    @@ -40,5 +42,39 @@ function rest_update_8201() {
    +function rest_update_8202() {
    

    This actually belongs in post-update.

    I started working on that, but… quickly ran into problems:

    18:35:09 <WimLeers> dawehner: alexpott_ I think I've found a significant problem with hook_update_post_NAME(), i.e. https://www.drupal.org/node/2563673
    18:35:10 <Druplicon> https://www.drupal.org/node/2563673 => hook_post_update_NAME() introduced to change content after hook_update_N() runs => 0 comments, 4 IRC mentions
    18:35:23 <WimLeers> dawehner: alexpott_ It's not possible to test hook_post_update_NAME() in isolation
    18:35:28 <WimLeers> dawehner: alexpott_ they always run
    18:35:45 <WimLeers> dawehner: alexpott_ So in the "simplification" follow-up, I need to update the _existing_ tests!
    18:36:01 <WimLeers> dawehner: alexpott_ And every single future hook_post_update_NAME() will have to do the same
    

    So, postponing doing this until I get feedback about that.

  2. +++ b/core/modules/rest/rest.install
    @@ -40,5 +42,39 @@ function rest_update_8201() {
    +    if ($resource_config_entity->get('granularity') === 'method') {
    ...
    +        $resource_config_entity->set('granularity', 'resource');
    

    Should use the constant.

  3. +++ b/core/modules/rest/rest.install
    --- a/core/modules/rest/src/Entity/ConfigDependencies.php
    +++ b/core/modules/rest/src/Entity/ConfigDependencies.php
    

    This looks bizarre when searching for classes. I wonder if renaming from ConfigDependencies to EntityResourceConfigDependencies would make more sense?

  4. +++ b/core/modules/rest/src/RestResourceConfigInterface.php
    @@ -16,6 +16,11 @@
    +   * Granularity value for per-resourceconfiguration.
    

    Typo.

  5. +++ b/core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php
    @@ -0,0 +1,70 @@
    +class ResourceGranularityRestResourcesConfigEntitiesUpdateTest extends UpdatePathTestBase {
    ...
    +  public function testResourcesConvertedToConfigEntities() {
    

    Copy/paste remnant.

  6. There's a @todo referencing this issue in rest.post-update.php. That should be removed.

For resource granularity REST resources, we could actually simplify the schema further. Looking into that now.

wim leers’s picture

For resource granularity REST resources, we could actually simplify the schema further. Looking into that now.

I thought we could get rid of the configuration key for the resource granularity. We can't. We need the schema type (called granularity here) to affect a predictable key (called configuration here).

The last submitted patch, 34: rest_config_simplify-2721595-32.patch, failed testing.

wim leers’s picture

StatusFileSize
new5.26 KB

For #35.1, here's what I started to do: interdiff-post_update.txt.

The only remaining thing is #35.3. But that can happen in a follow-up for sure.

So, AFAICT the main thing to review here is whether to use hook_post_update_NAME() or hook_update_N().

wim leers’s picture

Assigned: wim leers » Unassigned

#35 is green. This is now blocked on review. Unassigning.

dawehner’s picture

  1. +++ b/core/modules/rest/rest.install
    @@ -39,6 +41,40 @@ function rest_update_8201() {
    +/**
    + * Simplify method-granularity REST resources to resource-granularity.
    + */
    +function rest_update_8202() {
    +  $config_factory = \Drupal::configFactory();
    

    Are we sure this actually works properly? Wouldn't this method be executed before \rest_post_update_create_rest_resource_config_entities?

  2. +++ b/core/modules/rest/src/Entity/ConfigDependencies.php
    @@ -183,7 +176,71 @@ protected function onDependencyRemovalForMethodGranularity(RestResourceConfigInt
    +        if (empty($configuration['authentication'])) {
    +          // Remove the key if there are no more authentication providers
    +          // supported.
    +          unset($configuration['authentication']);
    +        }
    +        if (empty($configuration['formats'])) {
    +          // Remove the key if there are no more formats supported.
    +          unset($configuration['formats']);
    +        }
    +        if (empty($configuration['authentication']) || empty($configuration['formats'])) {
    +          // If there no longer are any supported authentication providers or
    +          // formats, this REST resource can no longer function, and so we
    +          // cannot fix this config entity to keep it working.
    +          $configuration = [];
    +        }
    

    As said before, we could simplify all that to a single array_filter call, can't we?

  3. +++ b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php
    @@ -58,6 +58,10 @@ public function testResourcesConvertedToConfigEntities() {
    diff --git a/core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_update_8202.php b/core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_update_8202.php
    

    Note: This file uses a module weight of 8201, did you considered using 21121112 instead? Or some other number :)

wim leers’s picture

Status: Needs review » Needs work

Discussed with dawehner. Moving the upgrade path to post-update.

#40:

  1. If we move to post-update, then this becomes moot.
  2. Agreed!
  3. Oh wait, what? I thought this was just a naming convention to create fixtures for a particular hook_update_N(), where the fixture must include the "N" in the filename?
wim leers’s picture

Assigned: Unassigned » wim leers
dawehner’s picture

Oh wait, what? I thought this was just a naming convention to create fixtures for a particular hook_update_N(), where the fixture must include the "N" in the filename?

Sorry I should have been more clear. This was about the core.extension weight being the same as the module schema version, due to some c&p.

If we move to post-update, then this becomes moot.

Exactly, cool

wim leers’s picture

Got it, thanks! :)

wim leers’s picture

Status: Needs work » Needs review
StatusFileSize
new36.84 KB
new10.28 KB

So this reroll:

  1. moves the upgrade path from hook_update_N() to hook_post_update_NAME()
  2. which means that the upgrade path test at RestConfigurationEntitiesUpdateTest had to be updated, because that will now also run this new post-update function
  3. but this still adds a separate test, so we can test all edge cases of the upgrade path

To be able to test this post-update hook in isolation, which is necessary to be able to test all nuances of this post-update hook, turns out you have to set the existing_updates key-value pair in the post_update collection in the key-value store. This is the very first test doing that. That required a lot of digging.

However, to make it much worse, the move from hook_update_N() to hook_post_update_NAME() introduced at least two bizarre problems, one of which is minor, the other one of which is critical:

  1. the minor one: loadMultiple() now appears to be insisting on returning things in a different order, even when calling resetCache() after running updates:
    $this->assertIdentical(['entity.comment', 'entity.node', 'entity.user'], array_keys($resource_config_entities));
    

    had to be changed to:

    $this->assertIdentical(['entity.node', 'entity.comment', 'entity.user'], array_keys($resource_config_entities));
    
  2. the critical one: the test coverage now fails … because loadMultiple() seems to be forever returning the old values (even after calling resetCache()), and load() always returns the correct, updated value. This is a HUGE WTF. I'm totally lost. I included the following debug code:
        // WTF: loadMultiple() vs load() are returning different results!
    
        // prints 'method'
        debug($resource_config_storage->loadMultiple()['entity.node']->get('granularity'));
        // prints 'resource'
        debug($resource_config_storage->load('entity.node')->get('granularity'));
    
    
    
        // WTF++: even after resetting caches
        $resource_config_storage->resetCache();
    
        // prints 'method'
        debug($resource_config_storage->loadMultiple()['entity.node']->get('granularity'));
        // prints 'resource'
        debug($resource_config_storage->load('entity.node')->get('granularity'));
    

Help very much appreciated with solving that second WTF.

Because that of course means the tests aren't passing… the very same tests that were passing just fine before! That's the even more scary/mysterious part: that this bug apparently only happens when running/testing post-update functions.


Still to do:

  1. sort out this huge WTF described above.
  2. rename drupal-8.rest-rest_update_8202.php, since it's no longer for a hook_update_N() function

Status: Needs review » Needs work

The last submitted patch, 45: rest_config_simplify-2721595-45.patch, failed testing.

wim leers’s picture

Assigned: wim leers » Unassigned

Rarely have I been this glad to see a fail from testbot. It confirms what I see locally:

debug: [Debug] Line 74 of core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php:
method

debug: [Debug] Line 76 of core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php:
resource

debug: [Debug] Line 84 of core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php:
method

debug: [Debug] Line 86 of core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php:
resource

Those should all be resource, but clearly they're not.

Hopefully a (config) entity system or update system expert can figure out the root cause for this.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new1.49 KB
new32.63 KB

We use simply a bad setup.

Here is a comment I had before.

So this is what happens:

You do stuff before running the updates, see \Drupal\rest\Tests\Update\ResourceGranularityRestResourcesConfigEntitiesUpdateTest::testMethodGranularityResourcesConvertedToResourceGranularityResources

This line is part of that for example:
$resource_config_entities = $resource_config_storage->loadMultiple();
This lines calls out to ConfigEntityStorage, and ConfigFactory and CachedStorage::findByPrefix. This later one caches stuff statically.

Here is a patch which adds a reset, but I'm not sure its actually a good idea to call out to any kind of api before running the test.

Status: Needs review » Needs work

The last submitted patch, 48: 2721595-48.patch, failed testing.

dawehner’s picture

Damnit, I forgot --binary

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new35.02 KB
wim leers’s picture

We use simply a bad setup.

GAHHHHHHHHHHH !!!!!!!!!!!!!!

-     'name' => 'rest.resource.entity__node',
+     'name' => 'rest.resource.entity.node',

Glad you found it, a fresh pair of eyes clearly saved the day there :D


Here is a patch which adds a reset, but I'm not sure its actually a good idea to call out to any kind of api before running the test.

There's no reset in the patch.

Here is a patch which adds a reset, but I'm not sure its actually a good idea to call out to any kind of api before running the test.

IMO it is fine. The whole point of post-update is that it behaves just like regular code. Which means you get tag-based invalidation, and static cache invalidation.


This patch is now ready as far as I'm concerned. The only thing that remains to be done, is renaming core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_update_8202.php to something that doesn't include 8202 in its name. The post-update function has a quite long name (rest_post_update_simplify_method_granularity_to_resource_granularity_where_possible()), do we want the same for the fixture? Do we even want that very long name for the post-update function?

dawehner’s picture

There's no reset in the patch.

oh yeah that was the patch before I found the actual reason.

The post-update function has a quite long name (rest_post_update_simplify_method_granularity_to_resource_granularity_where_possible()), do we want the same for the fixture? Do we even want that very long name for the post-update function?

Well, I would just use rest_post_update_simplify_granularity() and call it a day.

wim leers’s picture

Assigned: Unassigned » wim leers
Status: Needs review » Needs work

Cool, will do.

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Needs review
StatusFileSize
new36.14 KB
new5.43 KB

Done.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thank you!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 55: rest_config_simplify-2721595-55.patch, failed testing.

wim leers’s picture

Status: Needs work » Reviewed & tested by the community

Random fail in Drupal\field\Tests\EntityReference\EntityReferenceAdminTest. Re-testing, back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 55: rest_config_simplify-2721595-55.patch, failed testing.

wim leers’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new36.13 KB

Trivial conflict in core/modules/rest/config/optional/rest.resource.entity.node.yml after #2755843: The order in which config is saved affects dependency calculations landed.

Straight reroll, so no interdiff.

gcassie’s picture

I'm getting an error trying to apply this update.

STR:
* Clean Drupal 8.1.6 install with Rest UI contrib
* Enable the user rest resource, with GET and basic auth
* Update core code to 8.2.x-dev
* Attempt to run update.php
* Get this:

rest module

Update #8201

Failed: Drupal\Component\Plugin\Exception\PluginNotFoundException: The "rest_resource_config" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 125 of /Users/george.cassie/Sites/devdesktop/drupal-8.1.6/core/lib/Drupal/Core/Entity/EntityTypeManager.php).
wim leers’s picture

#61: the same problem was reported at #2308745-253: Remove rest.settings.yml, use rest_resource config entities, it's not something this patch introduces.

effulgentsia’s picture

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

Grrr. d.o. ate my dreditor review of this patch. I need to rewrite that review, but don't have time this second. Will do so within 24 hours.

effulgentsia’s picture

Assigned: effulgentsia » Unassigned
  1. +++ b/core/modules/rest/config/optional/rest.resource.entity.node.yml
    @@ -7,25 +7,14 @@ dependencies:
    -  GET:
    -    supported_formats:
    -      - hal_json
    -    supported_auth:
    -      - basic_auth
    -  POST:
    -    supported_formats:
    -      - hal_json
    -    supported_auth:
    -      - basic_auth
    -  PATCH:
    -    supported_formats:
    -      - hal_json
    -    supported_auth:
    -      - basic_auth
    -  DELETE:
    -    supported_formats:
    -      - hal_json
    -    supported_auth:
    -      - basic_auth
    +  methods:
    +    - GET
    +    - POST
    +    - PATCH
    +    - DELETE
    +  formats:
    +    - hal_json
    +  authentication:
    +    - basic_auth
    

    Yay!

  2. +++ b/core/modules/rest/rest.post_update.php
    @@ -34,6 +31,42 @@ function rest_post_update_create_rest_resource_config_entities() {
    +          'formats' => $configuration['GET']['supported_formats'],
    +          'authentication' => $configuration['GET']['supported_auth']
    

    Is it not possible for existing config to support some methods without supporting GET?

  3. +++ b/core/modules/rest/src/Entity/ConfigDependencies.php
    @@ -61,31 +61,21 @@ public static function create(ContainerInterface $container) {
    +      $methods = ['GET'];
    

    Same question here, but for new config using resource granularity.

  4. +++ b/core/modules/rest/src/Entity/RestResourceConfig.php
    @@ -45,7 +45,7 @@ class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigI
    +   * Currently either 'method' or 'resource'.
    

    Should we reference the constants here instead of the literals?

  5. +++ b/core/modules/rest/src/Entity/RestResourceConfig.php
    @@ -116,8 +116,7 @@ public function getMethods() {
         else {
    -      throw new \InvalidArgumentException("A different granularity then 'method' is not supported yet.");
    -      // @todo Add resource-level granularity support in https://www.drupal.org/node/2721595.
    +      return $this->configuration['methods'];
         }
    

    Can we changes this and similar code blocks to use switch/case, and still throw an exception when not one of the allowed constants? Both for clarity, and since nothing in the config schema enforces the value to one of these 2.

  6. +++ b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php
    @@ -55,8 +53,12 @@ public function testResourcesConvertedToConfigEntities() {
    diff --git a/core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_post_update_resource_granularity.php b/core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_post_update_resource_granularity.php
    

    This shows up in the patch as binary rather than text, so no additional context via dreditor. But, within this file, there's db ->merge statements, but we know the starting database, so shouldn't these all be either ->insert or ->update? Other similar fixtures in HEAD use explicit insert/update, not merge.

  7. +++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity.comment_2721595.yml
    @@ -0,0 +1,30 @@
    +      - xml
    

    Let's add a comment here explaining why this is done (e.g., so that there's a test of updating configuration with method-specific formats).

  8. +++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity.user_2721595.yml
    @@ -0,0 +1,30 @@
    +      - oauth
    

    Let's add a comment here explaining why this is done (e.g., so that there's a test of updating configuration with method-specific authentication).

wim leers’s picture

Assigned: Unassigned » wim leers
Status: Needs review » Needs work

Will reroll.

wim leers’s picture

Assigned: wim leers » Unassigned
Status: Needs work » Reviewed & tested by the community
StatusFileSize
new38.88 KB
new7.84 KB
  1. Interesting point! Technically that totally is possible. Fixed.
  2. Also fixed.
  3. Changed to reference the constants.
  4. Done. (Note that config schemas cannot enforce the value to one of these 2: config schemas have no "enum" concept. Maybe that's something worth adding in the future…)
  5. Other fixtures also use ->merge(), that's where I even got the idea from. Examples: core/modules/rest/tests/fixtures/update/rest-export-with-authentication.php, core/modules/system/tests/fixtures/update/drupal-8.language-enabled.php.
  6. Done.
  7. Done.

Re-RTBC'd since this is all trivial refactoring.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 66: rest_config_simplify-2721595-66.patch, failed testing.

wim leers’s picture

Status: Needs work » Reviewed & tested by the community

Random fail: UpdatePathTestBaseFilledTest .

effulgentsia’s picture

Status: Reviewed & tested by the community » Needs work

Sorry. A few more nits.

  1. +++ b/core/modules/rest/src/Entity/ConfigDependencies.php
    @@ -102,6 +96,10 @@ protected function calculateDependenciesForMethodGranularity(RestResourceConfigI
    +    if (isset($dependencies['module'])) {
    +      sort($dependencies['module']);
    +    }
    

    Either we assume that the result of this function always passes through DependencyTrait::addDependency() (which it does within RestResourceConfig::calculateDependencies()) prior to being saved to storage or otherwise needing to be sorted, or we do not assume this. If we assume it, then we shouldn't need this hunk, but we should add docs to the function to clarify this assumption. If it's only to make a unit test pass, then perhaps the unit test should be the one to sort. If we do not assume this, and therefore, require this function to sort, then it should mimic DependencyTrait::addDependency() and also ensure uniqueness.

  2. +++ b/core/modules/rest/src/Entity/ConfigDependencies.php
    @@ -183,7 +182,71 @@ protected function onDependencyRemovalForMethodGranularity(RestResourceConfigInt
    +          if (in_array($format, $rest_config->getFormats('GET'))) {
    +            $configuration['formats'] = array_diff($configuration['formats'], $removed_formats);
    +          }
    

    Per #64.2, we shouldn't assume GET is defined. Technically, this code doesn't, as the implementation of getFormats() ignores the passed-in method, but that's not obvious from reading this code in isolation. Any reason not to simply use $configuration['formats'] instead of $rest_config->getFormats('GET')?

  3. +++ b/core/modules/rest/src/Entity/ConfigDependencies.php
    @@ -183,7 +182,71 @@ protected function onDependencyRemovalForMethodGranularity(RestResourceConfigInt
    +          if (in_array($auth, $rest_config->getAuthenticationProviders('GET'))) {
    +            $configuration['authentication'] = array_diff($configuration['authentication'], $removed_auth);
    +          }
    

    Same as above, but for getAuthenticationProviders().

  • effulgentsia committed 3a13f94 on 8.2.x
    Issue #2721595 by Wim Leers, dawehner: Simplify REST configuration
    
effulgentsia’s picture

Status: Needs work » Fixed

As I looked more at #69, I couldn't find a reason for those points to block commit, so pushed to 8.2.x. A follow-up for #69 would be appreciated though.

wim leers’s picture

Awesome, thank you! I will file a follow-up first thing tomorrow!

effulgentsia’s picture

Issue tags: +8.2.0 release notes
wim leers’s picture

  • effulgentsia committed 3a13f94 on 8.3.x
    Issue #2721595 by Wim Leers, dawehner: Simplify REST configuration
    

  • effulgentsia committed 3a13f94 on 8.3.x
    Issue #2721595 by Wim Leers, dawehner: Simplify REST configuration
    

Status: Fixed » Closed (fixed)

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