 .../config/optional/rest.resource.entity.node.yml  |  31 ++--
 core/modules/rest/config/schema/rest.schema.yml    |  24 ++-
 core/modules/rest/rest.install                     |   2 +
 core/modules/rest/rest.post_update.php             |  41 ++++-
 .../modules/rest/src/Entity/ConfigDependencies.php |  97 ++++++++---
 .../modules/rest/src/Entity/RestResourceConfig.php |  12 +-
 .../rest/src/RestResourceConfigInterface.php       |   5 +
 ...larityRestResourcesConfigEntitiesUpdateTest.php |  89 ++++++++++
 .../Update/RestConfigurationEntitiesUpdateTest.php |  12 +-
 .../update/drupal-8.rest-rest_update_8202.php      | Bin 0 -> 5371 bytes
 .../rest.resource.entity.comment_2721595.yml       |  30 ++++
 .../update/rest.resource.entity.node_2721595.yml   |  29 ++++
 .../update/rest.resource.entity.user_2721595.yml   |  30 ++++
 .../src/Kernel/Entity/ConfigDependenciesTest.php   | 187 ++++++++++++++++-----
 .../src/Tests/System/ResponseGeneratorTest.php     |  13 +-
 15 files changed, 494 insertions(+), 108 deletions(-)

diff --git a/core/modules/rest/config/optional/rest.resource.entity.node.yml b/core/modules/rest/config/optional/rest.resource.entity.node.yml
index 0cf4d78..39f7713 100644
--- a/core/modules/rest/config/optional/rest.resource.entity.node.yml
+++ b/core/modules/rest/config/optional/rest.resource.entity.node.yml
@@ -1,27 +1,16 @@
 id: entity.node
 plugin_id: 'entity:node'
-granularity: method
+granularity: resource
 configuration:
-  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
 dependencies:
   module:
     - node
diff --git a/core/modules/rest/config/schema/rest.schema.yml b/core/modules/rest/config/schema/rest.schema.yml
index 41bc2bf..04f88a6 100644
--- a/core/modules/rest/config/schema/rest.schema.yml
+++ b/core/modules/rest/config/schema/rest.schema.yml
@@ -8,7 +8,6 @@ rest.settings:
       label: 'Domain of the relation'
 
 # Method-level granularity of REST resource configuration.
-# @todo Add resource-level granularity in https://www.drupal.org/node/2721595.
 rest_resource.method:
   type: mapping
   mapping:
@@ -25,6 +24,29 @@ rest_resource.method:
       type: rest_request
       label: 'DELETE method settings'
 
+# Resource-level granularity of REST resource configuration.
+rest_resource.resource:
+  type: mapping
+  mapping:
+    methods:
+      type: sequence
+      label: 'Supported methods'
+      sequence:
+        type: string
+        label: 'HTTP method'
+    formats:
+      type: sequence
+      label: 'Supported formats'
+      sequence:
+        type: string
+        label: 'Format'
+    authentication:
+      type: sequence
+      label: 'Supported authentication providers'
+      sequence:
+        type: string
+        label: 'Authentication provider'
+
 rest_request:
   type: mapping
   mapping:
diff --git a/core/modules/rest/rest.install b/core/modules/rest/rest.install
index 601206d..f786feb 100644
--- a/core/modules/rest/rest.install
+++ b/core/modules/rest/rest.install
@@ -30,6 +30,8 @@ function rest_requirements($phase) {
 
 /**
  * Install the REST config entity type and fix old settings-based config.
+ *
+ * @see rest_post_update_create_rest_resource_config_entities()
  */
 function rest_update_8201() {
   \Drupal::entityDefinitionUpdateManager()->installEntityType(\Drupal::entityTypeManager()->getDefinition('rest_resource_config'));
diff --git a/core/modules/rest/rest.post_update.php b/core/modules/rest/rest.post_update.php
index 7068662..68ec807 100644
--- a/core/modules/rest/rest.post_update.php
+++ b/core/modules/rest/rest.post_update.php
@@ -16,11 +16,8 @@
 /**
  * Create REST resource configuration entities.
  *
- * @todo https://www.drupal.org/node/2721595 Automatically upgrade those REST
- *   resource config entities that have the same formats/auth mechanisms for all
- *   methods to "granular: resource".
- *
  * @see rest_update_8201()
+ * @see https://www.drupal.org/node/2308745
  */
 function rest_post_update_create_rest_resource_config_entities() {
   $resources = \Drupal::state()->get('rest_update_8201_resources', []);
@@ -34,6 +31,42 @@ function rest_post_update_create_rest_resource_config_entities() {
   }
 }
 
+/**
+ * Simplify method-granularity REST resources to resource-granularity.
+ *
+ * @see https://www.drupal.org/node/2721595
+ */
+function rest_post_update_simplify_method_granularity_to_resource_granularity_where_possible() {
+  /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_config_entities */
+  $resource_config_entities = RestResourceConfig::loadMultiple();
+
+  foreach ($resource_config_entities as $resource_config_entity) {
+    if ($resource_config_entity->get('granularity') === RestResourceConfigInterface::METHOD_GRANULARITY) {
+      $configuration = $resource_config_entity->get('configuration');
+
+      $format_and_auth_configuration = [];
+      foreach (array_keys($configuration) as $method) {
+        $format_and_auth_configuration['format'][$method] = implode(',', $configuration[$method]['supported_formats']);
+        $format_and_auth_configuration['auth'][$method] = implode(',', $configuration[$method]['supported_auth']);
+      }
+
+      // If each method has the same formats and the same authentication
+      // providers configured, convert it to 'granularity: resource', which has
+      // a simpler/less verbose configuration.
+      if (count(array_unique($format_and_auth_configuration['format'])) === 1 && count(array_unique($format_and_auth_configuration['auth'])) === 1) {
+        $resource_config_entity->set('configuration', [
+          'methods' => array_keys($configuration),
+          'formats' => $configuration['GET']['supported_formats'],
+          'authentication' => $configuration['GET']['supported_auth']
+        ]);
+        $resource_config_entity->set('granularity', RestResourceConfigInterface::RESOURCE_GRANULARITY);
+        $resource_config_entity->save();
+      }
+    }
+  }
+}
+
+
 /**
  * @} End of "addtogroup updates-8.1.x-to-8.2.x".
  */
diff --git a/core/modules/rest/src/Entity/ConfigDependencies.php b/core/modules/rest/src/Entity/ConfigDependencies.php
index 72fc8cc..5362d91 100644
--- a/core/modules/rest/src/Entity/ConfigDependencies.php
+++ b/core/modules/rest/src/Entity/ConfigDependencies.php
@@ -61,31 +61,21 @@ public static function create(ContainerInterface $container) {
    */
   public function calculateDependencies(RestResourceConfigInterface $rest_config) {
     $granularity = $rest_config->get('granularity');
+
+    // Dependency calculation is the same for either granularity, the most
+    // notable difference is that for the 'resource' granularity, the same
+    // authentication providers and formats are supported for every method.
     if ($granularity === RestResourceConfigInterface::METHOD_GRANULARITY) {
-      return $this->calculateDependenciesForMethodGranularity($rest_config);
+      $methods = array_keys($rest_config->get('configuration'));
     }
     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.
+      $methods = ['GET'];
     }
-  }
 
-  /**
-   * Calculates dependencies of a specific rest resource configuration.
-   *
-   * @param \Drupal\rest\RestResourceConfigInterface $rest_config
-   *   The rest configuration.
-   *
-   * @return string[][]
-   *   Dependencies keyed by dependency type.
-   *
-   * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies()
-   */
-  protected function calculateDependenciesForMethodGranularity(RestResourceConfigInterface $rest_config) {
     // The dependency lists for authentication providers and formats
     // generated on container build.
     $dependencies = [];
-    foreach (array_keys($rest_config->get('configuration')) as $request_method) {
+    foreach ($methods as $request_method) {
       // Add dependencies based on the supported authentication providers.
       foreach ($rest_config->getAuthenticationProviders($request_method) as $auth) {
         if (isset($this->authProviders[$auth])) {
@@ -102,6 +92,10 @@ protected function calculateDependenciesForMethodGranularity(RestResourceConfigI
       }
     }
 
+    if (isset($dependencies['module'])) {
+      sort($dependencies['module']);
+    }
+
     return $dependencies;
   }
 
@@ -125,8 +119,7 @@ public function onDependencyRemoval(RestResourceConfigInterface $rest_config, ar
       return $this->onDependencyRemovalForMethodGranularity($rest_config, $dependencies);
     }
     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->onDependencyRemovalForResourceGranularity($rest_config, $dependencies);
     }
   }
 
@@ -183,7 +176,71 @@ protected function onDependencyRemovalForMethodGranularity(RestResourceConfigInt
           }
         }
       }
-      if (!empty($configuration_before != $configuration)) {
+      if ($configuration_before != $configuration && !empty($configuration)) {
+        $rest_config->set('configuration', $configuration);
+        // Only mark the dependencies problems as fixed if there is any
+        // configuration left.
+        $changed = TRUE;
+      }
+    }
+    // If the dependency problems are not marked as fixed at this point they
+    // should be related to the resource plugin and the config entity should
+    // be deleted.
+    return $changed;
+  }
+
+  /**
+   * Informs the entity that entities it depends on will be deleted.
+   *
+   * @param \Drupal\rest\RestResourceConfigInterface $rest_config
+   *   The rest configuration.
+   * @param array $dependencies
+   *   An array of dependencies that will be deleted keyed by dependency type.
+   *   Dependency types are, for example, entity, module and theme.
+   *
+   * @return bool
+   *   TRUE if the entity has been changed as a result, FALSE if not.
+   */
+  public function onDependencyRemovalForResourceGranularity(RestResourceConfigInterface $rest_config, array $dependencies) {
+    $changed = FALSE;
+    // Only module-related dependencies can be fixed. All other types of
+    // dependencies cannot, because they were not generated based on supported
+    // authentication providers or formats.
+    if (isset($dependencies['module'])) {
+      // Try to fix dependencies.
+      $removed_auth = array_keys(array_intersect($this->authProviders, $dependencies['module']));
+      $removed_formats = array_keys(array_intersect($this->formatProviders, $dependencies['module']));
+      $configuration_before = $configuration = $rest_config->get('configuration');
+      if (!empty($removed_auth) || !empty($removed_formats)) {
+        // Try to fix dependency problems by removing affected
+        // authentication providers and formats.
+        foreach ($removed_formats as $format) {
+          if (in_array($format, $rest_config->getFormats('GET'))) {
+            $configuration['formats'] = array_diff($configuration['formats'], $removed_formats);
+          }
+        }
+        foreach ($removed_auth as $auth) {
+          if (in_array($auth, $rest_config->getAuthenticationProviders('GET'))) {
+            $configuration['authentication'] = array_diff($configuration['authentication'], $removed_auth);
+          }
+        }
+        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 = [];
+        }
+      }
+      if ($configuration_before != $configuration && !empty($configuration)) {
         $rest_config->set('configuration', $configuration);
         // Only mark the dependencies problems as fixed if there is any
         // configuration left.
diff --git a/core/modules/rest/src/Entity/RestResourceConfig.php b/core/modules/rest/src/Entity/RestResourceConfig.php
index f2b8b0e..708fae8 100644
--- a/core/modules/rest/src/Entity/RestResourceConfig.php
+++ b/core/modules/rest/src/Entity/RestResourceConfig.php
@@ -3,6 +3,7 @@
 namespace Drupal\rest\Entity;
 
 use Drupal\Core\Config\Entity\ConfigEntityBase;
+use Drupal\Core\Entity\Entity;
 use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
 use Drupal\rest\RestResourceConfigInterface;
 
@@ -45,7 +46,7 @@ class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigI
   /**
    * The REST resource configuration granularity.
    *
-   * @todo Currently only 'method', but https://www.drupal.org/node/2721595 will add 'resource'
+   * Currently either 'method' or 'resource'.
    *
    * @var string
    */
@@ -116,8 +117,7 @@ public function getMethods() {
       return $this->getMethodsForMethodGranularity();
     }
     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'];
     }
   }
 
@@ -140,8 +140,7 @@ public function getAuthenticationProviders($method) {
       return $this->getAuthenticationProvidersForMethodGranularity($method);
     }
     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['authentication'];
     }
   }
 
@@ -170,8 +169,7 @@ public function getFormats($method) {
       return $this->getFormatsForMethodGranularity($method);
     }
     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['formats'];
     }
   }
 
diff --git a/core/modules/rest/src/RestResourceConfigInterface.php b/core/modules/rest/src/RestResourceConfigInterface.php
index 7f34bc4..601bb0e 100644
--- a/core/modules/rest/src/RestResourceConfigInterface.php
+++ b/core/modules/rest/src/RestResourceConfigInterface.php
@@ -15,6 +15,11 @@
    */
   const METHOD_GRANULARITY = 'method';
 
+  /**
+   * Granularity value for per-resource configuration.
+   */
+  const RESOURCE_GRANULARITY = 'resource';
+
   /**
    * Retrieves the REST resource plugin.
    *
diff --git a/core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php b/core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php
new file mode 100644
index 0000000..2d08b8a
--- /dev/null
+++ b/core/modules/rest/src/Tests/Update/ResourceGranularityRestResourcesConfigEntitiesUpdateTest.php
@@ -0,0 +1,89 @@
+<?php
+
+namespace Drupal\rest\Tests\Update;
+
+use Drupal\system\Tests\Update\UpdatePathTestBase;
+
+/**
+ * Tests rest_resource_config entities are simplified when possible.
+ *
+ * @see https://www.drupal.org/node/2721595
+ *
+ * @group rest
+ */
+class ResourceGranularityRestResourcesConfigEntitiesUpdateTest extends UpdatePathTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $modules = ['rest', 'serialization'];
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setDatabaseDumpFiles() {
+    $this->databaseDumpFiles = [
+      __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
+      __DIR__ . '/../../../../rest/tests/fixtures/update/drupal-8.rest-rest_update_8202.php',
+    ];
+  }
+
+  /**
+   * Tests rest_post_update_simplify_method_granularity_to_resource_granularity_where_possible().
+   */
+  public function testMethodGranularityResourcesConvertedToResourceGranularityResources() {
+    /** @var \Drupal\Core\Entity\EntityStorageInterface $resource_config_storage */
+    $resource_config_storage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config');
+
+    // Make sure we have the expected values before the update.
+    $resource_config_entities = $resource_config_storage->loadMultiple();
+    $this->assertIdentical(['entity.comment', 'entity.node', 'entity.user'], array_keys($resource_config_entities));
+    $this->assertIdentical('method', $resource_config_entities['entity.node']->get('granularity'));
+    $this->assertIdentical('method', $resource_config_entities['entity.comment']->get('granularity'));
+    $this->assertIdentical('method', $resource_config_entities['entity.user']->get('granularity'));
+
+    // Read the existing 'entity:comment' and 'entity:user' resource
+    // configuration so we can verify it after the update.
+    $comment_resource_configuration = $resource_config_entities['entity.comment']->get('configuration');
+    $user_resource_configuration = $resource_config_entities['entity.user']->get('configuration');
+
+    $this->runUpdates();
+
+    // Make sure we have the expected values after the update.
+    $resource_config_entities = $resource_config_storage->loadMultiple();
+    $this->assertIdentical(['entity.node', 'entity.comment', 'entity.user'], array_keys($resource_config_entities));
+    // 'entity:node' should be updated.
+    $this->assertIdentical('resource', $resource_config_entities['entity.node']->get('granularity'));
+    $this->assertidentical($resource_config_entities['entity.node']->get('configuration'), [
+      'methods' => ['GET', 'POST', 'PATCH', 'DELETE'],
+      'formats' => ['hal_json'],
+      'authentication' => ['basic_auth'],
+    ]);
+    // 'entity:comment' should be unchanged.
+    $this->assertIdentical('method', $resource_config_entities['entity.comment']->get('granularity'));
+    $this->assertIdentical($comment_resource_configuration, $resource_config_entities['entity.comment']->get('configuration'));
+    // 'entity:user' should be unchanged.
+    $this->assertIdentical('method', $resource_config_entities['entity.user']->get('granularity'));
+    $this->assertIdentical($user_resource_configuration, $resource_config_entities['entity.user']->get('configuration'));
+
+
+
+    // 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'));
+  }
+
+}
diff --git a/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php
index 9100f16..4a506f9 100644
--- a/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php
+++ b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php
@@ -43,10 +43,6 @@ public function testResourcesConvertedToConfigEntities() {
     $resource_config_entities = $resource_config_storage->loadMultiple();
     $this->assertIdentical([], array_keys($resource_config_entities));
 
-    // Read the existing 'entity:node' resource configuration so we can verify
-    // it after the update.
-    $node_configuration = $rest_settings->getRawData()['resources']['entity:node'];
-
     $this->runUpdates();
 
     // Make sure we have the expected values after the update.
@@ -55,8 +51,12 @@ public function testResourcesConvertedToConfigEntities() {
     $resource_config_entities = $resource_config_storage->loadMultiple();
     $this->assertIdentical(['entity.node'], array_keys($resource_config_entities));
     $node_resource_config_entity = $resource_config_entities['entity.node'];
-    $this->assertIdentical(RestResourceConfigInterface::METHOD_GRANULARITY, $node_resource_config_entity->get('granularity'));
-    $this->assertIdentical($node_configuration, $node_resource_config_entity->get('configuration'));
+    $this->assertIdentical(RestResourceConfigInterface::RESOURCE_GRANULARITY, $node_resource_config_entity->get('granularity'));
+    $this->assertIdentical([
+      'methods' => ['GET'],
+      'formats' => ['json'],
+      'authentication' => ['basic_auth'],
+    ], $node_resource_config_entity->get('configuration'));
     $this->assertIdentical(['module' => ['basic_auth', 'node', 'serialization']], $node_resource_config_entity->getDependencies());
   }
 
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
new file mode 100644
index 0000000000000000000000000000000000000000..31ddf3dfcea27ac69cb6bd181c29aa15e74f4663
GIT binary patch
literal 5371
zcmcIoZEqVl67FaJiiJ=>b`Z;vZ0BWl^|f#kw7|7J#JLYe91zqlB@yN>7hJAmCFp;@
zXE@x|t|c9=X!0S>iX0ASo_S_C<Ujsdt*fJ>vnNlEk|)WJOKlW=T-(x1UDio1y<Esz
zB{I*o*A_$GCb?@WX-;2G7SgFn?PZxumrtN-vih7XtxLSBy)IXYUk80nwQ@37NhSR{
zvCG6^T2vN)nhIMeQR|{IdbtsW@@tz5h?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W!thxCgw%`Ki`n$@)#YSUnDNQUNmoDN-*w3N
zp?gmg%HgE6KB@6;La$U{V4dJ$2Q6RMoYY?v209%<nQw?h+~MznAsf6~p*u&J!m;;F
UF~Rek`-Xs!`h#ekUiFgfzdxhq*Z=?k

literal 0
HcmV?d00001

diff --git a/core/modules/rest/tests/fixtures/update/rest.resource.entity.comment_2721595.yml b/core/modules/rest/tests/fixtures/update/rest.resource.entity.comment_2721595.yml
new file mode 100644
index 0000000..15f81be
--- /dev/null
+++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity.comment_2721595.yml
@@ -0,0 +1,30 @@
+id: entity.comment
+plugin_id: 'entity:comment'
+granularity: method
+configuration:
+  GET:
+    supported_formats:
+      - hal_json
+      - xml
+    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
+dependencies:
+  module:
+    - node
+    - basic_auth
+    - hal
diff --git a/core/modules/rest/tests/fixtures/update/rest.resource.entity.node_2721595.yml b/core/modules/rest/tests/fixtures/update/rest.resource.entity.node_2721595.yml
new file mode 100644
index 0000000..0cf4d78
--- /dev/null
+++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity.node_2721595.yml
@@ -0,0 +1,29 @@
+id: entity.node
+plugin_id: 'entity:node'
+granularity: method
+configuration:
+  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
+dependencies:
+  module:
+    - node
+    - basic_auth
+    - hal
diff --git a/core/modules/rest/tests/fixtures/update/rest.resource.entity.user_2721595.yml b/core/modules/rest/tests/fixtures/update/rest.resource.entity.user_2721595.yml
new file mode 100644
index 0000000..be2a607
--- /dev/null
+++ b/core/modules/rest/tests/fixtures/update/rest.resource.entity.user_2721595.yml
@@ -0,0 +1,30 @@
+id: entity.user
+plugin_id: 'entity:user'
+granularity: method
+configuration:
+  GET:
+    supported_formats:
+      - hal_json
+    supported_auth:
+      - basic_auth
+      - oauth
+  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
+dependencies:
+  module:
+    - node
+    - basic_auth
+    - hal
diff --git a/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php b/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php
index b9a9062..4743a03 100644
--- a/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php
+++ b/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php
@@ -21,72 +21,78 @@ class ConfigDependenciesTest extends KernelTestBase {
 
   /**
    * @covers ::calculateDependencies
-   * @covers ::calculateDependenciesForMethodGranularity
+   *
+   * @dataProvider providerBasicDependencies
    */
-  public function testCalculateDependencies() {
+  public function testCalculateDependencies(array $configuration) {
     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
 
-    $rest_config = RestResourceConfig::create([
-      'plugin_id' => 'entity:entity_test',
-      'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
-      'configuration' => [
-        'GET' => [
-          'supported_auth' => ['cookie'],
-          'supported_formats' => ['json'],
-        ],
-        'POST' => [
-          'supported_auth' => ['basic_auth'],
-          'supported_formats' => ['hal_json'],
-        ],
-      ],
-    ]);
+    $rest_config = RestResourceConfig::create($configuration);
 
     $result = $config_dependencies->calculateDependencies($rest_config);
     $this->assertEquals(['module' => [
-      'serialization', 'basic_auth', 'hal',
+      'basic_auth', 'hal', 'serialization',
     ]], $result);
   }
 
   /**
    * @covers ::onDependencyRemoval
-   * @covers ::calculateDependenciesForMethodGranularity
+   * @covers ::onDependencyRemovalForMethodGranularity
+   * @covers ::onDependencyRemovalForResourceGranularity
+   *
+   * @dataProvider providerBasicDependencies
    */
-  public function testOnDependencyRemovalRemoveUnrelatedDependency() {
+  public function testOnDependencyRemovalRemoveUnrelatedDependency(array $configuration) {
     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
 
-    $rest_config = RestResourceConfig::create([
-      'plugin_id' => 'entity:entity_test',
-      'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
-      'configuration' => [
-        'GET' => [
-          'supported_auth' => ['cookie'],
-          'supported_formats' => ['json'],
-        ],
-        'POST' => [
-          'supported_auth' => ['basic_auth'],
-          'supported_formats' => ['hal_json'],
-        ],
-      ],
-    ]);
+    $rest_config = RestResourceConfig::create($configuration);
 
     $this->assertFalse($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['node']]));
-    $this->assertEquals([
-      'GET' => [
-        'supported_auth' => ['cookie'],
-        'supported_formats' => ['json'],
+    $this->assertEquals($configuration['configuration'], $rest_config->get('configuration'));
+  }
+
+  /**
+   * @return array
+   *   An array with numerical keys:
+   *   0. The original REST resource configuration.
+   */
+  public function providerBasicDependencies() {
+    return [
+      'method' => [
+        [
+          'plugin_id' => 'entity:entity_test',
+          'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
+          'configuration' => [
+            'GET' => [
+              'supported_auth' => ['cookie'],
+              'supported_formats' => ['json'],
+            ],
+            'POST' => [
+              'supported_auth' => ['basic_auth'],
+              'supported_formats' => ['hal_json'],
+            ],
+          ],
+        ],
       ],
-      'POST' => [
-        'supported_auth' => ['basic_auth'],
-        'supported_formats' => ['hal_json'],
+      'resource' => [
+        [
+          'plugin_id' => 'entity:entity_test',
+          'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
+          'configuration' => [
+            'methods' => ['GET', 'POST'],
+            'formats' => ['json', 'hal_json'],
+            'authentication' => ['cookie', 'basic_auth'],
+          ],
+        ],
       ],
-    ], $rest_config->get('configuration'));
+    ];
   }
 
   /**
    * @covers ::onDependencyRemoval
-   * @covers ::calculateDependenciesForMethodGranularity
+   * @covers ::onDependencyRemovalForMethodGranularity
    */
-  public function testOnDependencyRemovalRemoveFormat() {
+  public function testOnDependencyRemovalRemoveFormatForMethodGranularity() {
     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
 
     $rest_config = RestResourceConfig::create([
@@ -120,7 +126,7 @@ public function testOnDependencyRemovalRemoveFormat() {
 
   /**
    * @covers ::onDependencyRemoval
-   * @covers ::calculateDependenciesForMethodGranularity
+   * @covers ::onDependencyRemovalForMethodGranularity
    */
   public function testOnDependencyRemovalRemoveAuth() {
     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
@@ -156,7 +162,7 @@ public function testOnDependencyRemovalRemoveAuth() {
 
   /**
    * @covers ::onDependencyRemoval
-   * @covers ::calculateDependenciesForMethodGranularity
+   * @covers ::onDependencyRemovalForMethodGranularity
    */
   public function testOnDependencyRemovalRemoveAuthAndFormats() {
     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
@@ -189,4 +195,95 @@ public function testOnDependencyRemovalRemoveAuthAndFormats() {
     ], $rest_config->get('configuration'));
   }
 
+  /**
+   * @covers ::onDependencyRemoval
+   * @covers ::onDependencyRemovalForResourceGranularity
+   *
+   * @dataProvider providerOnDependencyRemovalForResourceGranularity
+   */
+  public function testOnDependencyRemovalForResourceGranularity(array $configuration, $module, $expected_configuration) {
+    assert('is_string($module)');
+    assert('$expected_configuration === FALSE || is_array($expected_configuration)');
+    $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
+
+    $rest_config = RestResourceConfig::create($configuration);
+
+    $this->assertSame(!empty($expected_configuration), $config_dependencies->onDependencyRemoval($rest_config, ['module' => [$module]]));
+    if (!empty($expected_configuration)) {
+      $this->assertEquals($expected_configuration, $rest_config->get('configuration'));
+    }
+  }
+
+  /**
+   * @return array
+   *   An array with numerical keys:
+   *   0. The original REST resource configuration.
+   *   1. The module to uninstall (the dependency that is about to be removed).
+   *   2. The expected configuration after uninstalling this module.
+   */
+  public function providerOnDependencyRemovalForResourceGranularity() {
+    return [
+      'resource with multiple formats' => [
+        [
+          'plugin_id' => 'entity:entity_test',
+          'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
+          'configuration' => [
+            'methods' => ['GET', 'POST'],
+            'formats' => ['json', 'hal_json'],
+            'authentication' => ['cookie', 'basic_auth'],
+          ],
+        ],
+        'hal',
+        [
+          'methods' => ['GET', 'POST'],
+          'formats' => ['json'],
+          'authentication' => ['cookie', 'basic_auth'],
+        ]
+      ],
+      'resource with only HAL+JSON format' => [
+        [
+          'plugin_id' => 'entity:entity_test',
+          'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
+          'configuration' => [
+            'methods' => ['GET', 'POST'],
+            'formats' => ['hal_json'],
+            'authentication' => ['cookie', 'basic_auth'],
+          ],
+        ],
+        'hal',
+        FALSE
+      ],
+      'resource with multiple authentication providers' => [
+        [
+          'plugin_id' => 'entity:entity_test',
+          'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
+          'configuration' => [
+            'methods' => ['GET', 'POST'],
+            'formats' => ['json', 'hal_json'],
+            'authentication' => ['cookie', 'basic_auth'],
+          ],
+        ],
+        'basic_auth',
+        [
+          'methods' => ['GET', 'POST'],
+          'formats' => ['json', 'hal_json',],
+          'authentication' => ['cookie'],
+        ]
+      ],
+      'resource with only basic_auth authentication' => [
+        [
+          'plugin_id' => 'entity:entity_test',
+          'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
+          'configuration' => [
+            'methods' => ['GET', 'POST'],
+            'formats' => ['json', 'hal_json'],
+            'authentication' => ['basic_auth'],
+          ],
+        ],
+        'basic_auth',
+        FALSE,
+      ],
+    ];
+  }
+
 }
diff --git a/core/modules/system/src/Tests/System/ResponseGeneratorTest.php b/core/modules/system/src/Tests/System/ResponseGeneratorTest.php
index d0d22a2..d2f8f5f 100644
--- a/core/modules/system/src/Tests/System/ResponseGeneratorTest.php
+++ b/core/modules/system/src/Tests/System/ResponseGeneratorTest.php
@@ -53,13 +53,18 @@ function testGeneratorHeaderAdded() {
     $this->assertEqual('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
     $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
 
-    // Enable rest API for nodes
-    $this->enableService('entity:node', 'GET', 'json');
+    // Enable cookie-based authentication for the entity:node REST resource.
+    /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
+    $resource_config = $this->resourceConfigStorage->load('entity.node');
+    $configuration = $resource_config->get('configuration');
+    $configuration['authentication'][] = 'cookie';
+    $resource_config->set('configuration', $configuration)->save();
+    $this->rebuildCache();
 
     // Tests to see if this also works for a non-html request
-    $this->httpRequest($node->urlInfo()->setOption('query', ['_format' => 'json']), 'GET');
+    $this->httpRequest($node->urlInfo()->setOption('query', ['_format' => 'hal_json']), 'GET');
     $this->assertResponse(200);
-    $this->assertEqual('application/json', $this->drupalGetHeader('Content-Type'));
+    $this->assertEqual('application/hal+json', $this->drupalGetHeader('Content-Type'));
     $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
 
   }
