Skip to content

feat: projection expression support#2141

Merged
latonz merged 21 commits into
riok:mainfrom
Dreamescaper:expressions
Feb 17, 2026
Merged

feat: projection expression support#2141
latonz merged 21 commits into
riok:mainfrom
Dreamescaper:expressions

Conversation

@Dreamescaper

@Dreamescaper Dreamescaper commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

Add Expression<Func<TSource, TTarget>> mapping support

Description

Adds support for generating Expression<Func<TSource, TTarget>> mappings directly:

[Mapper]
public static partial class CarMapper
{
    public static partial Expression<Func<Car, CarDto>> ProjectToDto();
}

Implementation

  • UserDefinedExpressionMethodMapping - Handles parameterless methods returning Expression, extracting source/target from type arguments
  • ExpressionMapping - Generates lambda with nullable pragmas (same pattern as IQueryable)
  • Reuses InlineExpressionMappingBuilderContext for expression tree building
  • No changes to IQueryable behavior

vs #951

PR #951 modified IQueryable to call a generated Expression method, requiring a "void source hack" throughout core classes.
This PR keeps Expression mappings separate from IQuaryable. They share implementation details, but still separate branches.
"void source hack" is still present here, but I have tried to keep it's impact limited to UserDefinedExpressionMethodMapping type only.

Fixes #733

Checklist

  • I did not use AI tools to generate this PR, or I have manually verified that the code is correct, optimal, and follows the project guidelines and architecture.
  • I understand that low-quality, AI-generated PRs will be closed immediately without further explanation
  • 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)

@latonz latonz added the enhancement New feature or request label Feb 5, 2026
@latonz latonz changed the title Expressions (WIP) feat: projection expression support Feb 5, 2026

@latonz latonz left a comment

Copy link
Copy Markdown
Contributor

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! ❤️ Very nice implementation.
I added my feedback.

Comment thread src/Riok.Mapperly/Descriptors/MappingBodyBuilders/UserMethodMappingBodyBuilder.cs Outdated
Comment thread docs/docs/configuration/queryable-projections.mdx Outdated
Comment thread src/Riok.Mapperly/Descriptors/MappingBodyBuilders/UserMethodMappingBodyBuilder.cs Outdated
Comment thread src/Riok.Mapperly/Descriptors/MappingBodyBuilders/UserMethodMappingBodyBuilder.cs Outdated
Comment thread test/Riok.Mapperly.Tests/Mapping/ExpressionMappingTest.cs
@Dreamescaper
Dreamescaper requested a review from latonz February 7, 2026 14:21
@Dreamescaper
Dreamescaper marked this pull request as ready for review February 7, 2026 14:21
@Dreamescaper

Copy link
Copy Markdown
Contributor Author

@latonz

Thanks a lot for your review! Hopefully, I've addressed all your comments, and marked PR as Ready For Review.

@latonz latonz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the updates. There are just a few small fixes required, and then we should be ready to merge.

Comment thread test/Riok.Mapperly.Tests/Mapping/ExpressionMappingTest.cs Outdated
Comment thread test/Riok.Mapperly.Tests/Mapping/ExpressionMappingTest.cs Outdated
Comment thread src/Riok.Mapperly/Descriptors/MappingBodyBuilders/ExpressionMappingBodyBuilder.cs Outdated
@latonz

latonz commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

Regarding the failing tests:

  • for the integration tests you may need to apply the VersionedSnapshot attribute. See other tests on how to use it
  • You need to run prettier in the docs directory to fix the formatting issue.

@Dreamescaper

Copy link
Copy Markdown
Contributor Author

Hopefully I have fixed all those issues!

@latonz
latonz merged commit afc6775 into riok:main Feb 17, 2026
17 checks passed
@latonz

latonz commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Thank you for this great contribution 🥳

@CodyDuff

Copy link
Copy Markdown

Do we know what release this will be included in?

@latonz

latonz commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

It is available as part of v5.0.0-next.2 and will be available on the stable channel with 5.0.0.

@manuelelucchi

Copy link
Copy Markdown

@latonz @CodyDuff does this support configuration attrbutes? It doesn't seem to care about MapperIgnore etc

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.

Allow creating an Expression<Func<>> instead of a IQueryable Projection

4 participants