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

Deprecate not setting doctrine.orm.controller_resolver.auto_mapping #1762

Conversation

bobvandevijver
Copy link
Contributor

As I have been bitten by this again in one of my projects, and I saw a related issue in the Symfony repository (symfony/symfony#50739 and a comment from @stof), I propose to change the default with the next major. The deprecation will only be triggered when not explicitly configured.

In the future, this will prevent new users from unexpected behaviour, but keeps the existing functionality available for anyone that wants to use this. And, with the new MapEntity attributes it would also be trivial to change the mapping configuration for a single controller argument, which is in my opinion preferable to have this wildcard match enabled globally.

If this is to be merged, the documentation on https://symfony.com/doc/current/doctrine.html#fetch-automatically needs to be updated as well. I can do that as well.

@bobvandevijver bobvandevijver force-pushed the change-controller-resolver-auto-mapping-default branch from 7c9b894 to 2b7ab54 Compare February 21, 2024 13:39
@ostrolucky ostrolucky changed the base branch from 2.11.x to 2.12.x February 21, 2024 14:13
@ostrolucky
Copy link
Member

I'll need a feedback from other maintainers here, I can't decide on this. I think this might be controversial.

@dmaicher
Copy link
Contributor

dmaicher commented Feb 21, 2024

It will however still be true for new projects then using the flex recipe

Regarding changing the default: I'm indifferent on this. I checked 2 of my work projects and disabled the option: nothing changed. Seems we are not relying on that auto mapping feature. We never experienced any issues with it being enabled either though.

@bobvandevijver
Copy link
Contributor Author

@dmaicher You are referring to the other auto_mapping option (yes, there are two options with the same name). This change is specifically for the controller resolver, not for the entity managers. The flex recipe does not define a default for controller resolver option.

In simple situations either option behaves the same. But there are more complex situations where the auto mapping can bite you unexpectedly. For example, if you have multiple parameters in your route where both should be mapped to an entity. Consider the following example:

#[Route('/event/{event<\d+>}/activity/{activity<\d+>}`)]
public function show(Event $event, Activity $activity): Response {
}

If you would try to access this for a non-existing Activity, you would expect a 404 as the activity with that id does not exist. However, due to the auto mapping feature, the resolver will use all other parameters to try find something that might match. So, if you have a relation with Event in your Activity, it would instead resolve the first found Activity with that event, and thus not come with the expected 404.

@dmaicher
Copy link
Contributor

@bobvandevijver ah of course 🤦‍♂️ I confused the two options. Sorry. I tested again and we are indeed relying on it 😅 A small number of tests failing when I disable it. They can be fixed by explicitly adding MapEntity with a mapping.

@stof WDYT?

Copy link
Contributor

@dmaicher dmaicher left a comment

Choose a reason for hiding this comment

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

I'm fine with this change as it can lead to really surprising behavior in some cases

@dmaicher dmaicher added this to the 2.12.0 milestone Mar 14, 2024
@ostrolucky
Copy link
Member

pls add UPGRADE-2.12.md file where this is documented too

@bobvandevijver
Copy link
Contributor Author

@ostrolucky Something like this?

@ostrolucky
Copy link
Member

yes that looks very good! thanks

@ostrolucky ostrolucky changed the title Change controller resolver auto mapping default configuration value Deprecate not setting doctrine.orm.controller_resolver.auto_mapping Mar 14, 2024
@ostrolucky ostrolucky merged commit 1a3b650 into doctrine:2.12.x Mar 14, 2024
14 checks passed
@bobvandevijver bobvandevijver deleted the change-controller-resolver-auto-mapping-default branch March 14, 2024 12:56
@dmaicher
Copy link
Contributor

Should we also adjust the recipe? So that new projects don't start with a deprecation?

@bobvandevijver
Copy link
Contributor Author

Seems reasonable to me: I'm currently in progress of updating the Symfony documentation, will look into the recipe after that 👍🏻

bobvandevijver added a commit to bobvandevijver/symfony-docs that referenced this pull request Mar 14, 2024
This reflects the changes introduced with
doctrine/DoctrineBundle#1762
and changes the documentation to instruct users to not rely on
the auto_mapping option as it can lead to surprising behaviour.

A change for the recipe is also incoming.
bobvandevijver added a commit to bobvandevijver/symfony-docs that referenced this pull request Mar 14, 2024
This reflects the changes introduced with
doctrine/DoctrineBundle#1762
and changes the documentation to instruct users to not rely on
the auto_mapping option as it can lead to surprising behaviour.

A change for the recipe is also incoming.
bobvandevijver added a commit to bobvandevijver/symfony-docs that referenced this pull request Mar 14, 2024
This reflects the changes introduced with
doctrine/DoctrineBundle#1762
and changes the documentation to instruct users to not rely on
the auto_mapping option as it can lead to surprising behaviour.

A change for the recipe is also incoming.
bobvandevijver added a commit to bobvandevijver/symfony-docs that referenced this pull request Mar 14, 2024
This reflects the changes introduced with
doctrine/DoctrineBundle#1762
and changes the documentation to instruct users to not rely on
the auto_mapping option as it can lead to surprising behaviour.

A change for the recipe is also incoming.
@stof
Copy link
Member

stof commented Mar 15, 2024

Totally in favor of this change. I've advocated for years that this feature should not exist at all because it is a total footgun. When introducing the argument value resolver to replace the DoctrineParamConverter of SensioFrameworkExtraBundle, I managed to make this feature configurable (it was not in DoctrineParamConverter) but I did not manage to make it disabled by default or non-existent.
Disabling this is literally one of the first things I'm doing when starting a new project, so I'm glad it is off by default.

@ostrolucky
Copy link
Member

Well it's not off by default yet, that will happen in next major version

@dmaicher
Copy link
Contributor

Well it's not off by default yet, that will happen in next major version

well at least with the recipe change its off by default for new projects with flex 😄

@bobvandevijver
Copy link
Contributor Author

Even with the recipe change it still requires DoctrineBundle 2.12 to be released first 😄

@dmaicher
Copy link
Contributor

@bobvandevijver I noticed that we now have those deprecations when running tests on 2.12.x

Remaining self deprecation notices (98)

  98x: Since doctrine/doctrine-bundle 2.12: The default value of "doctrine.orm.controller_resolver.auto_mapping" will be changed from `true` to `false`. Explicitly configure `true` to keep existing behaviour.
    6x in DoctrineExtensionTest::testCacheConfiguration from Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection
    3x in DoctrineExtensionTest::testAutomapping from Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection
    2x in DoctrineExtensionTest::testDependencyInjectionConfigurationDefaults from Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection
    2x in DoctrineExtensionTest::testSingleEntityManagerWithEmptyConfiguration from Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection
    2x in DoctrineExtensionTest::testControllerResolver from Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection

Would you have time by any chance to look into fixing those? If not I can also take a look.

@seb-jean
Copy link

seb-jean commented Mar 23, 2024

Hi,
Since the default value of setting doctrine.orm.controller_resolver.auto_mapping is false, I get the following error:

Cannot autowire argument $user of "App\Controller\ProfileController::show()": it needs an instance of "App\Entity\User" but this type has been excluded in "config/services.yaml".

My controller:

// src/Controller/ProfileController.php
namespace App\Controller;

use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ProfileController
{
    #[Route('/profile/{ulid}')]
    public function show(Request $request, User $user): Response
    {
        // ...
    }
}

@ostrolucky
Copy link
Member

Default value isn't false

@nicolas-grekas
Copy link
Member

I'm proposing to revert this change in symfony/recipes#1302: this breaks the DX, we need to find a better way.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Mar 27, 2024

To me this change doesn't make sense BTW: automapping is supposed to be a DX helper. If it is disabled by default, why would anyone want to re-enable it to get a clumsy behavior in the end.

The right way that'd be consistent with this change would be to remove automapping altogether.

BUT, this would still break DX, so that I'm NOT proposing to remove automapping.
I'd rather revert this change, and possibly improve automapping instead if we can improve the edge cases.

@bobvandevijver
Copy link
Contributor Author

For me the automapper was bad DX from the start, and you do not even need it for the most common scenarios. For those other specific scenarios you are always better of with the MapEntity attribute.

And if you do want to use it, you are free to do so. I have updated the documentation (which has not yet been merged) to make clear where it can be used for, and what bad DX you get in return.

In my opinion relying on entity property names in your routing parameters is bad design anyways (not refactor safe).

@ostrolucky
Copy link
Member

We did this change to fix the DX. I'm not sure about disabling this by default in recipe, but I think having to be explicit about it is better too. Sorry @nicolas-grekas but looks like it's 4v1. Deprecation stays.

asdfzdfj added a commit to MbinOrg/mbin that referenced this pull request Apr 29, 2024
it looks like doctrine bundle is about to deprecate
doctrine.orm.controller_resolver.auto_mapping and it's giving out
deprecation notice about this

explicitly set doctrine.orm.controller_resolver.auto_mapping: true as
suggested by deprecation log to keep existing behavior and remove the
notice

see doctrine/DoctrineBundle#1762 for more info
asdfzdfj added a commit to MbinOrg/mbin that referenced this pull request Apr 30, 2024
it looks like doctrine bundle is about to deprecate
doctrine.orm.controller_resolver.auto_mapping and it's giving out
deprecation notice about this

explicitly set doctrine.orm.controller_resolver.auto_mapping: true as
suggested by deprecation log to keep existing behavior and remove the
notice

see doctrine/DoctrineBundle#1762 for more info
asdfzdfj added a commit to MbinOrg/mbin that referenced this pull request Apr 30, 2024
it looks like doctrine bundle is about to deprecate controller auto mapping stuff,
and it's giving out deprecation notice about this

explicitly set doctrine.orm.controller_resolver.auto_mapping: true as suggested
by deprecation notice to keep existing behavior and remove the notice

see doctrine/DoctrineBundle#1762 for more info
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.

None yet

6 participants