Skip to content

[FrameworkBundle][Serializer] Allow setting $objectClassResolver via configuration #60092

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

HypeMC
Copy link
Member

@HypeMC HypeMC commented Mar 31, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #58526
License MIT

Currently, the MaxDepth feature doesn't work correctly with Doctrine proxies because the isMaxDepthReached method relies on the object's class:

private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
{
if (!($enableMaxDepth = $context[self::ENABLE_MAX_DEPTH] ?? $this->defaultContext[self::ENABLE_MAX_DEPTH] ?? false)
|| !isset($attributesMetadata[$attribute]) || null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
) {
return false;
}
$key = \sprintf(self::DEPTH_KEY_PATTERN, $class, $attribute);
if (!isset($context[$key])) {
$context[$key] = 1;
return false;
}
if ($context[$key] === $maxDepth) {
return true;
}
++$context[$key];
return false;
}

This class is determined using get_class(), which returns a different result for proxy and non-proxy objects:

$this->objectClassResolver = ($objectClassResolver ?? 'get_class')(...);

Both the ObjectNormalizer and PropertyNormalizer have a $objectClassResolver property, but there is currently no way to configure it.

This PR introduces a new configuration option to set the $objectClassResolver. You can provide either a callable or a service:

Using a callable:

framework:
    serializer:
        object_class_resolver: [ 'Doctrine\ORM\Proxy\DefaultProxyClassNameResolver', 'getClass' ]

Using a service:

services:
    app.object_class_resolver:
        class: Closure
        from_callable: [ !service { class: 'Doctrine\ORM\Proxy\DefaultProxyClassNameResolver' }, 'getClass' ]

framework:
    serializer:
        object_class_resolver: app.object_class_resolver

@HypeMC HypeMC requested a review from dunglas as a code owner March 31, 2025 01:10
@carsonbot carsonbot added this to the 7.3 milestone Mar 31, 2025
@carsonbot carsonbot changed the title [Serializer][FrameworkBundle] Allow setting $objectClassResolver via configuration [FrameworkBundle][Serializer] Allow setting $objectClassResolver via configuration Mar 31, 2025
@HypeMC HypeMC force-pushed the allow-setting-object-class-resolver branch from f737ffb to b20009c Compare March 31, 2025 01:12
if (!($enableMaxDepth = $context[self::ENABLE_MAX_DEPTH] ?? $this->defaultContext[self::ENABLE_MAX_DEPTH] ?? false)
if (!($context[self::ENABLE_MAX_DEPTH] ?? $this->defaultContext[self::ENABLE_MAX_DEPTH] ?? false)
Copy link
Member Author

Choose a reason for hiding this comment

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

Not used.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

IIUC, this means everybody using the Serializer with Doctrine entities will need this, isn't it?
Shouldn't we patch DoctrineBundle instead so that there's nothing to configure on the user side?

@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serializer "max_depth" of 1 goes 2 levels deep when encountering a Doctrine Proxy class
4 participants