cezarypiatek / MappingGenerator

:arrows_counterclockwise: "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.mappinggenerator
MIT License
1.03k stars 120 forks source link

Exception when generating mapping #75

Closed zpqrtbnk closed 5 years ago

zpqrtbnk commented 5 years ago

I create this code:

public Thing1 Test(Thing2 thing)
{
}

Where Thing1 and Thing2 have some properties in common, and other, non-common properties. Oh and they might also have properties matching by name but not by type. And maybe Thing1 has only parameter-less constructors.

Gettings this exception soon as I hit the generate menu:

System.NullReferenceException : Object reference not set to an instance of an object.
   at MappingGenerator.MappingEngine.<>c__DisplayClass9_2.<TryToCreateMappingExpression>b__3(MappingElement x)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source,Func`2 predicate)
   at MappingGenerator.MappingEngine.<>c__DisplayClass9_1.<TryToCreateMappingExpression>b__2(MappingElement foundElement)
   at MappingGenerator.IgnorableMappingSourceFinder.FindMappingSource(String targetName,ITypeSymbol targetType)
   at MappingGenerator.MappingEngine.<>c__DisplayClass11_0.<MapUsingSimpleAssignment>b__0(IPropertySymbol property)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MappingGenerator.MappingEngine.MapUsingSimpleAssignment(SyntaxGenerator generator,IEnumerable`1 targets,IMappingSourceFinder sourceFinder,MappingPath mappingPath,SyntaxNode globalTargetAccessor)
   at MappingGenerator.MappingEngine.AddInitializerWithMapping(ObjectCreationExpressionSyntax objectCreationExpression,IMappingSourceFinder mappingSourceFinder,ITypeSymbol createdObjectTyp,MappingPath mappingPath)
   at MappingGenerator.MappingEngine.TryToCreateMappingExpression(MappingElement source,ITypeSymbol targetType,MappingPath mappingPath)
   at MappingGenerator.MappingEngine.MapExpression(MappingElement element,ITypeSymbol targetType,MappingPath mappingPath)
   at MappingGenerator.MappingEngine.MapExpression(ExpressionSyntax sourceExpression,ITypeSymbol sourceType,ITypeSymbol destinationType)
   at MappingGenerator.MappingGeneratorRefactoring.GenerateMappingCode(IMethodSymbol methodSymbol,SyntaxGenerator generator,SemanticModel semanticModel)
   at async MappingGenerator.MappingGeneratorRefactoring.GenerateMappingMethodBody(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)

Is it... that I am doing something that I should not do, or is it some kind of anomaly?

cezarypiatek commented 5 years ago

Hi

Could you provide definitions of Thing1 and Thing2 classes?

zpqrtbnk commented 5 years ago

Tried to do my homework, using the unit tests in the MappingGenerator project. Been able to narrow it down to

    public class Thing1
    {
        public string Value { get; set; }
    }

    public class Thing2
    {
        public Thing2(string value)
        { }

        public string Value { get; set; }
    }

The non-empty ctor is killing it. Which... kinda makes sense I assume?

cezarypiatek commented 5 years ago

I guess this could be related to the new feature #64 I will take a look at this on the next weekend. Thanks for due diligence.

zpqrtbnk commented 5 years ago

Cool. No rush, enjoy your weekend ;-)

cezarypiatek commented 5 years ago

I was't able to reproduce this issue using your example. Here's the proof

issue75

Can you create a fork with UT that is reproducing the problem?

cezarypiatek commented 5 years ago

Still no luck with reproducing, but probably I've found the culprit. Here's version with the fix https://ci.appveyor.com/api/buildjobs/24v6iq24h1bxu8rx/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator.Vsix%2Fbin%2FRelease%2FMappingGenerator.vsix

@zpqrtbnk would you mind to install it and verify if everything is working as expected?

cezarypiatek commented 5 years ago

Hi @zpqrtbnk, did you have an occasion to test this new feature?

zpqrtbnk commented 5 years ago

Arh - got side tracked on other stuff, will try soon as I can

brrbtr commented 5 years ago

I was experiencing the same issue. Downloaded the above version. Now, all is well. Thanks!

cezarypiatek commented 5 years ago

Thanks @brrbtr for testing.

cezarypiatek commented 5 years ago

An official version with this fix was released Please install version of the official channel https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.mappinggenerator