Someone adds a field to a DTO. The entity already has it. MapStruct compiles fine, the tests pass, the PR merges. Two weeks later the field is null in production and nobody knows why.
The compiler knew. It just wasn't told to care.
MapStruct is the default mapping library on most Java teams I've worked on, and almost nobody touches the config past @Mapper. That default config is exactly what lets the bug above happen. Here are the three things I add to every mapper to make it safe, testable, and predictable.
1. Make the compiler fail on missing mappings
By default, when a target field has no matching source, MapStruct's unmappedTargetPolicy is WARN. A warning in an annotation processor is noise. It scrolls past in the build log and nobody reads it. So the field stays null and you find out from a bug report.
Flip it to ERROR:
@Mapper(
componentModel = MappingConstants.ComponentModel.SPRING,
unmappedTargetPolicy = ReportingPolicy.ERROR
)
public interface UserM
Discussion
Be the first to comment
Add your perspective to get the discussion started.