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

Readd getter for MetadataFactory in Serialzier #1439

Open
tgaertner opened this issue Nov 28, 2022 · 2 comments
Open

Readd getter for MetadataFactory in Serialzier #1439

tgaertner opened this issue Nov 28, 2022 · 2 comments

Comments

@tgaertner
Copy link

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no

Re-add getter for MetadataFactory in Serialzier to make it possible again to override metadata types like this:
/** @var \JMS\Serializer\Metadata\PropertyMetadata $itemsMetadata */
$itemsMetadata = $this->serializer->getMetadataFactory()->getMetadataForClass(Content::class)->propertyMetadata['items'];
$itemsMetadata->setType($type);

Expected Result

  • This code worked in version 1.x and breaks ins version (2.x) 3.x

Actual Result

  • Found no other way to achieve the desired type override.
@scyzoryck
Copy link
Collaborator

Hi @tgaertner!
This usage seems to be a bit hacky to override the property types on the Factory level and might impact performance & Could you provide more context what is a use case for you? May I will be better to help you with some better solution :)

@tgaertner
Copy link
Author

Well this is some legacy code, I haven't written. The full function looks like this:

/**
     * @param string $embeddedType
     * @return string
     */
    protected function getPagerType(string $embeddedType)
    {
        $type = [
            'name' => 'array',
            'params' => [
                [
                    'name' => $embeddedType,
                    'params' => [],
                ],
            ],

        ];

        // Overrides Configuration: Vendor.Package.Persistence.HATEOAS.Content.yml
        /** @var PropertyMetadata $itemsMetadata */
        $itemsMetadata = $this->serializer->getMetadataFactory()->getMetadataForClass(Content::class)->propertyMetadata['items'];
        $itemsMetadata->setType($type);

        return Pager::class;
    }

This function is only called to override the type for embedded Objects to serialize.

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

No branches or pull requests

2 participants