Catch AutoMapper configuration errors at compile-time before they cause runtime chaos! This Roslyn analyzer detects type mismatches, nullable issues, missing properties, collection incompatibilities, and complex mapping problems - all with intelligent code fixes. Transform AutoMapper development from reactive debugging to proactive prevention. Features 13 analyzer rules with smart fixes for type safety, data integrity, complex mappings, performance, and configuration. Compatible with .NET Framework 4.8+, .NET 6.0+, 8.0+, 9.0+, 10.0+.
$ dotnet add package AutoMapperAnalyzer.Analyzers✨ Catch AutoMapper configuration errors before they cause runtime chaos
A sophisticated Roslyn analyzer that transforms AutoMapper development from reactive debugging to proactive prevention
Critical Nullable Bug Fix + Comprehensive Test Coverage Expansion
🐛 Critical Fix:
int? to int) were incorrectly flagged as redundant. This could have caused runtime null reference exceptions in production code.📊 Test Coverage Expansion (+50 tests across 6 analyzers):
✅ Validation:
631 passed, 9 skipped) - 100% pass rateCode Fix Consolidation and Sample Verification - Refactoring improvements and AM006 sample fixes.
Bug Fixes and Test Coverage - Fixed AM011 placeholder bug and AM004 tree traversal, added 32 tests.
AM004 Second Pass - Tightened symbol gating, direction-scoped analysis, reverse-map aware bulk fixer.
AM041 Accuracy & Fix Safety
CreateMap symbols.Analyzer Accuracy & Contradiction Fixes
Architecture Refactoring & Enhanced Bulk Fixes
AutoMapperCodeFixProviderBase.New Icon & Visual Update
Build & Stability Fixes
🛠️ Fixes:
Bulk Code Fixes & Improved UX
✨ New Capabilities:
UserName to Username).IValueConverter class implementation and wires it up.AutoMapper is powerful, but silent failures are its Achilles' heel. Properties that don't map, type mismatches that throw at runtime, nullable violations that cause NullReferenceExceptions—these issues typically surface in production, not during development.
This analyzer changes that equation entirely.
// Before: 😰 Runtime surprise!
public void MapUserData()
{
var user = mapper.Map<UserDto>(userEntity);
// 💥 NullReferenceException in production
// 💥 Data loss from unmapped properties
// 💥 Type conversion failures
}
// After: 🛡️ Compile-time confidence!
public void MapUserData()
{
var user = mapper.Map<UserDto>(userEntity);
// ✅ All mapping issues caught at compile-time
// ✅ Code fixes suggest proper solutions
// ✅ Ship with confidence
}
# Install via .NET CLI
dotnet add package AutoMapperAnalyzer.Analyzers
# Or via Package Manager Console
Install-Package AutoMapperAnalyzer.Analyzers
That's it! The analyzer automatically activates and starts checking your AutoMapper configurations. Open any file with AutoMapper mappings and see diagnostics appear instantly.
See it work:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<User, UserDto>();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AM001: Property 'Age' type mismatch
// 💡 Press Ctrl+. for code fix suggestions
});
Every analyzer comes with intelligent code fixes that don't just identify problems—they solve them:
// Problem detected ⚠️
cfg.CreateMap<Source, Dest>();
// ~~~~~~~~~~~~~~~~~~~~~~~~~ AM001: Property 'Age' type mismatch
// Code fix applied ✨
cfg.CreateMap<Source, Dest>()
.ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
int.TryParse(src.Age, out var age) ? age : 0));
| Before | After | |
|---|---|---|
| 🐛 | Runtime mapping failures | ✅ Compile-time validation |
| 🔍 | Manual debugging sessions | ✅ Instant error highlights |
| 📝 | Guessing correct configurations | ✅ Code fixes with best practices |
| ⚠️ | Production NullReferenceExceptions | ✅ Null safety enforcement |
| 📊 | Silent data loss | ✅ Missing property detection |
| 🌐 | Cross-platform mapping inconsistencies | ✅ Case sensitivity validation |
dotnet add package AutoMapperAnalyzer.Analyzers
Install-Package AutoMapperAnalyzer.Analyzers
<PackageReference Include="AutoMapperAnalyzer.Analyzers" Version="2.27.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
| Platform | Version | Support | AutoMapper | CI/CD Status |
|---|---|---|---|---|
| .NET Framework | 4.8+ | 🟢 Full | 10.1.1+ | ✅ Tested |
| .NET | 6.0+ | 🟢 Full | 12.0.1+ | ✅ Tested |
| .NET | 8.0+ | 🟢 Full | 14.0.0+ | ✅ Tested |
| .NET | 9.0+ | 🟢 Full | 14.0.0+ | ✅ Tested |
| .NET | 10.0+ | 🟢 Full | 14.0.0+ | ✅ Tested |
Analyzer targets .NET Standard 2.0 for maximum compatibility
All platforms validated in automated CI/CD pipeline
public class UserEntity
{
public int Id { get; set; }
public string? FirstName { get; set; } // Nullable
public string LastName { get; set; }
public string Email { get; set; }
public DateTime CreatedAt { get; set; }
public List<string> Tags { get; set; } // Collection type
public Address HomeAddress { get; set; } // Complex object
}
public class UserDto
{
public int Id { get; set; }
public string FirstName { get; set; } // Non-nullable!
public string FullName { get; set; } // Different property!
public string Age { get; set; } // Different type!
public HashSet<int> Tags { get; set; } // Incompatible collection!
public AddressDto HomeAddress { get; set; } // Needs explicit mapping!
}
// This configuration has MULTIPLE issues:
cfg.CreateMap<UserEntity, UserDto>();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 🚨 AM002: FirstName nullable→non-nullable (NullReferenceException risk)
// 🚨 AM004: LastName will not be mapped (data loss)
// 🚨 AM001: Age expects int but gets DateTime (runtime exception)
// 🚨 AM021: Tags List<string>→HashSet<int> incompatible (mapping failure)
// 🚨 AM020: HomeAddress→AddressDto needs CreateMap (runtime exception)
// 🚨 AM030: Custom converter missing ConvertUsing configuration
// Code fixes automatically suggest:
cfg.CreateMap<UserEntity, UserDto>()
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName ?? ""))
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
.ForMember(dest => dest.Age, opt => opt.MapFrom(src =>
DateTime.Now.Year - src.CreatedAt.Year))
.ForMember(dest => dest.Tags, opt => opt.MapFrom(src =>
src.Tags.Select((tag, index) => index).ToHashSet()));
// Separate mapping for complex types
cfg.CreateMap<Address, AddressDto>();
# Treat type safety as build errors
dotnet_diagnostic.AM001.severity = error
dotnet_diagnostic.AM002.severity = error
dotnet_diagnostic.AM011.severity = error
# Data loss warnings
dotnet_diagnostic.AM004.severity = warning
dotnet_diagnostic.AM005.severity = warning
# Suggestions for optimization
dotnet_diagnostic.AM020.severity = suggestion
dotnet_diagnostic.AM021.severity = suggestion
// Suppress with clear justification
#pragma warning disable AM001 // Custom IValueConverter handles string→int
cfg.CreateMap<Source, Dest>();
#pragma warning restore AM001
// Method-level suppression
[SuppressMessage("AutoMapper", "AM004:Missing destination property",
Justification = "PII data intentionally excluded for GDPR compliance")]
public void ConfigureSafeUserMapping() { }
| Rule | Description | Analyzer | Code Fix | Severity |
|---|---|---|---|---|
| 🔒 Type Safety | ||||
| AM001 | Property Type Mismatch | ✅ | ✅ | Warning |
| AM002 | Nullable→Non-nullable | ✅ | ✅ | Warning |
| AM003 | Collection Incompatibility | ✅ | ✅ | Warning |
| 📊 Data Integrity | ||||
| AM004 | Missing Destination Property | ✅ | ✅ | Info |
| AM005 | Case Sensitivity Issues | ✅ | ✅ | Info |
| AM006 | Unmapped Destination Property | ✅ | ✅ | Info |
| AM011 | Required Property Missing | ✅ | ✅ | Error |
| 🧩 Complex Mappings | ||||
| AM020 | Nested Object Issues | ✅ | ✅ | Warning |
| AM021 | Collection Element Mismatch | ✅ | ✅ | Warning |
| AM022 | Circular Reference Risk | ✅ | ✅ | Warning |
| AM030 | Custom Type Converter Issues | ✅ | ✅ | Warning |
| ⚡ Performance | ||||
| AM031 | Performance Warnings | ✅ | ✅ | Warning |
| ⚙️ Configuration | ||||
| AM041 | Duplicate Mapping Registration | ✅ | ✅ | Warning |
| AM050 | Redundant MapFrom | ✅ | ✅ | Info |
| 🚀 Future | ||||
| AM032+ | Advanced Null Propagation | 🔮 | 🔮 | - |
| AM040+ | Configuration Rules | 🔮 | 🔮 | - |
| AM050+ | Advanced Optimizations | 🔮 | 🔮 | - |
dotnet build# Quick validation
dotnet build # Analyzer runs automatically
# Comprehensive testing
git clone https://github.com/georgepwall1991/automapper-analyser.git
cd automapper-analyser
dotnet run --project samples/AutoMapperAnalyzer.Samples
# See all analyzer warnings in action
dotnet build samples/ --verbosity normal
# Run full test suite with coverage
dotnet test --collect:"XPlat Code Coverage" --settings coverlet.runsettings
This isn't just another analyzer—it's built for enterprise-grade reliability:
We're building something special, and your expertise makes it better.
Quick Start Contributing:
git clone https://github.com/georgepwall1991/automapper-analyser.git
cd automapper-analyser
dotnet test
What We Need:
See our Contributing Guide for detailed guidelines.
Get Help:
MIT License - Use it anywhere, contribute back if you can.
Built with ❤️ by developers who've debugged too many AutoMapper issues
🚀 Get Started Now • 📖 Read the Docs • 💬 **Join the Discussion **