Skip to content

Commit

Permalink
Merge pull request #2490 from ragboyjr/denormalize-input-class-with-d…
Browse files Browse the repository at this point in the history
…ifferent-fields

Fix Bug Preventing Denormalization of Input Classes with Different Fields
  • Loading branch information
dunglas authored Feb 3, 2019
2 parents e23c509 + 391e9df commit 1a9335e
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ public function supportsDenormalization($data, $type, $format = null)
public function denormalize($data, $class, $format = null, array $context = [])
{
$context['api_denormalize'] = true;
if (!isset($context['resource_class'])) {
$context['resource_class'] = $class;
}
$context['resource_class'] = $class;

return parent::denormalize($data, $class, $format, $context);
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Fixtures/TestBundle/Entity/DummyForAdditionalFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
* @ApiResource
* @ORM\Entity
*/
class DummyForAdditionalFields
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/** @ORM\Column */
private $name;
/** @ORM\Column */
private $slug;

public function __construct(string $name, string $slug)
{
$this->name = $name;
$this->slug = $slug;
}

public function getId(): ?int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function getSlug(): string
{
return $this->slug;
}
}
34 changes: 34 additions & 0 deletions tests/Fixtures/TestBundle/Entity/DummyForAdditionalFieldsInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource
*/
final class DummyForAdditionalFieldsInput
{
private $dummyName;

public function __construct(string $dummyName)
{
$this->dummyName = $dummyName;
}

public function getDummyName(): string
{
return $this->dummyName;
}
}
44 changes: 44 additions & 0 deletions tests/Serializer/AbstractItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyForAdditionalFields;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyForAdditionalFieldsInput;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritance;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyTableInheritanceChild;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
Expand Down Expand Up @@ -290,6 +292,48 @@ public function testDenormalize()
], Dummy::class);
}

public function testCanDenormalizeInputClassWithDifferentFieldsThanResourceClass()
{
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(DummyForAdditionalFieldsInput::class, [])->willReturn(
new PropertyNameCollection(['dummyName'])
);
$propertyNameCollectionFactoryProphecy->create(DummyForAdditionalFields::class, [])->willReturn(
new PropertyNameCollection(['id', 'name', 'slug'])
);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
// Create DummyForAdditionalFieldsInput mocks
$propertyMetadataFactoryProphecy->create(DummyForAdditionalFieldsInput::class, 'dummyName', [])->willReturn(
(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true, false))->withInitializable(true)
);
// Create DummyForAdditionalFields mocks
$propertyMetadataFactoryProphecy->create(DummyForAdditionalFields::class, 'id', [])->willReturn(
(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), '', true, false))->withInitializable(false)
);
$propertyMetadataFactoryProphecy->create(DummyForAdditionalFields::class, 'name', [])->willReturn(
(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true, false))->withInitializable(true)
);
$propertyMetadataFactoryProphecy->create(DummyForAdditionalFields::class, 'slug', [])->willReturn(
(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true, false))->withInitializable(true)
);

$normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $this->prophesize(IriConverterInterface::class)->reveal(), $this->prophesize(ResourceClassResolverInterface::class)->reveal()) extends AbstractItemNormalizer {
};

/** @var DummyForAdditionalFieldsInput $res */
$res = $normalizer->denormalize([
'dummyName' => 'Dummy Name',
], DummyForAdditionalFieldsInput::class, 'json', [
'resource_class' => DummyForAdditionalFields::class,
'input_class' => DummyForAdditionalFieldsInput::class,
'output_class' => DummyForAdditionalFields::class,
]);

$this->assertInstanceOf(DummyForAdditionalFieldsInput::class, $res);
$this->assertEquals('Dummy Name', $res->getDummyName());
}

public function testDenormalizeWritableLinks()
{
$relatedDummy1 = new RelatedDummy();
Expand Down

0 comments on commit 1a9335e

Please sign in to comment.