Issue #2824827 by damiankloip, dawehner, Wim Leers, klausi: \Drupal\hal\Normalizer\ContentEntityNormalizer::denormalize() fails with fatal PHP error when bundles are missing from link relation types

8.3.x
Alex Pott 2017-01-10 17:40:26 +00:00
parent cc30d65427
commit 35acba7aed
3 changed files with 40 additions and 7 deletions

View File

@ -215,11 +215,17 @@ class ContentEntityNormalizer extends NormalizerBase {
$types = array($types);
}
if (empty($types)) {
throw new UnexpectedValueException('No entity type(s) specified');
}
foreach ($types as $type) {
if (!isset($type['href'])) {
throw new UnexpectedValueException('Type must contain an \'href\' attribute.');
}
$type_uri = $type['href'];
// Check whether the URI corresponds to a known type on this site. Break
// once one does.
if ($typed_data_ids = $this->linkManager->getTypeInternalIds($type['href'], $context)) {

View File

@ -97,21 +97,18 @@ trait HalEntityNormalizationTrait {
if ($this->entity->getEntityType()->hasKey('bundle')) {
$normalization = $this->getNormalizedPostEntity();
// @todo Uncomment this in https://www.drupal.org/node/2824827.
// @codingStandardsIgnoreStart
/*
$normalization['_links']['type'] = Url::fromUri('base:rest/type/' . static::$entityTypeId . '/bad_bundle_name');
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
// DX: 400 when incorrect entity type bundle is specified.
$response = $this->request($method, $url, $request_options);
// @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813853.
// $this->assertResourceErrorResponse(400, 'The type link relation must be specified.', $response);
// $this->assertResourceErrorResponse(400, 'No entity type(s) specified', $response);
$this->assertSame(400, $response->getStatusCode());
$this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
$this->assertSame($this->serializer->encode(['error' => 'The type link relation must be specified.'], static::$format), (string) $response->getBody());
*/
// @codingStandardsIgnoreEnd
$this->assertSame($this->serializer->encode(['error' => 'No entity type(s) specified'], static::$format), (string) $response->getBody());
unset($normalization['_links']['type']);
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);

View File

@ -74,6 +74,36 @@ class DenormalizeTest extends NormalizerTestBase {
}
}
/**
* Tests link relation handling with an invalid type.
*/
public function testTypeHandlingWithInvalidType() {
$data_with_invalid_type = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', array('absolute' => TRUE))->toString(),
),
),
);
$this->setExpectedException(UnexpectedValueException::class);
$this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
}
/**
* Tests link relation handling with no types.
*/
public function testTypeHandlingWithNoTypes() {
$data_with_no_types = array(
'_links' => array(
'type' => array(),
),
);
$this->setExpectedException(UnexpectedValueException::class);
$this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format);
}
/**
* Test that a field set to an empty array is different than an absent field.
*/