Revert "Issue #3175884 by gabesullice, mglaman, juagarc4: JSON:API link keys can collide"

This reverts commit e239d3e3c2.
merge-requests/25/head
Alex Pott 2020-10-27 15:07:04 +00:00
parent aba38bc0b2
commit e852eaf91d
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 1 additions and 84 deletions

View File

@ -94,14 +94,7 @@ class LinkCollectionNormalizer extends NormalizerBase {
if (!$this->hashSalt) {
$this->hashSalt = Crypt::randomBytesBase64();
}
$link_parameters = [
'href' => $link->getHref(),
] + $link->getTargetAttributes();
foreach ($link_parameters as $name => $value) {
$serialized_parameters[] = sprintf('%s="%s"', $name, implode(' ', (array) $value));
}
$b64_hash = Crypt::hashBase64($this->hashSalt . implode('; ', $serialized_parameters));
return substr(str_replace(['-', '_'], '', $b64_hash), 0, 7);
return substr(str_replace(['-', '_'], '', Crypt::hashBase64($this->hashSalt . $link->getHref())), 0, 7);
}
}

View File

@ -1,76 +0,0 @@
<?php
namespace Drupal\Tests\jsonapi\Kernel\Normalizer;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Url;
use Drupal\jsonapi\JsonApiResource\Link;
use Drupal\jsonapi\JsonApiResource\LinkCollection;
use Drupal\jsonapi\JsonApiResource\ResourceObject;
use Drupal\jsonapi\Normalizer\LinkCollectionNormalizer;
use Drupal\jsonapi\ResourceType\ResourceType;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\jsonapi\Normalizer\LinkCollectionNormalizer
* @group jsonapi
*
* @internal
*/
class LinkCollectionNormalizerTest extends KernelTestBase {
/**
* The subject under test.
*
* @var \Symfony\Component\Serializer\Normalizer\NormalizerInterface
*/
protected $normalizer;
/**
* {@inheritDoc}
*/
protected static $modules = [
'jsonapi',
'serialization',
];
/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();
$this->normalizer = new LinkCollectionNormalizer();
$this->normalizer->setSerializer($this->container->get('jsonapi.serializer'));
}
/**
* Tests the link collection normalizer.
*/
public function testNormalize() {
$link_context = new ResourceObject(new CacheableMetadata(), new ResourceType('n/a', 'n/a', 'n/a'), 'n/a', NULL, [], new LinkCollection([]));
$link_collection = (new LinkCollection([]))
->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', ['title' => 'Most viewed']))
->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', ['title' => 'Top rated']))
->withContext($link_context);
$normalized = $this->normalizer->normalize($link_collection)->getNormalization();
$this->assertIsArray($normalized);
foreach (array_keys($normalized) as $key) {
$this->assertStringStartsWith('related', $key);
}
$this->assertSame([
[
'href' => 'http://example.com/post/42',
'meta' => [
'title' => 'Most viewed',
],
],
[
'href' => 'http://example.com/post/42',
'meta' => [
'title' => 'Top rated',
],
],
], array_values($normalized));
}
}