Problem/Motivation
The link manager service allows to set a specific link domain by calling the method 'ConfigurableLinkManagerInterface::setLinkDomain($domain)'.
On the other hand, the type link manager manages a cache of type links present in the system. Each type link is identified by a URI that is based on the link domain setted at the moment that the cache is written.
If the link domain name changes by calling setLinkDomain, the link type cache remains as it was.
Steps to reproduce
- fresh install of 8.5-dev Drupal with standard profile
- enable the HAL module. Will ask to enable serialization as well by dependency. Also enable the 'rest' module needed as a new dependency, see [#1155816]
- download and enable the devel module
- try at /devel/php this working-well PHP snippet, that serializes and un-serializes a node:
$node = \Drupal\node\Entity\Node::create([ 'type' => 'page', 'title' => 'Test node', ]); $serialized_node = \Drupal::service('serializer')->serialize($node, 'hal_json'); $new_node = \Drupal::service('serializer')->deserialize($serialized_node, 'Drupal\node\Entity\Node', 'hal_json'); echo $new_node->get('title')->getString(); - at the same place, replace previous test code by this one that does the same but changing the link domain, it causes a "Type http://example.com/rest/type/node/page does not correspond to an entity on this site." exception:
\Drupal::service('hal.link_manager')->setLinkDomain('http://example.com'); $node = \Drupal\node\Entity\Node::create([ 'type' => 'page', 'title' => 'Test node', ]); $serialized_node = \Drupal::service('serializer')->serialize($node, 'hal_json'); $new_node = \Drupal::service('serializer')->deserialize($serialized_node, 'Drupal\node\Entity\Node', 'hal_json'); echo $new_node->get('title')->getString(); \Drupal::service('hal.link_manager')->setLinkDomain('');
Rebuilding the cache and retrying the last step will work, because the type links cache is write again with the setted link domain.
Note that it only affects the de-serialization process, in serialization the last setted link domain is always used.
Proposed resolution
- Clear the type links cache when a new link domain is set.
- Write the type link by absolute URI (with no domain) and add it in getters.
Remaining tasks
Notify about it in docs (until solved) to module developers that makes usage of serialization. Currently, any module that changes the link domain should clear the "hal:links:types" cache to avoid this issue and also prevent malfunctioning of any other module that works with HAL, since type links cache remains with unexpected URI.
API changes
Depending on how it is solved, the API could be affected.

