Issue #2935738 by samuel.mortenson, Wim Leers, Berdir: Entity reference field subclasses (such as file and image fields) lose the non-default properties upon denormalization, in both hal_json and json
parent
8b388f07c2
commit
a46c46194d
|
@ -149,7 +149,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
|
||||||
$target_type = $field_definition->getSetting('target_type');
|
$target_type = $field_definition->getSetting('target_type');
|
||||||
$id = $this->entityResolver->resolve($this, $data, $target_type);
|
$id = $this->entityResolver->resolve($this, $data, $target_type);
|
||||||
if (isset($id)) {
|
if (isset($id)) {
|
||||||
return ['target_id' => $id];
|
return ['target_id' => $id] + array_intersect_key($data, $field_item->getProperties());
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -199,10 +199,6 @@ abstract class FileUploadResourceTestBase extends ResourceTestBase {
|
||||||
$response = $this->request('POST', $entity_test_post_url, $request_options);
|
$response = $this->request('POST', $entity_test_post_url, $request_options);
|
||||||
$this->assertResourceResponse(201, FALSE, $response);
|
$this->assertResourceResponse(201, FALSE, $response);
|
||||||
$this->assertTrue($this->fileStorage->loadUnchanged(1)->isPermanent());
|
$this->assertTrue($this->fileStorage->loadUnchanged(1)->isPermanent());
|
||||||
// @todo Remove this early return in https://www.drupal.org/project/drupal/issues/2935738.
|
|
||||||
if (static::$format === 'hal_json') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$this->assertSame([
|
$this->assertSame([
|
||||||
[
|
[
|
||||||
'target_id' => '1',
|
'target_id' => '1',
|
||||||
|
|
|
@ -78,7 +78,7 @@ class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
|
||||||
throw new UnexpectedValueException(sprintf('The field "%s" property "target_type" must be set to "%s" or omitted.', $field_item->getFieldDefinition()->getName(), $target_type));
|
throw new UnexpectedValueException(sprintf('The field "%s" property "target_type" must be set to "%s" or omitted.', $field_item->getFieldDefinition()->getName(), $target_type));
|
||||||
}
|
}
|
||||||
if ($entity = $this->entityRepository->loadEntityByUuid($target_type, $data['target_uuid'])) {
|
if ($entity = $this->entityRepository->loadEntityByUuid($target_type, $data['target_uuid'])) {
|
||||||
return ['target_id' => $entity->id()];
|
return ['target_id' => $entity->id()] + array_intersect_key($data, $field_item->getProperties());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Unable to load entity by uuid.
|
// Unable to load entity by uuid.
|
||||||
|
|
|
@ -4,12 +4,14 @@ namespace Drupal\Tests\serialization\Unit\Normalizer;
|
||||||
|
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
use Drupal\Core\Entity\EntityInterface;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||||
|
use Drupal\Core\TypedData\Type\IntegerInterface;
|
||||||
use Drupal\Core\TypedData\TypedDataInterface;
|
use Drupal\Core\TypedData\TypedDataInterface;
|
||||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||||
use Drupal\Core\Field\FieldItemInterface;
|
use Drupal\Core\Field\FieldItemInterface;
|
||||||
use Drupal\Core\Field\FieldItemListInterface;
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||||
|
use Drupal\locale\StringInterface;
|
||||||
use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer;
|
use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer;
|
||||||
use Drupal\Tests\UnitTestCase;
|
use Drupal\Tests\UnitTestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
|
@ -237,6 +239,9 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||||
->willReturn($entity)
|
->willReturn($entity)
|
||||||
->shouldBeCalled();
|
->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->fieldItem->getProperties()->willReturn([
|
||||||
|
'target_id' => $this->prophesize(IntegerInterface::class),
|
||||||
|
]);
|
||||||
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
|
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
|
||||||
|
|
||||||
$this->assertDenormalize($data);
|
$this->assertDenormalize($data);
|
||||||
|
@ -260,6 +265,9 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||||
->willReturn($entity)
|
->willReturn($entity)
|
||||||
->shouldBeCalled();
|
->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->fieldItem->getProperties()->willReturn([
|
||||||
|
'target_id' => $this->prophesize(IntegerInterface::class),
|
||||||
|
]);
|
||||||
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
|
$this->fieldItem->setValue(['target_id' => 'test'])->shouldBeCalled();
|
||||||
|
|
||||||
$this->assertDenormalize($data);
|
$this->assertDenormalize($data);
|
||||||
|
@ -361,4 +369,36 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase {
|
||||||
$this->assertSame($context['target_instance'], $denormalized);
|
$this->assertSame($context['target_instance'], $denormalized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::constructValue
|
||||||
|
*/
|
||||||
|
public function testConstructValueProperties() {
|
||||||
|
$data = [
|
||||||
|
'target_id' => 'test',
|
||||||
|
'target_type' => 'test_type',
|
||||||
|
'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
|
||||||
|
'extra_property' => 'extra_value',
|
||||||
|
];
|
||||||
|
|
||||||
|
$entity = $this->prophesize(FieldableEntityInterface::class);
|
||||||
|
$entity->id()
|
||||||
|
->willReturn('test')
|
||||||
|
->shouldBeCalled();
|
||||||
|
$this->entityRepository
|
||||||
|
->loadEntityByUuid($data['target_type'], $data['target_uuid'])
|
||||||
|
->willReturn($entity)
|
||||||
|
->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->fieldItem->getProperties()->willReturn([
|
||||||
|
'target_id' => $this->prophesize(IntegerInterface::class),
|
||||||
|
'extra_property' => $this->prophesize(StringInterface::class),
|
||||||
|
]);
|
||||||
|
$this->fieldItem->setValue([
|
||||||
|
'target_id' => 'test',
|
||||||
|
'extra_property' => 'extra_value',
|
||||||
|
])->shouldBeCalled();
|
||||||
|
|
||||||
|
$this->assertDenormalize($data);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue