Last active
January 18, 2024 11:22
-
-
Save mSobhy90/bfce8e96bdf9387956345408510d5e44 to your computer and use it in GitHub Desktop.
MapStruct <> Lombok boolean property with `is` in the name issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The mapper generated by MapStruct without fixes (commented out code) | |
package com.transferwise.swiftprocessor.mapper.event; | |
import javax.annotation.processing.Generated; | |
@Generated( | |
value = "org.mapstruct.ap.MappingProcessor", | |
date = "2024-01-17T22:06:57+0000", | |
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.4.2.jar, environment: Java 17.0.9 (Azul Systems, Inc.)" | |
) | |
public class TestMapperImpl implements TestMapper { | |
@Override | |
public LombokTest fromNonLombok(NonLombokTest test) { | |
if ( test == null ) { | |
return null; | |
} | |
LombokTest.LombokTestBuilder lombokTest = LombokTest.builder(); | |
lombokTest.valid( test.isValid() ); | |
lombokTest.isGood( test.isIsGood() ); | |
return lombokTest.build(); | |
} | |
@Override | |
public NonLombokTest fromLombok(LombokTest test) { | |
if ( test == null ) { | |
return null; | |
} | |
boolean isGood = false; | |
boolean valid = false; | |
isGood = test.isGood(); | |
valid = test.isValid(); | |
NonLombokTest nonLombokTest = new NonLombokTest( valid, isGood ); | |
return nonLombokTest; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The mapper generated by MapStruct with fixes (commented out code uncommented) | |
package com.transferwise.swiftprocessor.mapper.event; | |
import javax.annotation.processing.Generated; | |
@Generated( | |
value = "org.mapstruct.ap.MappingProcessor", | |
date = "2024-01-17T22:12:48+0000", | |
comments = "version: 1.5.3.Final, compiler: IncrementalProcessingEnvironment from gradle-language-java-7.4.2.jar, environment: Java 17.0.9 (Azul Systems, Inc.)" | |
) | |
public class TestMapperImpl implements TestMapper { | |
@Override | |
public LombokTest fromNonLombok(NonLombokTest test) { | |
if ( test == null ) { | |
return null; | |
} | |
LombokTest.LombokTestBuilder lombokTest = LombokTest.builder(); | |
lombokTest.valid( test.isValid() ); | |
lombokTest.isGood( test.isIsGood() ); // Notice isIsGood being used here | |
return lombokTest.build(); | |
} | |
@Override | |
public NonLombokTest fromLombok(LombokTest test) { | |
if ( test == null ) { | |
return null; | |
} | |
boolean isGood = false; | |
boolean valid = false; | |
isGood = test.isGood(); // Notice isGood being used here, since we mapped our source property to be just `good` | |
valid = test.isValid(); | |
NonLombokTest nonLombokTest = new NonLombokTest( valid, isGood ); | |
return nonLombokTest; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.transferwise.swiftprocessor.mapper.event; | |
import lombok.AllArgsConstructor; | |
import lombok.Builder; | |
import lombok.Data; | |
import org.mapstruct.Mapper; | |
import org.mapstruct.Mapping; | |
@Data | |
@Builder | |
@AllArgsConstructor | |
class LombokTest { | |
private final boolean valid; | |
private final boolean isGood; | |
} | |
class NonLombokTest { | |
private final boolean valid; | |
private final boolean isGood; | |
NonLombokTest(boolean valid, boolean isGood) { | |
this.valid = valid; | |
this.isGood = isGood; | |
} | |
public boolean isValid() { | |
return valid; | |
} | |
public boolean isGood() { | |
return isGood; | |
} | |
/** | |
* Uncommenting this would make the mapping work, as it would use the `isIsGood` function instead of `isGood`. Which is why we're having this | |
* issue. | |
* <p> | |
* `lombokTest.valid( test.isValid() );` | |
* <p> | |
* `lombokTest.isGood( test.isIsGood() );` | |
*/ | |
// public boolean isIsGood() { | |
// return isGood; | |
// } | |
} | |
@Mapper | |
public interface TestMapper { | |
LombokTest fromNonLombok(NonLombokTest test); | |
// @Mapping(source = "good", target = "isGood") | |
NonLombokTest fromLombok(LombokTest test); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.transferwise.swiftprocessor.mapper.event; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import org.mapstruct.factory.Mappers; | |
class TestMapperTest { | |
TestMapper mapper = Mappers.getMapper(TestMapper.class); | |
@Test | |
void mapFromLombokTest() { | |
LombokTest lombokTest = LombokTest.builder() | |
.valid(true) | |
.isGood(true) | |
.build(); | |
NonLombokTest nonLombokTest = mapper.fromLombok(lombokTest); | |
Assertions.assertTrue(nonLombokTest.isValid()); | |
// Fails! Unless we uncomment `@Mapping(source = "good", target = "isGood")` on `fromLombok` | |
Assertions.assertTrue(nonLombokTest.isGood()); | |
} | |
@Test | |
void mapFromNonLombok() { | |
NonLombokTest nonLombokTest = new NonLombokTest(true, true); | |
LombokTest lombokTest = mapper.fromNonLombok(nonLombokTest); | |
Assertions.assertTrue(lombokTest.isValid()); | |
// Fails! Unless we uncomment `isIsGood` from `NonLombokTest` | |
Assertions.assertTrue(lombokTest.isGood()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment