Commit 35acba7a authored by alexpott's avatar alexpott

Issue #2824827 by damiankloip, dawehner, Wim Leers, klausi:...

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
parent cc30d654
...@@ -215,11 +215,17 @@ protected function getTypedDataIds($types, $context = array()) { ...@@ -215,11 +215,17 @@ protected function getTypedDataIds($types, $context = array()) {
$types = array($types); $types = array($types);
} }
if (empty($types)) {
throw new UnexpectedValueException('No entity type(s) specified');
}
foreach ($types as $type) { foreach ($types as $type) {
if (!isset($type['href'])) { if (!isset($type['href'])) {
throw new UnexpectedValueException('Type must contain an \'href\' attribute.'); throw new UnexpectedValueException('Type must contain an \'href\' attribute.');
} }
$type_uri = $type['href']; $type_uri = $type['href'];
// Check whether the URI corresponds to a known type on this site. Break // Check whether the URI corresponds to a known type on this site. Break
// once one does. // once one does.
if ($typed_data_ids = $this->linkManager->getTypeInternalIds($type['href'], $context)) { if ($typed_data_ids = $this->linkManager->getTypeInternalIds($type['href'], $context)) {
......
...@@ -97,21 +97,18 @@ protected function assertNormalizationEdgeCases($method, Url $url, array $reques ...@@ -97,21 +97,18 @@ protected function assertNormalizationEdgeCases($method, Url $url, array $reques
if ($this->entity->getEntityType()->hasKey('bundle')) { if ($this->entity->getEntityType()->hasKey('bundle')) {
$normalization = $this->getNormalizedPostEntity(); $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'); $normalization['_links']['type'] = Url::fromUri('base:rest/type/' . static::$entityTypeId . '/bad_bundle_name');
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
// DX: 400 when incorrect entity type bundle is specified. // DX: 400 when incorrect entity type bundle is specified.
$response = $this->request($method, $url, $request_options); $response = $this->request($method, $url, $request_options);
// @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813853. // @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(400, $response->getStatusCode());
$this->assertSame([static::$mimeType], $response->getHeader('Content-Type')); $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()); $this->assertSame($this->serializer->encode(['error' => 'No entity type(s) specified'], static::$format), (string) $response->getBody());
*/
// @codingStandardsIgnoreEnd
unset($normalization['_links']['type']); unset($normalization['_links']['type']);
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
......
...@@ -74,6 +74,36 @@ public function testTypeHandling() { ...@@ -74,6 +74,36 @@ public function testTypeHandling() {
} }
} }
/**
* 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. * Test that a field set to an empty array is different than an absent field.
*/ */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment