Add SnakeCase strategy to PropertyNameMappingStrategy
Add SnakeCase Property Name Mapping Strategy
Description
This PR adds support for SnakeCase as a new property name mapping strategy in Mapperly, enabling automatic mapping between PascalCase properties in C# source objects and snake_case properties in target objects.
Motivation
In modern C# development, it's common to work with external systems or code generators that produce DTOs with snake_case property names (e.g., from JSON APIs, database schemas, or code generation tools). Currently, mapping between C# PascalCase properties and snake_case properties requires manual [MapProperty] attributes for each property, which is tedious and error-prone for large objects.
This feature is particularly valuable when:
- Working with auto-generated C# DTOs from external contracts that use snake_case naming
- Integrating with APIs that follow snake_case conventions (common in Python, Ruby, or REST APIs)
- The source contracts cannot be modified due to external dependencies or architectural constraints
Changes Made
- Added
SnakeCaseenum value toPropertyNameMappingStrategywith XML documentation - Implemented snake_case to PascalCase conversion for property matching in
MemberPathCandidateBuilder - Updated
MemberPathCandidateBuilderwith new overload supporting naming strategies - Modified
MembersMappingBuilderContextto use SnakeCase strategy when configured - Updated PublicAPI snapshot to reflect new enum value
- Added comprehensive test case for SnakeCase property mapping validation
Usage Example
[Mapper(PropertyNameMappingStrategy = PropertyNameMappingStrategy.SnakeCase)]
public partial class MyMapper
{
public partial TargetDto Map(SourceDto source);
}
// Maps automatically:
// source.FirstName -> target.first_name
// source.LastName -> target.last_name
This allows developers to map properties between PascalCase source objects and snake_case target objects (e.g., FirstName -> first_name) by simply setting PropertyNameMappingStrategy = PropertyNameMappingStrategy.SnakeCase.
Fixes # (issue - if applicable)
Checklist
- [x] The existing code style is followed
- [x] The commit message follows our guidelines
- [x] Performed a self-review of my code
- [x] Hard-to-understand areas of my code are commented
- [ ] The documentation is updated (as applicable)
- [x] Unit tests are added/updated
- [ ] Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)