Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ObjectMapper] Add MappingAwareTransformCallableInterface#64220

Open
ousamabenyounes wants to merge 1 commit into
symfony:8.1from
ousamabenyounes:fix/issue-64161
Open

[ObjectMapper] Add MappingAwareTransformCallableInterface#64220
ousamabenyounes wants to merge 1 commit into
symfony:8.1from
ousamabenyounes:fix/issue-64161

Conversation

@ousamabenyounes
Copy link
Copy Markdown
Contributor

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

Adds MappingAwareTransformCallableInterface, an optional interface a transformer can implement to receive the Map metadata describing the property currently being mapped (target/source name, condition, transform).

Mirrors the existing ObjectMapperAwareInterface pattern. Pure addition — existing transformers and existing usage are unaffected.

namespace Symfony\Component\ObjectMapper;

use Symfony\Component\ObjectMapper\Attribute\Map;

interface MappingAwareTransformCallableInterface
{
    public function withMapping(Map $mapping): static;
}

In ObjectMapper::applyTransforms(), the transformer is enriched if it implements the interface, just like ObjectMapperAwareInterface:

if ($fn instanceof ObjectMapperAwareInterface) {
    $fn = $fn->withObjectMapper($this->objectMapper ?? $this);
}
if ($fn instanceof MappingAwareTransformCallableInterface) {
    $fn = $fn->withMapping($map);
}

Type-hinted as Map (the public attribute) rather than the internal Metadata\Mapping subclass so the contract stays inside the public API surface. Public fields (target, source, if, transform) are sufficient for the documented use cases.

RED/GREEN verification

Without the fix (ObjectMapper::applyTransforms() reverted to base on upstream/8.1):

Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'bar'
+'value'

FAILURES!
Tests: 1, Assertions: 2, Failures: 1, PHPUnit Warnings: 2.

With the fix (MappingAwareTransformCallableInterface hook restored):

Tests: 1, Assertions: 2, PHPUnit Warnings: 2.

The new testMappingAwareTransformCallable returns $mapping->target from the transformer; without the hook withMapping() is never called and the test sees the raw source value 'value' instead of 'bar'.

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.

[ObjectMapper] Add MappingAwareTransformCallableInterface to give transformers access to Mapping

2 participants