mapstruct / mapstruct/mapstruct

QOL improvement with @PrimaryMappingSource instead of explicit source declaration

オープン
#3,161 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feature
主要言語
Java
スター
7.7k
フォーク
1.1k
PR マージ指標
30日以内にマージされた PR はありません

説明

Use case

Sometimes, a mapping needs to borrow a field from another table.

@Mapping(target = "borrowed", source = "tblB.borrowed")
EntityA toEntity(TblA tblA, TblB tblB)

But if those tables have many similar fields, then we'll get lots of errors

error: Several possible source properties for target property "id"
error: Several possible source properties for target property "name"
error: Several possible source properties for target property "description"
error: Several possible source properties for target property "createdAtUtc"
error: Several possible source properties for target property "updatedAtUtc"

which can be resolved by manually specifying the source of every conflicting property

@Mapping(target = "id", source = "tblA.id")
@Mapping(target = "id", source = "tblA.name")
@Mapping(target = "id", source = "tblA.description")
@Mapping(target = "id", source = "tblA.createdAtUtc")
@Mapping(target = "id", source = "tblA.updatedAtUtc")
@Mapping(target = "borrowed", source = "tblB.borrowed")
EntityA toEntity(TblA tblA, TblB tblB)

My proposal is a new annotation called something like @PrimaryMappingSource which would be used like this

@Mapping(target = "borrowed", source = "tblB.borrowed")
EntityA toEntity(
  @PrimaryMappingSource TblA tblA, 
  TblB tblB
)

Which would have the effect of defaulting to tblA when there is any ambiguity in the source for a target property. This is similar to the @org.springframework.context.annotation.Primary annotation, which they explain like this:

Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.

Generated Code

The generated code would be the same as manually specifying the source of all those ambiguous properties

Possible workarounds

No response

MapStruct Version

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

この issue には、ソースファイル、テスト、エントリーポイントが記載されていません。まず、MapStruct の曖昧な source-property の解決と annotation-processing のテストを見つけ、次に既存のパラメーター annotation の規約と比較してください。完了条件には、primary-source annotation のサポート、曖昧なプロパティのカバレッジ、明示的な source 宣言と同等の生成 mappings を含めてください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
java
領域
developer-experience, tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。