Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(laravel): allow serializer attributes through ApiProperty #6680

Open
wants to merge 2 commits into
base: 4.0
Choose a base branch
from

Conversation

soyuka
Copy link
Member

@soyuka soyuka commented Sep 27, 2024

Q A
Branch? 4.0 (new metadata feature but a bug fix for laravel)
License MIT
Doc PR required?
#[ApiResource(normalizationContext: ['groups' => ['read']])]
class Resource
{
    #[ApiProperty(serialize: [new Groups(['read'])])]
    public function relation(): Relation
    {
        return new Relation(id: 1, name: 'test');
    }
}

#[Groups(['read'])]
#[ApiResource(operations: [])]
class Relation
{
    public function __construct(public int $id, public string $name) {}
}
{
  "@context": "/api/contexts/SomeClass",
  "@id": "/api/some_classes/1",
  "@type": "SomeClass",
  "relation": {
    "@id": "/api/relations/1",
    "@type": "Relation",
    "id": 1,
    "name": "test"
  }
}

@soyuka soyuka force-pushed the fix/groups-2 branch 2 times, most recently from ed1ed40 to 6d39500 Compare September 27, 2024 16:30
* @param Type[] $builtinTypes
* @param string|null $uriTemplate (experimental) whether to return the subRessource collection IRI instead of an iterable of IRI
* @param string|null $property The property name
* @param array<int, Groups|SerializedName|SerializedPath|MaxDepth|Ignore|Context> $serialize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array<int, Groups|SerializedName|SerializedPath|MaxDepth|Ignore|Context> $serialize
* @param array<int, Context|DiscriminatorMap|Groups|Ignore|MaxDepth|SerializedName|SerializedPath> $serialize

(Context and DiscriminatorMap are missing, and sort in alphabetical order)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DiscriminatorMap is a class attribute only

}

/**
* @param array<int, Groups|SerializedName|SerializedPath|MaxDepth|Ignore|Context> $serialize
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array<int, Groups|SerializedName|SerializedPath|MaxDepth|Ignore|Context> $serialize
* @param array<int, Context|DiscriminatorMap|Groups|Ignore|MaxDepth|SerializedName|SerializedPath> $serialize

@@ -47,7 +47,8 @@ final class XmlPropertyAdapter implements PropertyAdapterInterface
'property',
];

private const EXCLUDE = ['policy'];
# Laravel only, we don't need xml support
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Laravel only, we don't need xml support
# Laravel only, we don't need XML support

$classGroups = [];
$classContextAnnotation = null;

// It's very weird to grab an eloquent's properties in that case as they're never serialized
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// It's very weird to grab an eloquent's properties in that case as they're never serialized
// It's very weird to grab Eloquent's properties in that case as they're never serialized

Comment on lines +113 to +127
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]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

match?

(I guess this comes from Symfony, if you want to keep the code as-is to facilitate backporting the code if it changes upstream it's ok too, but please add a comment linking to the original source in this case)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants