-
-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(laravel): allow serializer attributes through ApiProperty
- Loading branch information
Showing
12 changed files
with
368 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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 Workbench\App\Models; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Symfony\Component\Serializer\Attribute\Groups; | ||
|
||
#[Groups(['read'])] | ||
#[ApiResource(operations: [])] | ||
class WithAccessorRelation extends Model | ||
{ | ||
use HasFactory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/Serializer/Mapping/Loader/PropertyMetadataLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?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\Mapping\Loader; | ||
|
||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Symfony\Component\Serializer\Attribute\Context; | ||
use Symfony\Component\Serializer\Attribute\DiscriminatorMap; | ||
use Symfony\Component\Serializer\Attribute\Groups; | ||
use Symfony\Component\Serializer\Attribute\Ignore; | ||
use Symfony\Component\Serializer\Attribute\MaxDepth; | ||
use Symfony\Component\Serializer\Attribute\SerializedName; | ||
use Symfony\Component\Serializer\Attribute\SerializedPath; | ||
use Symfony\Component\Serializer\Mapping\AttributeMetadata; | ||
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; | ||
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; | ||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface; | ||
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; | ||
|
||
/** | ||
* Loader for PHP attributes using ApiProperty. | ||
*/ | ||
final class PropertyMetadataLoader implements LoaderInterface | ||
{ | ||
public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory) | ||
{ | ||
} | ||
|
||
public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool | ||
{ | ||
$attributesMetadata = $classMetadata->getAttributesMetadata(); | ||
$refl = $classMetadata->getReflectionClass(); | ||
$attributes = []; | ||
$classGroups = []; | ||
$classContextAnnotation = null; | ||
|
||
// It's very weird to grab an eloquent's properties in that case as they're never serialized | ||
// the Serializer makes a call on the abstract class, let's save some unneeded work with a condition | ||
if (Model::class === $classMetadata->getName()) { | ||
return false; | ||
} | ||
|
||
foreach ($refl->getAttributes(ApiProperty::class) as $clAttr) { | ||
$this->addAttributeMetadata($clAttr->newInstance(), $attributes); | ||
} | ||
|
||
$attributesMetadata = $classMetadata->getAttributesMetadata(); | ||
|
||
foreach ($refl->getAttributes() as $a) { | ||
$attribute = $a->newInstance(); | ||
if ($attribute instanceof DiscriminatorMap) { | ||
$classMetadata->setClassDiscriminatorMapping(new ClassDiscriminatorMapping( | ||
$attribute->getTypeProperty(), | ||
$attribute->getMapping() | ||
)); | ||
continue; | ||
} | ||
|
||
if ($attribute instanceof Groups) { | ||
$classGroups = $attribute->getGroups(); | ||
|
||
continue; | ||
} | ||
|
||
if ($attribute instanceof Context) { | ||
$classContextAnnotation = $attribute; | ||
} | ||
} | ||
|
||
foreach ($refl->getProperties() as $reflProperty) { | ||
foreach ($reflProperty->getAttributes(ApiProperty::class) as $propAttr) { | ||
$this->addAttributeMetadata($propAttr->newInstance()->withProperty($reflProperty->name), $attributes); | ||
} | ||
} | ||
|
||
foreach ($refl->getMethods() as $reflMethod) { | ||
foreach ($reflMethod->getAttributes(ApiProperty::class) as $methodAttr) { | ||
$this->addAttributeMetadata($methodAttr->newInstance()->withProperty($reflMethod->getName()), $attributes); | ||
} | ||
} | ||
|
||
foreach ($this->propertyNameCollectionFactory->create($classMetadata->getName()) as $propertyName) { | ||
if (!isset($attributesMetadata[$propertyName])) { | ||
$attributesMetadata[$propertyName] = new AttributeMetadata($propertyName); | ||
$classMetadata->addAttributeMetadata($attributesMetadata[$propertyName]); | ||
} | ||
|
||
foreach ($classGroups as $group) { | ||
$attributesMetadata[$propertyName]->addGroup($group); | ||
} | ||
|
||
if ($classContextAnnotation) { | ||
$this->setAttributeContextsForGroups($classContextAnnotation, $attributesMetadata[$propertyName]); | ||
} | ||
|
||
if (!isset($attributes[$propertyName])) { | ||
continue; | ||
} | ||
|
||
foreach ($attributes[$propertyName] as $attr) { | ||
if ($attr instanceof Groups) { | ||
foreach ($attr->getGroups() as $group) { | ||
$attributesMetadata[$propertyName]->addGroup($group); | ||
} | ||
} elseif ($attr instanceof MaxDepth) { | ||
$attributesMetadata[$propertyName]->setMaxDepth($attr->getMaxDepth()); | ||
} elseif ($attr instanceof SerializedName) { | ||
$attributesMetadata[$propertyName]->setSerializedName($attr->getSerializedName()); | ||
} elseif ($attr instanceof SerializedPath) { | ||
$attributesMetadata[$propertyName]->setSerializedPath($attr->getSerializedPath()); | ||
} elseif ($attr instanceof Ignore) { | ||
$attributesMetadata[$propertyName]->setIgnore(true); | ||
} elseif ($attr instanceof Context) { | ||
$this->setAttributeContextsForGroups($attr, $attributesMetadata[$propertyName]); | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param ApiProperty[] $attributes | ||
*/ | ||
private function addAttributeMetadata(ApiProperty $attribute, array &$attributes): void | ||
{ | ||
if (($prop = $attribute->getProperty()) && ($value = $attribute->getSerialize())) { | ||
$attributes[$prop] = $value; | ||
} | ||
} | ||
|
||
private function setAttributeContextsForGroups(Context $annotation, AttributeMetadataInterface $attributeMetadata): void | ||
{ | ||
if ($annotation->getContext()) { | ||
$attributeMetadata->setNormalizationContextForGroups($annotation->getContext(), $annotation->getGroups()); | ||
$attributeMetadata->setDenormalizationContextForGroups($annotation->getContext(), $annotation->getGroups()); | ||
} | ||
|
||
if ($annotation->getNormalizationContext()) { | ||
$attributeMetadata->setNormalizationContextForGroups($annotation->getNormalizationContext(), $annotation->getGroups()); | ||
} | ||
|
||
if ($annotation->getDenormalizationContext()) { | ||
$attributeMetadata->setDenormalizationContextForGroups($annotation->getDenormalizationContext(), $annotation->getGroups()); | ||
} | ||
} | ||
} |
Oops, something went wrong.