Java / MapStruct Java Interview questions part 2
How does MapStruct handle nested object mapping?
When a source or target property is itself a complex object rather than a primitive or String, MapStruct looks for another mapping method - either generated for another top-level method in the same mapper, a default method, or a method in a mapper listed under uses - that converts between the nested types.
@Mapper(uses = AddressMapper.class) public interface CustomerMapper { CustomerDto toDto(Customer customer); // customer.address -> dto.address }
If a matching nested mapping method is found, MapStruct calls it as part of the generated code, effectively composing mappers. If no such method exists and the types aren't identical or trivially convertible, the build produces a warning (or error, depending on configuration) rather than silently copying an incompatible reference.
More Related questions...