publicissapient-france/selma

Using interceptor with withIgnoreNullValue = true

cbeaujoin opened this issue · 5 comments

Usage of interceptor and withIgnoreNullValue = true result in NPE

@Mapper(
        withImmutables = {Calendar.class, Byte.class}, withCyclicMapping = false, withIgnoreMissing = IgnoreMissing.ALL,
        withIoC = IoC.SPRING, withIgnoreNullValue = true)
public interface MyMapper {
    @Maps(withCustom = {CustomDateTimeInterceptor.class})
    Validation asValidation(ValidationJson in, Validation out);
}
@Component
public class CustomDateTimeInterceptor {

    public void intercept(ValidationJson in, Validation out) {
             ...
            out.setValidationDate(validationDate);
    }
}

Caused by: java.lang.NullPointerException
at fr.xebia.extras.selma.codegen.SourceNodeVars.isOutPrimitive(SourceNodeVars.java:162)
at fr.xebia.extras.selma.codegen.MappingBuilder.build(MappingBuilder.java:747)
at fr.xebia.extras.selma.codegen.MapperMethodGenerator.buildMappingMethod(MapperMethodGenerator.java:190)
at fr.xebia.extras.selma.codegen.MapperMethodGenerator.build(MapperMethodGenerator.java:69)
at fr.xebia.extras.selma.codegen.MapperClassGenerator.build(MapperClassGenerator.java:161)
at fr.xebia.extras.selma.codegen.MapperProcessor.generateMappingClassses(MapperProcessor.java:88)
at fr.xebia.extras.selma.codegen.MapperProcessor.process(MapperProcessor.java:71)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
... 29 more

Fix it using withIgnoreNullValue = false

Hi,

Thanks for the report.
can you provide Validation and ValidationJson code ?
Also can you tell me what release of selma are you using.
The generated code could also be helpfull.
I am trying to reproduce the bug without success for now :p, so I'll need a bit more food for thought.

I added a test in last commit to reproduce the bug, but without success.
Can check the test and tell what's missing ?

<selma.version>1.0</selma.version>

It fails at code generation (compile maven's goal)

ValidationJson :

public class ValidationJson {

    private Integer eventDateStamp;

    private Integer eventTimeStamp;

    public Integer getEventDateStamp() {
        return eventDateStamp;
    }

    public void setEventDateStamp(Integer eventDateStamp) {
        this.eventDateStamp = eventDateStamp;
    }

    public Integer getEventTimeStamp() {
        return eventTimeStamp;
    }

    public void setEventTimeStamp(Integer eventTimeStamp) {
        this.eventTimeStamp = eventTimeStamp;
    }
}

Validation :

@Entity
@Table(name = "validation")
public class Validation implements Serializable {

   @Id
    @SequenceGenerator(name = "validation_id_seq", sequenceName = "validation_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "validation_id_seq")
    private Integer id;

    private Calendar validationDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Calendar getValidationDate() {
        return validationDate;
    }

    public void setValidationDate(Calendar validationDate) {
        this.validationDate = validationDate;
    }
}

CustomDateTimeInterceptor :

@Component
public class CustomDateTimeInterceptor {

    public void intercept(ValidationJson in, Validation out) {
        final Integer eventDateStamp = in.getEventDateStamp();
        final Integer eventTimeStamp = in.getEventTimeStamp();
        if (null != eventDateStamp && null != eventTimeStamp) {
            final LocalDate date = LocalDate.of(1997, 1, 1).plusDays(eventDateStamp);
            final LocalTime time = LocalTime.of(0, 0).plusMinutes(eventTimeStamp);
            final Calendar validationDate = GregorianCalendar.from(ZonedDateTime.of(date, time, ZoneId.systemDefault()));
            out.setValidationDate(validationDate);
        }
    }

Hi,
I think this bug is resolved in current 1.1-SNAPSHOT build.
Can you give it a try ?
I added your beans and mapper to a test passing with success, and no NPE at compile time

Hello,
It works fine with the current 1.1-SNAPSHOT build.
I will use it as soon as it will be available on maven central.