ModelMapper fail validation when types don't match exactly

61 Views Asked by At

I've just noticed my two classes had slightly different types for the same property, double vs Double. As a result null Double would be automatically be converted to 0 double. Now I see I could even have the same property defined as a String on one side without any validation errors being raised.

Is there a configuration in ModelMapper to require strict type mapping between properties, ie. fail validation if the types are not exactly the same.

Example:

public class MyCarDto {
    public double numberOfWheels;
}

public class MyCarDao {
    public Double numberOfWheels;
}

public class MyCarMapper {
    private final ModelMapper modelMapper;

    public MyCarMapper() {
        modelMapper = new ModelMapper();
        modelMapper.typeMap(MyCarDto.class, MyCarDao.class);

        // should throw an exception saying property numberOfWheels should be exact same type
        modelMapper.validate();
    }
    
}
1

There are 1 best solutions below

1
Unmitigated On

You can configure full type matching.

modelMapper.getConfiguration().setFullTypeMatchingRequired(true);