Skip to content
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

fix: fix doctrine collection with union/intersection type #847

Merged
merged 3 commits into from
Mar 14, 2025

Conversation

mdeboer
Copy link
Contributor

@mdeboer mdeboer commented Mar 13, 2025

This PR fixes an issue introduced in v2.3.2 where hydrating doctrine collections with a union or intersection type failed. These types were not recognized as doctrine collections and therefore they were assigned as array instead of ArrayCollection.

@mdeboer mdeboer changed the title [2.3.x] Fix doctrine collection with union/intersection type fix: fix doctrine collection with union/intersection type Mar 13, 2025
@nikophil
Copy link
Member

Hi @mdeboer

thanks for this fix!

could you please add a non regression test, please? Don't hesitate if you need some hints about the testsuite

@mdeboer mdeboer force-pushed the bug/mdb-fix-doctrine-collection branch from 3bad1cd to 1c3c024 Compare March 13, 2025 15:53
@mdeboer
Copy link
Contributor Author

mdeboer commented Mar 13, 2025

Hi @mdeboer

thanks for this fix!

could you please add a non regression test, please? Don't hesitate if you need some hints about the testsuite

@nikophil I am fairly new with the project itself and I see that the Hydrator class is not covered by any tests at all (as far as I can tell).

I don't have that much time to spend unfortunately, but I can add a few basic unit tests to that cover my changes?

@nikophil
Copy link
Member

could you please provide an example of failing code here please? so I can understand the problem better, I'll write the test myself

@mdeboer
Copy link
Contributor Author

mdeboer commented Mar 13, 2025

Yeah I'm writing some basic unit tests for the Hydrator as the moment, they will clarify I think.

But for instance, hydrating this would work in v2.3.1 but fails in v2.3.2+:

<?php

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;

class Foo {
    public Collection&Selectable $foo;

    __construct() {
        $this->foo = new ArrayCollection();
    }
}

This is because Foo::$foo is a union type here. In v2.3.2+ the hydrator fails to see Foo::$foo as a doctrine collection as it only checks if the type of that property is a \ReflectionNamedType, which is not true, because it is a \ReflectionUnionType.

@nikophil
Copy link
Member

ok thanks! If you have some time to write a unit test for the hydrator, that's super nice!

I'll create an integration test anyway.

it only checks if the type of that property is a \ReflectionNamedType, which is not true, because it is a \ReflectionUnionType.

yeah that's totally true. I wish I could use component type info for this, this would have prevented this I guess

@mdeboer
Copy link
Contributor Author

mdeboer commented Mar 13, 2025

@nikophil I have added some unit tests for the Hydrator that should prevent regressions. Hopefully they help explain what the problem was.

@nikophil
Copy link
Member

nikophil commented Mar 13, 2025

Pretty nice! could you rebase on 2.3.x please? so I can release this quickly

Also, I might make some changes on your PR before merging

@mdeboer
Copy link
Contributor Author

mdeboer commented Mar 13, 2025

@nikophil I already based it on 2.3.x, from what I see it is up to date 😅 Feel free to make any changes you like!

@nikophil nikophil force-pushed the bug/mdb-fix-doctrine-collection branch from 61ae401 to 179bdcb Compare March 14, 2025 13:24
@nikophil nikophil force-pushed the bug/mdb-fix-doctrine-collection branch from 179bdcb to 4aae90b Compare March 14, 2025 13:41
@nikophil nikophil merged commit 8356b53 into zenstruck:2.3.x Mar 14, 2025
131 of 132 checks passed
@nikophil
Copy link
Member

hi @mdeboer

your fix is released https://github.com/zenstruck/foundry/releases/tag/v2.3.8

@mdeboer
Copy link
Contributor Author

mdeboer commented Mar 14, 2025

Thank you so much! Appreciate the quick response 😁 Keep up the good work!

@mdeboer mdeboer deleted the bug/mdb-fix-doctrine-collection branch March 18, 2025 10:01
nikophil added a commit to nikophil/foundry that referenced this pull request Mar 31, 2025
* 2.3.x:
  bot: fix cs [skip ci]
  fix: handle "inverse one to one" without "placeholder" solution (zenstruck#855)
  bot: fix cs [skip ci]
  feat: add force() helper (zenstruck#854)
  chore: add support for doctrine/persistence 4 (zenstruck#852)
  fix: fix doctrine collection with union/intersection type (zenstruck#847)
  bot: fix cs [skip ci]
  fix: bug with factory collectin of persistent factory in unit test (zenstruck#842)
  fix: use Doctrine metadata event when persist is disabled (zenstruck#841)
  minor: add parameter "withAutoRefresh" to unproxy() function (zenstruck#840)
  chore: upgrade also dama when upgrading phpunit version (zenstruck#839)
  bot: fix cs [skip ci]
  changelog: update [skip ci]
  fix: can call ->create() in after persist callback (zenstruck#833)
@nikophil nikophil mentioned this pull request Mar 31, 2025
nikophil added a commit that referenced this pull request Mar 31, 2025
* fix: can call ->create() in after persist callback (#833)

* changelog: update [skip ci]

* bot: fix cs [skip ci]

* chore: upgrade also dama when upgrading phpunit version (#839)

* minor: add parameter "withAutoRefresh" to unproxy() function (#840)

* fix: use Doctrine metadata event when persist is disabled (#841)

* fix: bug with factory collectin of persistent factory in unit test (#842)

* bot: fix cs [skip ci]

* fix: fix doctrine collection with union/intersection type (#847)

* fix: fix hydrator with doctrine collections using intersection types

* feat: add unit tests for hydrator

* test: add functional test for union type

---------

Co-authored-by: Nicolas PHILIPPE <[email protected]>

* chore: add support for doctrine/persistence 4 (#852)

* feat: add force() helper (#854)

* bot: fix cs [skip ci]

* fix: handle "inverse one to one" without "placeholder" solution (#855)

* fix: one to one no more uses placeholder

* fix: use different error message if types are missing

* bot: fix cs [skip ci]

* changelog: update

---------

Co-authored-by: nikophil <[email protected]>
Co-authored-by: Maarten de Boer <[email protected]>
Co-authored-by: chris <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants