Skip to content

MappingTransformer: cannot add a new property to a stdClass destination #244

Closed

Description

@njoubert-cleverage

Description

MappingTransformer cannot add a new property to a \stdClass destination (initial_value: new \stdClass(), or keep_input on a \stdClass input): it throws Property '...' is not writable. Only properties already defined on the object can be mapped.

This is consistent with the documentation wording ("the target is a writable property path of the destination"), but surprising for a \stdClass, and inconsistent with RecursivePropertySetterTransformer, which explicitly adds missing properties to \stdClass items.

Reproduction

$transformer->transform(['a' => 1], [
    'initial_value' => new \stdClass(),
    'mapping' => ['b' => ['code' => '[a]']],
]);
UnexpectedValueException: Property 'b' is not writable

With initial_value: (object) ['b' => null], the result is {"b":1}.

Expected: {"b":1} in both cases.

Reproduced on main (ad31c1f), PHP 8.5, Symfony 8.1, by calling the task / transformer directly. Found while adding the missing tests of #197.

Cause

MappingTransformer.php#L111: the PropertyAccessor does not consider a missing property of a \stdClass as writable, and only arrays have a fallback.

Proposed fix

Add a \stdClass fallback ($result->{$targetProperty} = $transformedValue for a simple property path), like the array one. Update mapping_transformer.md, add tests.

Requirements

  • Documentation updates
    • Reference
    • Cookbooks
    • Changelog
  • Unit tests

Breaking changes

None: this case currently throws.

Activity

  1. added a commit that references this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions