Skip to content

MappingType lost in recursive mapping #56

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
petromir opened this issue Oct 19, 2016 · 7 comments
Closed

MappingType lost in recursive mapping #56

petromir opened this issue Oct 19, 2016 · 7 comments
Assignees
Labels
Milestone

Comments

@petromir
Copy link

I have the following case:

public class Source {
    private String sourceText;
    private NestedSource nestedSource;

    // Getters, Setters and toString()
}

public class NestedSource {
    private String nestedSourceText;

    // Getters, Setters and toString()
}
public class Destination {
    @JMap(value = "sourceText", classes = Source.class)
    private String destinationText;
    @JMap(value = "nestedSource", classes = Source.class)
    private NestedDestination nestedDestination;

    // Getters, Setters and toString()
}

public class NestedDestination {
    @JMap(value = "nestedSourceText", classes = NestedSource.class)
    private String nestedDestinationText = "Default Destination Text";

    // Getters, Setters and toString()
}

final Source source = new Source();
source.setSourceText("Source text");
source.setNestedSource(new NestedSource());
System.out.println(source);
final JMapper<Destination, Source> mapper = new JMapper<Destination, Source>(Destination.class, Source.class);
final Destination destination = mapper.getDestination(source, NullPointerControl.ALL, MappingType.ONLY_VALUED_FIELDS);
System.out.println(destination);

And the output is the following:

Source{sourceText='Source text', nestedSource=NestedSource{nestedSourceText='null'}}
Destination{destinationText='Source text', nestedDestination=NestedDestination{nestedDestinationText='null'}}

As you can see nestedDestinationText's value has been deleted even if MappingType.ONLY_VALUED_FIELDS is selected. Not that this happens only in nested objects, so if you leave sourceText as nulland populate destinationText with some value, then the value will be presented, e.g.

Source{sourceText='null', nestedSource=NestedSource{nestedSourceText='null'}}
Destination{destinationText='Default Destination Text', nestedDestination=NestedDestination{nestedDestinationText='null'}}
@avurro
Copy link
Member

avurro commented Oct 19, 2016

Hi @petromir,

this is a bug that i'm going to fix, for now you can proceed? or you're stuck?

@avurro avurro added the bug label Oct 19, 2016
@avurro avurro added this to the 1.6.1.CR2 milestone Oct 19, 2016
@avurro avurro self-assigned this Oct 19, 2016
@petromir
Copy link
Author

I'm currently blocked, but I will be happy if you suggest any temporary workaround.

@avurro
Copy link
Member

avurro commented Oct 19, 2016

you can change the setNestedDestinationText method? in the case of it:

public void setNestedDestinationText (String nestedDestinationText){
   if(nestedDestinationText != null)
       this.nestedDestinationText = nestedDestinationText;
}

@avurro
Copy link
Member

avurro commented Oct 19, 2016

However this is a recursive mapping, for nested mapping i mean when you need to map a field with another field in different level, an example of nested mapping using your code is:

public class Source {
    @JMap("{nestedDestination.nestedDestinationText}")
    private String sourceText;
    private NestedSource nestedSource;

    // Getters, Setters and toString()
}

for recursive mapping the problem should not exist, I will try to fix as soon as possible

@petromir
Copy link
Author

petromir commented Oct 19, 2016

We use lombok to avoid boilerplate code, although I can change that until you fix the issue.
Your second example is not our case, but I'm aware of that functionality and also waiting for resolution of #43

avurro added a commit that referenced this issue Oct 19, 2016
@avurro avurro changed the title Default values deleted when nested objects are used MappingType lost in recursive mapping Oct 19, 2016
@avurro
Copy link
Member

avurro commented Oct 19, 2016

ok @petromir! issue fixed! download the project and run: mvn package to get the jar with this fix.
Replace the current one with this generated.
The 1.6.1.CR2 version will be released shortly! stay tuned! 👍

@avurro avurro closed this as completed Oct 19, 2016
@petromir
Copy link
Author

Thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants