automapper icon indicating copy to clipboard operation
automapper copied to clipboard

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

Open lyrixx opened this issue 9 months ago • 5 comments

lyrixx avatar Mar 10 '25 09:03 lyrixx

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 ?

joelwurtz avatar Mar 10 '25 09:03 joelwurtz

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.

lyrixx avatar Mar 10 '25 09:03 lyrixx

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

joelwurtz avatar Mar 10 '25 10:03 joelwurtz

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

lyrixx avatar Mar 10 '25 10:03 lyrixx

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

joelwurtz avatar Mar 10 '25 10:03 joelwurtz

I'm closing this, don't have the time to do that and even if the feature can be useful, i never needed it.

joelwurtz avatar Nov 29 '25 19:11 joelwurtz