Skip to content

#3931 Fix @DeepClone to clone Date and Calendar instead of copying references#4092

Open
kamilkrzywanski wants to merge 1 commit into
mapstruct:mainfrom
kamilkrzywanski:issue-3931-deepclone-date
Open

#3931 Fix @DeepClone to clone Date and Calendar instead of copying references#4092
kamilkrzywanski wants to merge 1 commit into
mapstruct:mainfrom
kamilkrzywanski:issue-3931-deepclone-date

Conversation

@kamilkrzywanski

Copy link
Copy Markdown

Summary

  • Fixes #3931: with @Mapper(mappingControl = DeepClone.class), java.util.Date fields were assigned by reference (setDate(source.getDate())) instead of being cloned.
  • Root cause: when DIRECT is disabled, types from the java package are still treated as always-direct. That is correct for immutable types like String, but wrong for mutable Date / Calendar.
  • Processor-only fix in MappingResolverImpl: exclude java.util.Date and java.util.Calendar from the always-direct rule when DIRECT is off, and emit an inline clone:
    • Datenew Date(source.getTime())
    • Calendar((Calendar) source.clone())
  • Default mappers (with DIRECT enabled) are unchanged and still copy the reference.
  • No public API changes.

Generated code (after)

if ( source.getDate() != null ) {
    beanA.setDate( new Date( source.getDate().getTime() ) );
}
if ( source.getCalendar() != null ) {
    beanA.setCalendar( ((Calendar) source.getCalendar().clone()) );
}

Test plan

  • Issue3931Test — Date/Calendar independence under @DeepClone, null handling
  • MappingControlTest — existing deep-clone / mapping-control behaviour
  • DateConversionTest — normal Date conversion paths still pass

…opying references

When DIRECT mapping is disabled (e.g. via @deepClone), java.util.Date and
java.util.Calendar were still assigned by reference because types from the
java package are treated as always-direct. Clone them instead so mutations
on the source no longer affect the target.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant