Skip to content

Conversation

@Kataane
Copy link
Contributor

@Kataane Kataane commented Oct 30, 2025

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

  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Hard-to-understand areas of my code are commented
  • The documentation is updated (as applicable)
  • Unit tests are added/updated
  • Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)

Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution. I added my feedback. IMO we should also support auto flattening (see docs) and UpperSnakeCase.

@latonz latonz added the enhancement New feature or request label Oct 31, 2025
@latonz
Copy link
Contributor

latonz commented Oct 31, 2025

Please also update the documentation.

@Kataane
Copy link
Contributor Author

Kataane commented Dec 6, 2025

Hi @latonz,
Could you please review the changes? I would like to add this functionality to my code as soon as possible.

Copy link
Contributor

@latonz latonz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates!

@Kataane
Copy link
Contributor Author

Kataane commented Dec 14, 2025

Hi @latonz,

Thank you for your review.

I fixed the issues based on your reviews. The main point that is still unresolved is the complex login in MemberPathCandidateBuilder.cs.

@latonz latonz force-pushed the feature/snake-case-property-mapping branch 3 times, most recently from c2e1117 to 5d003ed Compare December 24, 2025 15:33
- Added SnakeCase enum value to PropertyNameMappingStrategy with XML documentation
- Implemented snake_case to PascalCase conversion for property matching
- 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 test case for SnakeCase property mapping validation

This allows to map properties between PascalCase
and snake_case objects (e.g., FirstName -> first_name) by setting
PropertyNameMappingStrategy = PropertyNameMappingStrategy.SnakeCase

Co-authored-by: Lars <lars@riok.ch>
@latonz latonz force-pushed the feature/snake-case-property-mapping branch from 5d003ed to 1ef6b21 Compare December 24, 2025 15:37
@latonz latonz enabled auto-merge (squash) December 24, 2025 15:48
@latonz latonz disabled auto-merge December 24, 2025 15:48
@latonz latonz merged commit 779a322 into riok:main Dec 24, 2025
17 checks passed
@latonz latonz changed the title Add SnakeCase strategy to PropertyNameMappingStrategy feat: Add SnakeCase strategy to PropertyNameMappingStrategy Dec 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants