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

Special '*' serialization group #6820

Open
SergeyCherenkov opened this issue Nov 25, 2024 · 2 comments
Open

Special '*' serialization group #6820

SergeyCherenkov opened this issue Nov 25, 2024 · 2 comments

Comments

@SergeyCherenkov
Copy link

Description

The SymfonySerializer has a special group * that allows you to serialize all fields.

symfony/symfony#33540
symfony/symfony#32622
symfony/symfony@de58759

The SerializerPropertyMetadataFactory treats it as a regular group.

namespace ApiPlatform\Metadata\Property\Factory;

final class SerializerPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
    // ...
    private function transformReadWrite(ApiProperty $propertyMetadata, string $resourceClass, string $propertyName, ?array $normalizationGroups = null, ?array $denormalizationGroups = null): ApiProperty
    {
        $serializerAttributeMetadata = $this->getSerializerAttributeMetadata($resourceClass, $propertyName);
        $groups = $serializerAttributeMetadata ? $serializerAttributeMetadata->getGroups() : [];
        $ignored = $serializerAttributeMetadata && $serializerAttributeMetadata->isIgnored();

        if (false !== $propertyMetadata->isReadable()) {
            $propertyMetadata = $propertyMetadata->withReadable(!$ignored && (null === $normalizationGroups || array_intersect($normalizationGroups, $groups)));
        }

        if (false !== $propertyMetadata->isWritable()) {
            $propertyMetadata = $propertyMetadata->withWritable(!$ignored && (null === $denormalizationGroups || array_intersect($denormalizationGroups, $groups)));
        }

        return $propertyMetadata;
    }
    // ...
}

Wouldn't it be correct to add handling for this group in API Platform?

@SergeyCherenkov
Copy link
Author

I propose the following solution (I think it's also applicable for denormalization).

namespace ApiPlatform\Metadata\Property\Factory;

final class SerializerPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
    // ...
    private function transformReadWrite(ApiProperty $propertyMetadata, string $resourceClass, string $propertyName, ?array $normalizationGroups = null, ?array $denormalizationGroups = null): ApiProperty
    {
        $serializerAttributeMetadata = $this->getSerializerAttributeMetadata($resourceClass, $propertyName);
        $groups = $serializerAttributeMetadata ? $serializerAttributeMetadata->getGroups() : [];
        $groups = array_merge($groups, ['*']); // new line

        $ignored = $serializerAttributeMetadata && $serializerAttributeMetadata->isIgnored();

        if (false !== $propertyMetadata->isReadable()) {
            $propertyMetadata = $propertyMetadata->withReadable(!$ignored && (null === $normalizationGroups || array_intersect($normalizationGroups, $groups)));
        }

        if (false !== $propertyMetadata->isWritable()) {
            $propertyMetadata = $propertyMetadata->withWritable(!$ignored && (null === $denormalizationGroups || array_intersect($denormalizationGroups, $groups)));
        }

        return $propertyMetadata;
    }
    // ...
}

If this is a correct solution, I can create a PR.

@soyuka
Copy link
Member

soyuka commented Dec 16, 2024

Shouldn't the * group be opt-in? What behavior should change in our code? I guess that this makes properties readable/writable if they've a group? poke @dunglas

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