KrisG
KrisG

Reputation: 1081

ModelMapper fail validation when types don't match exactly

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();
    }
    
}

Upvotes: 0

Views: 215

Answers (1)

Unmitigated
Unmitigated

Reputation: 89139

You can configure full type matching.

modelMapper.getConfiguration().setFullTypeMatchingRequired(true);

Upvotes: 1

Related Questions