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

Add failing test case for constructor + readonly + list + constructor_arguments #240

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Mar 10, 2025

No description provided.

@joelwurtz
Copy link
Member

It's an intended behavior, see https://automapper.jolicode.com/latest/getting-started/configuration/ on constructStrategy

Adding

#[Mapper(constructorStrategy: ConstructorStrategy::ALWAYS)]

on top of class make the test work

By default, if all properties from the source are not present on the target constructor we build the class without invoking the constructor

Otherwise we generate it, this parameter force the generation of the constructor.

Maybe we could always generate it and use this behavior depending on a context value instead ?

@lyrixx
Copy link
Member Author

lyrixx commented Mar 10, 2025

By default, if all properties from the source are not present on the target constructor we build the class without invoking the constructor

I think this could confusion with the parameter constructor_arguments. I was expecting to seed some parameters the automapper could not find in the source.

That's being said, I understand what you explained. I do think this should be clarified in the documentation.

Maybe we could always generate it and use this behavior depending on a context value instead ?

I was thinking that indeed. But actually, automapper will never be able to construct my target based on the source. So I'll always need to repeat the context option. So now, I don't think it's useful anymore.

TL;DR: It may be only a doc issue.

@joelwurtz
Copy link
Member

I was thinking that indeed. But actually, automapper will never be able to construct my target based on the source. So I'll always need to repeat the context option. So now, I don't think it's useful anymore.

Basically we would generate both possibility of construction and if constructor arguments is present for this class we use the constructor (otherwise we don't)

Think it will work better.

The constructor strategy would still be present and would determine what's the default behavior when constructor arguments are not present in the context :

  • Never : will always use cached reflection (wether there is constructor arguments or not)
  • Auto :
    1. If constructor can be done with source properties : use it
    2. If constructor can not be done with source properties and constructor arguments present : use it
    3. If constructor can not be done with source properties and constructor arguments not present : do not use it
  • Always : always use constructor wether there is constructor arguments or not

@lyrixx
Copy link
Member Author

lyrixx commented Mar 10, 2025

It's up to you, but what is sure is I don't need this feature

@joelwurtz
Copy link
Member

It's up to you, but what is sure is I don't need this feature

Could be useful if there is code that can give those constructor arguments and other that cannot for the same mapping

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