core icon indicating copy to clipboard operation
core copied to clipboard

Core composer.json should replace all sub packages to avoid ambiguous class resolution

Open ambroisemaupate opened this issue 1 year ago • 4 comments

How to reproduce

  • Require api-platform/core in main project
  • Main project requires a package which requires api-platform/metadata
Warning: Ambiguous class resolution, 
"ApiPlatform\Metadata\Resource\Factory\CachedResourceMetadataCollectionFactory" was found 2x: 
in "/xxxxx/vendor/api-platform/metadata/Resource/Factory/CachedResourceMetadataCollectionFactory.php" 
and "/xxxxx/vendor/api-platform/core/src/Metadata/Resource/Factory/CachedResourceMetadataCollectionFactory.php", 
the first will be used.

Possible resolution

Core package composer.json should have a replace section with all sub-packages splitted from core:

{
    "replace": {
        "api-platform/metadata": "*",
        "api-platform/state": "*",
        // etc
    }
}

That way, if core is loaded, composer will not autoload any sub-package.

ambroisemaupate avatar Mar 12 '24 21:03 ambroisemaupate

Hi, would you be able to check the v3.3.0-alpha.2? We removed these dependencies from the main composer.json.

soyuka avatar Mar 13 '24 08:03 soyuka

Hi, would you be able to check the v3.3.0-alpha.2? We removed these dependencies from the main composer.json.

I don't understand. Problem would be the same on v3.3.0-alpha.2.

If I create a project using api-pack, api-platform/core will be loaded into my project. Then, if my project depends on a library that relies only on api-platform/metadata, my project will have core and metadata packages autoloaded.

There are 2 solutions :

  1. api-platform/core composer.json declares a replace section, avoiding autoload any api-platform sub-packages
  2. or api-platform/api-pack does not load api-platform/core but all single api-platform packages. Thus, api-platform/core would become a dev-only package. The same way my project does not rely on symfony/symfony package, but all symfony/** sub-packages.

ambroisemaupate avatar Mar 21 '24 17:03 ambroisemaupate

Then, if my project depends on a library that relies only on api-platform/metadata, my project will have core and metadata packages autoloaded.

It should not have core at all.

or api-platform/api-pack does not load api-platform/core but all single api-platform packages. Thus, api-platform/core would become a dev-only package. The same way my project does not rely on symfony/symfony package, but all symfony/** sub-packages.

yes we need to do this

soyuka avatar Mar 21 '24 18:03 soyuka

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 20 '24 20:05 stale[bot]