Skip to content

test(doctrine): uninitialized typed properties in PersistProcessor#7736

Open
Copilot wants to merge 4 commits into4.2from
copilot/fix-handle-lazy-object-relations
Open

test(doctrine): uninitialized typed properties in PersistProcessor#7736
Copilot wants to merge 4 commits into4.2from
copilot/fix-handle-lazy-object-relations

Conversation

Copy link
Contributor

Copilot AI commented Feb 6, 2026

This PR adds a functional test to verify the fix for issue #7735, which was already implemented in PR #7738. The fix prevents fatal errors when PersistProcessor::handleLazyObjectRelations() attempts to access uninitialized typed properties in entities that use Doctrine lifecycle callbacks.

Background

PersistProcessor::handleLazyObjectRelations() was calling ReflectionProperty::getValue() without checking initialization state, causing fatal errors when entities use typed properties initialized in Doctrine lifecycle callbacks. This occurs before persist() executes, so @PrePersist callbacks haven't run yet.

The fix (adding an isInitialized() guard before getValue()) was merged in PR #7738.

Changes

Example

#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
class MyEntity
{
    #[ORM\Column]
    private string $generatedValue; // Uninitialized - causes fatal on getValue()

    #[ORM\PrePersist]
    public function prePersist(): void
    {
        $this->generatedValue = 'generated_' . uniqid();
    }
}

Previously this crashed with "Typed property must not be accessed before initialization". Now the property is skipped until after persistence when it's initialized.

Original prompt

Fix issue #7735 on branch 4.2.

Context:

  • PersistProcessor::handleLazyObjectRelations() iterates entity properties and calls ReflectionProperty::getValue() without checking ReflectionProperty::isInitialized().
  • When entities use typed properties that are only initialized in Doctrine @PrePersist callbacks, handleLazyObjectRelations() runs before EntityManager::persist(), so properties may be uninitialized and PHP fatals: Typed property ... must not be accessed before initialization.
  • This affects ObjectMapper-enabled operations where $operation->canMap() is true.

Required changes:

  1. In PersistProcessor::handleLazyObjectRelations(), add a guard:

    • If !$reflectionProperty->isInitialized($data), skip/continue before calling getValue().
    • Keep existing behavior for identifiers and non-object values.
  2. Read and follow repository instructions in AGENTS.md (note: file is uppercased).

  3. Add a functional test under tests/Functional following existing conventions.

    • The test should reproduce the crash scenario: a Doctrine entity with typed properties that are not initialized until @PrePersist.
    • The resource should be ObjectMapper-enabled (Map usage) so canMap() is true.
    • Perform a POST request that previously triggered the fatal error.
    • Assert the request succeeds (e.g., 201) and that the response is valid (at minimum contains created resource data). Ensure the test would fail on current code (fatal) and pass with the fix.
  4. Open a pull request against api-platform/core with base branch 4.2.

Notes:

  • Current user login is soyuka.
  • Ensure code style and test conventions match the repo.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: soyuka <1321971+soyuka@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix uninitialized typed properties in PersistProcessor Fix fatal error on uninitialized typed properties in PersistProcessor Feb 6, 2026
Copilot AI requested a review from soyuka February 6, 2026 16:00
Copy link
Member

@soyuka soyuka left a comment

Choose a reason for hiding this comment

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

#7738 got merged rebase and keep the functional test.

Co-authored-by: soyuka <1321971+soyuka@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 6, 2026

#7738 got merged rebase and keep the functional test.

Rebased on 4.2 (commit 4f307d3). The functional test is preserved in commit cf31875.

Copilot AI changed the title Fix fatal error on uninitialized typed properties in PersistProcessor Add functional test for uninitialized typed properties in PersistProcessor Feb 6, 2026
Copilot AI requested a review from soyuka February 6, 2026 20:02
@soyuka soyuka changed the title Add functional test for uninitialized typed properties in PersistProcessor test(doctrine): uninitialized typed properties in PersistProcessor Feb 8, 2026
@soyuka soyuka marked this pull request as ready for review February 8, 2026 13:38
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

Successfully merging this pull request may close these issues.

2 participants