Skip to content
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

[Question] Map nested objects with single mapper #51

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

[Question] Map nested objects with single mapper #51

petromir opened this issue Oct 5, 2016 · 7 comments

Comments

@petromir
Copy link

petromir commented Oct 5, 2016

I have the following case:

public class Destination {
   private String destinationText;
   private NestedDestination nestedDestination;
}

public class NestedDestination {
   private String nestedDestinationData;
}

public class Source {
   @JMap(value = "destinationText", classes = Destination.class))
   private String sourceText;
   @JMap(value = "nestedDestination", classes = Destination.class))
   private NestedSource nestedSource;
}

public class NestedSource {
   @JMap(value = "nestedDestinationData", classes = NestedDestination.class))
   private String nestedSourceData;
}

The example above doesn't work due to:

com.googlecode.jmapper.exceptions.JMapperException:
com.googlecode.jmapper.exceptions.ConversionBodyIllegalCodeException: error in static
   conversion: probably is an incompatibility of signature, the expected input fields do not match
   with the real ones, checks the configured class.

I would like to map the nested object automatically using single mapper like:

JMapper<Destination, Source> mapper = new JMapper<>(Destination.class, Source.class);

without the need to create one for each nested object like:

JMapper<NestedDestination, NestedSource> mapper = new JMapper<>(NestedDestination.class, NestedSource.class);
@avurro
Copy link
Member

avurro commented Oct 5, 2016

Hi @petromir,
You do not need to create a mapper for each recursion, this is a feature already exists in the framework.
Now I create the test with your code, I'll let you know as soon

@petromir
Copy link
Author

petromir commented Oct 5, 2016

Great, waiting for the test.
Do you have an example in the wiki for this case?

@avurro
Copy link
Member

avurro commented Oct 5, 2016

I created the test using your classes and configurations but the test does not return errors, sure there is not anything else? however, below the test code:

JMapper<Destination, Source> mapper = new JMapper<>(Destination.class, Source.class);

NestedSource nestedSource = new NestedSource("nestedSourceData");

Source source = new Source();
source.setSourceText("sourceText");
source.setNestedSource(nestedSource);

Destination destination = mapper.getDestination(source);

assertEquals("sourceText", destination.getDestinationText());
assertEquals("nestedSourceData", destination.getNestedDestination().getNestedDestinationData());

@petromir
Copy link
Author

petromir commented Oct 6, 2016

Sorry, my mistake. It works as expected! Thanks

@petromir petromir closed this as completed Oct 6, 2016
@avurro
Copy link
Member

avurro commented Oct 6, 2016

@petromir classes attribute isn't mandatory, you could reduce configuration

@petromir
Copy link
Author

petromir commented Oct 6, 2016

@avurro Yeah I know but I want to limit it to specific class, if this is the way to do it 😄

@avurro
Copy link
Member

avurro commented Oct 6, 2016

it's the right way 👍

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

No branches or pull requests

2 participants