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

Extension error with a null exception #15

Closed MohadKamil closed 6 years ago

MohadKamil commented 6 years ago

Hi, thanks for the useful extension by the way.

The extension keeps crashing with a null reference exception, this started right after updating visual studio to version 15.6.4, prior versions of visual studio worked just fine with the extension.

Stack trace: System.NullReferenceException : Object reference not set to an instance of an object. at MappingGenerator.SymbolHelper.IsReadonlyProperty(IPropertySymbol property) at MappingGenerator.MappingGenerator.CanBeSet(IPropertySymbol targetProperty,SyntaxNode globbalTargetAccessor,Boolean isConstructorContext) at lMappingGenerator.MappingGenerator.d__4.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at Microsoft.CodeAnalysis.SyntaxList1.CreateNode(IEnumerable1 nodes) at Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.AsStatementList(IEnumerable1 nodes) at Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.CreateBlock(IEnumerable1 statements) at Microsoft.CodeAnalysis.CSharp.CodeGeneration.CSharpSyntaxGenerator.MethodDeclaration(String name,IEnumerable1 parameters,IEnumerable1 typeParameters,SyntaxNode returnType,Accessibility accessibility,DeclarationModifiers modifiers,IEnumerable1 statements) at Microsoft.CodeAnalysis.Editing.SyntaxGenerator.MethodDeclaration(IMethodSymbol method,String name,IEnumerable`1 statements) at async MappingGenerator.MappingGeneratorCodeFixProvider.GenerateMappingMethodBody() at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync() at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync() at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync() at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync() at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync() at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.PreviewChangesSuggestedAction.CreateAsync() at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.GetPreviewChangesFlavor() at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.CreateAllFlavors() at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T]()

cezarypiatek commented 6 years ago

Hi

Thanks for the feedback. I'm still working on VS 15.5.0 so I haven't had chance to test it on newer version. Could you provide a sample code that cause this exception of code fix?

OmiCron07 commented 6 years ago

I have a lot of null reference exceptions too. VS 15.6.4 and I get a lot of these errors when creating a response :

using System.Collections.Generic;
using System.Runtime.Serialization;
using Nms.Client.Abf.Shared.Contracts.Detailing.Entities;

namespace Nms.Client.Abf.Shared.Contracts.Detailing
{
  [DataContract]
  public class GetDetailingStatusesResponse : BaseAbfResponse
  {
    public GetDetailingStatusesResponse()
    {
    }

    public GetDetailingStatusesResponse(List<StatusEntity> statuses)
    {
      Statuses = statuses;
    }

    public List<StatusEntity> Statuses { get; }
  }
}
cezarypiatek commented 6 years ago

I've installed VS 15.6.5 but I'm unable to reproduce this problem. I would like to help but I need an example code that cause this issue. Maybe there was an issue with roslyn and VS 15.6.4 and 15.6.5 update fixed that.

OmiCron07 commented 6 years ago

ClassLibrary5.zip

There a repro with a .Net Standard class library, didn't test with a classic class library.

using System.Collections.Generic;

namespace ClassLibrary5
{
  public class GetReportsResponse : BaseAbfResponse
  {
    public GetReportsResponse(List<string> reports)
    {
    }
  }

  public abstract class BaseAbfResponse
  {
    public bool IsSuccessful => true;
  }
}
cezarypiatek commented 6 years ago

@OmiCron07 thanks for the example code which helped me to reproduce the issue. I've prepared the fix but I'm unable to test it because of the issue https://github.com/dotnet/roslyn/issues/25962 This also prevents me from releasing fix for #13

OmiCron07 commented 6 years ago

Does the version 15.6.6 fix the problem? There are no release notes yet.

cezarypiatek commented 6 years ago

The problem seems to persist in 15.6.6 (unlike #13)