mapstruct / mapstruct/mapstruct

DeepClone does not clone java.util.Date fields but perform a copy reference

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

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

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

説明

Expected behavior

I have a mapper annotated DeepClone to perform a copy clone of some bean. I notice that the generated implementation on java.util.Date field it perform a simple a.setDate(b.getDate()). This could be ok for String or BigDecimal object that are immutable but old java Date object are not immutable that mean if I change b.getDate().setTime(1l) it will also update date in the bean a.

What I expect is that in case of DeepClone it produce the following code:

    @Override
    public BeanA clone(BeanA value) {
        if ( value == null ) {
            return null;
        }

        BeanA beanA = new BeanA();
        if (source.getDate() != null) {
            beanA.setDate(new Date(source.getDate().getTime())); // expected behaviour
        }
        
        return beanA;
    }
Actual behavior

The generated code is

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2025-09-16T10:12:55+0200",
    comments = "version: 1.6.3, compiler: Eclipse JDT (IDE) 3.41.0.v20250213-1140, environment: Java 21.0.6 (Eclipse Adoptium)"
)
public class ICloneMapperImpl implements ICloneMapper {

    @Override
    public BeanA clone(BeanA value) {
        if ( value == null ) {
            return null;
        }

        BeanA beanA = new BeanA();
        beanA.setDate(source.getDate());
        
        return beanA;
    }
}
Steps to reproduce the problem

Create the following classes

public class BeanA {
    private Date date;
    public Date getDate() { return date; }
    public void setDate(Date value) { date = value; }
}

@Mapper(mappingControl = DeepClone.class)
public interface ICloneMapper {
    BeanA clone(BeanA source);
}
MapStruct Version

MapStruct 1.6.3

Workaround

In the ICloneMapper I have to define a method like this:

default Date map(Date value) {
        return value != null ? new Date(value.getTime()) : null;
    }

to get the clone of date.

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

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

はじめの一歩

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

調査の方向性

Issue に示されている DeepClone マッピング制御と ICloneMapper の再現から始めてください。生成された mapper が java.util.Date フィールドをどのように処理するかを確認してください。元の日付を後から変更した場合も含め、生成された clone が変更可能な Date 参照を共有しなければ完了です。

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

評価

技術スタック
java
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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