Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions features/hal/table_inheritance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Feature: Table inheritance
"href": "/dummy_table_inheritances/2"
}
},
"swaggerThanParent": true,
"id": 2,
"name": "Foobarbaz inheritance"
}
Expand Down
3 changes: 3 additions & 0 deletions features/main/table_inheritance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ Feature: Table inheritance
"type": "string",
"pattern": "^single item$"
},
"bar": {
"type": ["string", "null"]
},
"fooz": {
"type": "string",
"pattern": "fooz"
Expand Down
8 changes: 4 additions & 4 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,17 @@ protected function extractAttributes(object $object, ?string $format = null, arr
*/
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
{
if (!$this->resourceClassResolver->isResourceClass($context['resource_class'])) {
$contextResourceClass = $context['resource_class'];
if (!$this->resourceClassResolver->isResourceClass($contextResourceClass)) {
return parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
}

$resourceClass = $this->resourceClassResolver->getResourceClass(null, $context['resource_class']); // fix for abstract classes and interfaces
$options = $this->getFactoryOptions($context);
$propertyNames = $this->propertyNameCollectionFactory->create($resourceClass, $options);
$propertyNames = $this->propertyNameCollectionFactory->create($contextResourceClass, $options);

$allowedAttributes = [];
foreach ($propertyNames as $propertyName) {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $options);
$propertyMetadata = $this->propertyMetadataFactory->create($contextResourceClass, $propertyName, $options);

if (
$this->isAllowedAttribute($classOrObject, $propertyName, null, $context)
Expand Down
169 changes: 169 additions & 0 deletions src/Serializer/Tests/AbstractItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\PropertyCollectionIriOnlyRelation;
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\RelatedDummy;
use ApiPlatform\Serializer\Tests\Fixtures\ApiResource\SecuredDummy;
use ApiPlatform\Serializer\Tests\Fixtures\Polymorphism\ApiResource\Author;
use ApiPlatform\Serializer\Tests\Fixtures\Polymorphism\ApiResource\Book;
use ApiPlatform\Serializer\Tests\Fixtures\Polymorphism\Entity\FictionBook;
use ApiPlatform\Serializer\Tests\Fixtures\Polymorphism\Entity\TechnicalBook;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -1979,6 +1983,171 @@ public function testDenormalizeReportsAllMissingConstructorArguments(): void
$this->assertSame(['title', 'rating', 'comment'], $e->getMissingConstructorArguments());
}
}

public function testNormalizePolymorphicFictionBook(): void
{
$author = new Author();
$author->setName('author name');

$fictionBook = new FictionBook();
$fictionBook->setTitle('The Hobbit');
$fictionBook->setAuthor($author);
$fictionBook->setIsbn('978-0-345-33312-1');
$fictionBook->setGenre('Fantasy');
$fictionBook->setPageCount(310);

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(FictionBook::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['title', 'author', 'isbn', 'bookType', 'genre', 'pageCount']));

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);

if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
$stringType = new LegacyType(LegacyType::BUILTIN_TYPE_STRING);
$intType = new LegacyType(LegacyType::BUILTIN_TYPE_INT);
$authorType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, Author::class);

$propertyMetadataFactoryProphecy->create(FictionBook::class, 'title', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'author', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$authorType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'isbn', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'bookType', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(false));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'genre', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'pageCount', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$intType])->withReadable(true)->withWritable(true));
} else {
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'title', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'author', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::object(Author::class))->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'isbn', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'bookType', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(false));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'genre', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(FictionBook::class, 'pageCount', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::int())->withReadable(true)->withWritable(true));
}

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromResource($fictionBook, Argument::cetera())->willReturn('/books/1');
$iriConverterProphecy->getIriFromResource($author, Argument::cetera())->willReturn('/authors/1');

$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
$propertyAccessorProphecy->getValue($fictionBook, 'title')->willReturn('The Hobbit');
$propertyAccessorProphecy->getValue($fictionBook, 'author')->willReturn($author);
$propertyAccessorProphecy->getValue($fictionBook, 'isbn')->willReturn('978-0-345-33312-1');
$propertyAccessorProphecy->getValue($fictionBook, 'bookType')->willReturn('fiction');
$propertyAccessorProphecy->getValue($fictionBook, 'genre')->willReturn('Fantasy');
$propertyAccessorProphecy->getValue($fictionBook, 'pageCount')->willReturn(310);

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass(null, FictionBook::class)->willReturn(Book::class);
$resourceClassResolverProphecy->getResourceClass($fictionBook, null)->willReturn(FictionBook::class);
$resourceClassResolverProphecy->getResourceClass($author, Author::class)->willReturn(Author::class);
$resourceClassResolverProphecy->isResourceClass(FictionBook::class)->willReturn(true);
$resourceClassResolverProphecy->isResourceClass(Author::class)->willReturn(true);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);
$serializerProphecy->normalize('The Hobbit', null, Argument::type('array'))->willReturn('The Hobbit');
$serializerProphecy->normalize('/authors/1', null, Argument::type('array'))->willReturn('/authors/1');
$serializerProphecy->normalize('978-0-345-33312-1', null, Argument::type('array'))->willReturn('978-0-345-33312-1');
$serializerProphecy->normalize('fiction', null, Argument::type('array'))->willReturn('fiction');
$serializerProphecy->normalize('Fantasy', null, Argument::type('array'))->willReturn('Fantasy');
$serializerProphecy->normalize(310, null, Argument::type('array'))->willReturn(310);

$normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
$normalizer->setSerializer($serializerProphecy->reveal());

$expected = [
'title' => 'The Hobbit',
'author' => '/authors/1',
'isbn' => '978-0-345-33312-1',
'bookType' => 'fiction',
'genre' => 'Fantasy',
'pageCount' => 310,
];
$result = $normalizer->normalize($fictionBook, null, ['resources' => []]);
$this->assertSame($expected, $result);
}

public function testNormalizePolymorphicTechnicalBook(): void
{
$author = new Author();
$author->setName('author name');

$technicalBook = new TechnicalBook();
$technicalBook->setTitle('Design Patterns');
$technicalBook->setAuthor($author);
$technicalBook->setIsbn('978-0-201-63361-0');
$technicalBook->setProgrammingLanguage('C++');
$technicalBook->setDifficultyLevel('advanced');
$technicalBook->setTopic('Software Design');

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(TechnicalBook::class, Argument::type('array'))->willReturn(new PropertyNameCollection(['title', 'author', 'isbn', 'bookType', 'programmingLanguage', 'difficultyLevel', 'topic']));

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);

if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
$stringType = new LegacyType(LegacyType::BUILTIN_TYPE_STRING);
$authorType = new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, Author::class);

$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'title', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'author', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$authorType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'isbn', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'bookType', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(false));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'programmingLanguage', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'difficultyLevel', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'topic', Argument::type('array'))->willReturn((new ApiProperty())->withBuiltinTypes([$stringType])->withReadable(true)->withWritable(true));
} else {
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'title', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'author', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::object(Author::class))->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'isbn', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'bookType', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(false));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'programmingLanguage', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'difficultyLevel', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
$propertyMetadataFactoryProphecy->create(TechnicalBook::class, 'topic', Argument::type('array'))->willReturn((new ApiProperty())->withNativeType(Type::string())->withReadable(true)->withWritable(true));
}

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromResource($technicalBook, Argument::cetera())->willReturn('/books/2');
$iriConverterProphecy->getIriFromResource($author, Argument::cetera())->willReturn('/authors/1');

$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
$propertyAccessorProphecy->getValue($technicalBook, 'title')->willReturn('Design Patterns');
$propertyAccessorProphecy->getValue($technicalBook, 'author')->willReturn($author);
$propertyAccessorProphecy->getValue($technicalBook, 'isbn')->willReturn('978-0-201-63361-0');
$propertyAccessorProphecy->getValue($technicalBook, 'bookType')->willReturn('technical');
$propertyAccessorProphecy->getValue($technicalBook, 'programmingLanguage')->willReturn('C++');
$propertyAccessorProphecy->getValue($technicalBook, 'difficultyLevel')->willReturn('advanced');
$propertyAccessorProphecy->getValue($technicalBook, 'topic')->willReturn('Software Design');

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass(null, TechnicalBook::class)->willReturn(Book::class);
$resourceClassResolverProphecy->getResourceClass($technicalBook, null)->willReturn(TechnicalBook::class);
$resourceClassResolverProphecy->getResourceClass($author, Author::class)->willReturn(Author::class);
$resourceClassResolverProphecy->isResourceClass(TechnicalBook::class)->willReturn(true);
$resourceClassResolverProphecy->isResourceClass(Author::class)->willReturn(true);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);
$serializerProphecy->normalize('Design Patterns', null, Argument::type('array'))->willReturn('Design Patterns');
$serializerProphecy->normalize('/authors/1', null, Argument::type('array'))->willReturn('/authors/1');
$serializerProphecy->normalize('978-0-201-63361-0', null, Argument::type('array'))->willReturn('978-0-201-63361-0');
$serializerProphecy->normalize('technical', null, Argument::type('array'))->willReturn('technical');
$serializerProphecy->normalize('C++', null, Argument::type('array'))->willReturn('C++');
$serializerProphecy->normalize('advanced', null, Argument::type('array'))->willReturn('advanced');
$serializerProphecy->normalize('Software Design', null, Argument::type('array'))->willReturn('Software Design');

$normalizer = new class($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal(), null, null, [], null, null) extends AbstractItemNormalizer {};
$normalizer->setSerializer($serializerProphecy->reveal());

$expected = [
'title' => 'Design Patterns',
'author' => '/authors/1',
'isbn' => '978-0-201-63361-0',
'bookType' => 'technical',
'programmingLanguage' => 'C++',
'difficultyLevel' => 'advanced',
'topic' => 'Software Design',
];
$result = $normalizer->normalize($technicalBook, null, ['resources' => []]);
$this->assertSame($expected, $result);
}
}

class ObjectWithBasicProperties
Expand Down
52 changes: 52 additions & 0 deletions src/Serializer/Tests/Fixtures/Polymorphism/ApiResource/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Serializer\Tests\Fixtures\Polymorphism\ApiResource;

use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

#[ApiResource]
#[ORM\Entity]
class Author
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

#[ORM\Column(type: 'string', length: 255)]
private string $name;

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

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

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

public function setName(string $name): self
{
$this->name = $name;

return $this;
}
}
Loading
Loading