-
-
Notifications
You must be signed in to change notification settings - Fork 979
defaultExpression not being applied when source property not specified explicitly #1966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note: using MapStruct |
Thanks for the issue @marceloverdijk. In my opinion this is a bug, we should always apply the |
@marceloverdijk .. I tried to replicate the problem, but.. it works on my machine 😄 @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;
}
}
} result (as expected): @Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-11-10T12:25:37+0100",
comments = "version: , compiler: Eclipse JDT (Batch) 1.2.100.v20160418-1457, environment: Java 1.8.0_181 (Oracle Corporation)"
)
public class Issue1966MapperImpl implements Issue1966Mapper {
@Override
public Animal toAnimal(AnimalRecord record) {
if ( record == null ) {
return null;
}
Animal animal = new Animal();
List<String> list = stringArrayToStringList( record.getPreviousNames() );
if ( list != null ) {
animal.setPreviousNames( list );
}
else {
animal.setPreviousNames( Collections.emptyList() );
}
return animal;
}
protected List<String> stringArrayToStringList(String[] stringArray) {
if ( stringArray == null ) {
return null;
}
List<String> list = new ArrayList<String>( stringArray.length );
for ( String string : stringArray ) {
list.add( string );
}
return list;
}
} Note: I used the latest master branch in order to create a test case. But I don't think we fixed an issue in this area. Note ii: I added an import to my mapper. |
Interesting @sjaakd :-) I've setup this sample project https://github.com/marceloverdijk/mapstruct-1966 Could you check it out and see if you see different behavior? |
I just checked.. Compile your example against 1.4.0-SNAPSHOT (our master) and it works as expected.. I guess we must have fixed something for the better 😄 |
I added #1967 jus to avoid regression. |
@marceloverdijk thanks for reporting. I'll proceed to close the issue, since the problem does not occur on our master. |
Yes, I can confirm with |
mapstruct#1966 extra unit test (mapstruct#1967)
When do you expect the |
Hello, I'm trying the same use case and hitting the bug as well. Tried 1.4.2.Final, 1.5.0.Beta1 and 1.5.0.RC1. In my case an object has List where Step -> StepRecord is also processed by the mapper. The mapping looks like this: The generated code calls a separate method: And the method starts with the following statement:
After checking the rest of the generated mapper, the defaultExpression code is actually present, but just in the update() logic, not in to(). Is there any way to process default values during standard conversion or do we need to always call to() and then update() to make sure the defaults are applied? |
When having the following classes:
and a mapper like:
I would expect that the implementation would convert a
null
array inAnimalRecord
to an emptyList
(notnull
) in theAnimal
instance.However the generated code looks like:
When I explicitly add the
source = "previousNames"
to the@Mapping
like:then the code is generated as expected:
Note the
else
part generated to set the empty list.It seems the
defaultExpression
is not being applied when thesource
property is not specified.Maybe in general or related to the fact that there is also an array to
List
conversion needed.The text was updated successfully, but these errors were encountered: