mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Add SnakeCase strategy to PropertyNameMappingStrategy

Open Kataane opened this issue 3 months ago • 2 comments

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 SnakeCase enum value to PropertyNameMappingStrategy with XML documentation
  • Implemented snake_case to PascalCase conversion for property matching in MemberPathCandidateBuilder
  • Updated MemberPathCandidateBuilder with new overload supporting naming strategies
  • Modified MembersMappingBuilderContext to 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)

Kataane avatar Oct 30 '25 13:10 Kataane