Skip to content

Conversation

@mtarld
Copy link
Contributor

@mtarld mtarld commented Dec 22, 2024

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

When normalizing a property, the whole object is sometimes useful.

For example, in API Platform if we want to convert resource ID to an IRI, we'll have to use https://github.com/api-platform/core/blob/main/src/Metadata/IriConverterInterface.php#L46 which needs the whole resource to gather some data and create the IRI.

@chalasr
Copy link
Member

chalasr commented Dec 22, 2024

This rather complicates the normalizer API so allow me to ask: do we really want this conceptually? Do we already know about use cases other than the IRIConverter? If not, could it be solved otherwise in API Platform?

@mtarld
Copy link
Contributor Author

mtarld commented Dec 23, 2024

After rethinking it, I agree with you. This should be delegated to a precedent step (such as a mapping step). Having the object here will unexpectedly allow the user to change the object data during the normalization step.

@mtarld mtarld closed this Dec 23, 2024
nicolas-grekas added a commit that referenced this pull request Jul 24, 2025
…rmers (mtarld)

This PR was merged into the 7.4 branch.

Discussion
----------

[JsonStreamer] Provide current object to value transformers

| Q             | A
| ------------- | ---
| Branch?       | 7.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Issues        |
| License       | MIT

A new attempt to #59281.

As discussed with `@soyuka`, it is really important to have access to the current object during stream writing.
Without that, it'll for example be impossible for API Platform to generate resource IRIs (https://github.com/api-platform/core/blob/main/src/Metadata/IriConverterInterface.php#L46).

This PR makes is available in value transformers' `$context` under the `_current_object` key.

Commits
-------

e9dcd42 [JsonStreamer] Allow to access current object when writing
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.

3 participants