mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
parent
b26cd4e0cb
commit
071e5dc6b2
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright MapStruct Authors.
|
||||
*
|
||||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1966;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.NullValueCheckStrategy;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper( imports = Collections.class )
|
||||
public interface Issue1966Mapper {
|
||||
|
||||
Issue1966Mapper INSTANCE = Mappers.getMapper( Issue1966Mapper.class );
|
||||
|
||||
@Mapping(target = "previousNames",
|
||||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
|
||||
defaultExpression = "java(Collections.emptyList())")
|
||||
Animal toAnimal(AnimalRecord record);
|
||||
|
||||
class AnimalRecord {
|
||||
|
||||
private String[] previousNames;
|
||||
|
||||
public String[] getPreviousNames() {
|
||||
return previousNames;
|
||||
}
|
||||
|
||||
public void setPreviousNames(String[] previousNames) {
|
||||
this.previousNames = previousNames;
|
||||
}
|
||||
}
|
||||
|
||||
class Animal {
|
||||
|
||||
private List<String> previousNames;
|
||||
|
||||
public List<String> getPreviousNames() {
|
||||
return previousNames;
|
||||
}
|
||||
|
||||
public void setPreviousNames(List<String> previousNames) {
|
||||
this.previousNames = previousNames;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright MapStruct Authors.
|
||||
*
|
||||
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1966;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@IssueKey("1966")
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
@WithClasses({
|
||||
Issue1966Mapper.class
|
||||
})
|
||||
public class Issue1966Test {
|
||||
|
||||
@Test
|
||||
public void shouldSelectDefaultExpressionEvenWhenSourceInMappingIsNotSpecified() {
|
||||
|
||||
Issue1966Mapper.AnimalRecord dto = new Issue1966Mapper.AnimalRecord();
|
||||
|
||||
Issue1966Mapper.Animal entity = Issue1966Mapper.INSTANCE.toAnimal( dto );
|
||||
|
||||
assertThat( entity.getPreviousNames() ).isNotNull();
|
||||
assertThat( entity.getPreviousNames() ).isEmpty();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user