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

AutoMapper tests are failing when using FEC #196

Closed
dadhi opened this issue May 30, 2019 · 5 comments
Closed

AutoMapper tests are failing when using FEC #196

dadhi opened this issue May 30, 2019 · 5 comments
Assignees
Labels
application The real-world use-case bug

Comments

@dadhi
Copy link
Owner

dadhi commented May 30, 2019

AutoMapper issue AutoMapper/AutoMapper#3103 (comment)

@dadhi dadhi self-assigned this May 30, 2019
@dadhi dadhi added application The real-world use-case bug labels May 30, 2019
@jbogard
Copy link

jbogard commented May 30, 2019

I found the issue here. Here's a failing expression:

var expression = Lambda<Func<Source, Dest, ResolutionContext, Dest>>(
    Block(
        Condition(
            Equal(srcParam, Constant(null)),
            Default(typeof(Dest)),
            Block(typeof(Dest), new[] {typeMapDestParam},
                Assign(typeMapDestParam, Coalesce(destParam, New(typeof(Dest).GetConstructors()[0]))),
                TryCatch(
                    /* Assign src.Value */
                    Block(resolvedValueParam,
                        Block(
                            Assign(resolvedValueParam,
                                Condition(Or(Equal(srcParam, Constant(null)), Constant(false)),
                                    Default(typeof(int)),
                                    Property(srcParam, "Value"))
                            ),
                            Assign(Property(typeMapDestParam, "Value"), resolvedValueParam)
                        )
                    ),
                    Catch(exceptionParameter, Block(
                        Throw(New(constructorInfo, 
                            Constant("Error mapping types."), 
                            exceptionParameter,
                            Constant(typeMap.Types),
                            Constant(typeMap),
                            Constant(memberMap)
                            )
                        ),
                        Default(typeof(int))
                    ))
                ),
                typeMapDestParam
            )
        )
    ),
    srcParam,
    destParam,
    Parameter(typeof(ResolutionContext), "ctxt")
);

Note that my catch block has a "return" value for the TryCatch expression. This is because both sides of the try/catch have to return the same type, and we cheated by having the "catch" block "return" a value of default(int).

The fix is to make the try block have a return type of void:

var expression = Lambda<Func<Source, Dest, ResolutionContext, Dest>>(
    Block(
        Condition(
            Equal(srcParam, Constant(null)),
            Default(typeof(Dest)),
            Block(typeof(Dest), new[] {typeMapDestParam},
                Assign(typeMapDestParam, Coalesce(destParam, New(typeof(Dest).GetConstructors()[0]))),
                TryCatch(
                    /* Assign src.Value */
                    Block(typeof(void), new[] {resolvedValueParam},
                        Block(
                            Assign(resolvedValueParam,
                                Condition(Or(Equal(srcParam, Constant(null)), Constant(false)),
                                    Default(typeof(int)),
                                    Property(srcParam, "Value"))
                            ),
                            Assign(Property(typeMapDestParam, "Value"), resolvedValueParam)
                        )
                    ),
                    Catch(exceptionParameter, 
                        Throw(New(constructorInfo, 
                            Constant("Error mapping types."), 
                            exceptionParameter,
                            Constant(typeMap.Types),
                            Constant(typeMap),
                            Constant(memberMap)
                            )
                        )
                    )
                ),
                typeMapDestParam
            )
        )
    ),
    srcParam,
    destParam,
    Parameter(typeof(ResolutionContext), "ctxt")
);

Which makes more sense, but the C# expression compiler handled this scenario fine.

Relevant StackOverflow question: https://stackoverflow.com/questions/14365122/expression-convert-within-expression-trycatch

dadhi added a commit that referenced this issue May 30, 2019
… and true constants singletons; TBD: look to try catch

upped language to 7.3
@dadhi
Copy link
Owner Author

dadhi commented May 30, 2019

@jbogard Thanks for pointing it out! I am slowly moving towards the fix optimizing some things on the way.

dadhi added a commit that referenced this issue Jun 4, 2019
@dadhi dadhi added this to the 2.1.0 milestone Jun 4, 2019
@dadhi
Copy link
Owner Author

dadhi commented Jun 20, 2019

fixed. at least this case.

@dadhi
Copy link
Owner Author

dadhi commented Mar 19, 2020

There is still a lot to fix in #222

@dadhi dadhi removed this from the 3.0.0 milestone Jul 16, 2020
@dadhi
Copy link
Owner Author

dadhi commented Aug 10, 2020

closing, the remaining work is in #222

@dadhi dadhi closed this as completed Aug 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
application The real-world use-case bug
Projects
None yet
Development

No branches or pull requests

2 participants