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

Duplication of collections elements #667

Closed
leonardoluiz opened this issue Jul 18, 2018 · 8 comments
Closed

Duplication of collections elements #667

leonardoluiz opened this issue Jul 18, 2018 · 8 comments
Labels

Comments

@leonardoluiz
Copy link
Contributor

leonardoluiz commented Jul 18, 2018

Whats your runtime?

  • Dozer version: 6.2.0
  • OS version: Win 10
  • JDK version: JDK8

Whats the problem?

When a class is configured with class level is-accessible="true", and a List attribute in this class is annotated with @mapping, the elements of that attribute are being duplicated.

Steps to reproduce:

  1. Configure a class with is-accessible="true".
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
  <mapping>
    <class-a is-accessible="true">Source</class-a>
    <class-b is-accessible="true">Destination</class-b>
  </mapping>
</mappings>
  1. Map a collection attribute with @mapping
    public static class Destination {

        @Mapping
        private List<String> items;

Observed Results:

The destination attribute will have two elements (destination.items.size() = 2).

Expected Results:

The destination attribute should have the same number of elements found in the source object.

Link to GitHub repo with Unit test

https://github.com/leonardoluiz/dozer-testcases/blob/master/src/test/java/org/dozer/testcase/MappingDuplicatesCollectionItemsTest.java

leonardoluiz added a commit to leonardoluiz/dozer that referenced this issue Jul 18, 2018
leonardoluiz added a commit to leonardoluiz/dozer that referenced this issue Jul 18, 2018
@garethahealy
Copy link
Collaborator

@leonardoluiz ; it looks like you've committed a fix to your own branch, but not raised a PR. Can you raise a PR?

@orange-buffalo
Copy link
Contributor

@garethahealy actually there are two possible fixes.

One is local to Mapping annotation processing - it helps to resolve the issue, does not break anything existing, but maybe it is too local and we can catch the same problem in other places.

The second fix is global for addFieldMapping method - if there is a duplicated mapping for a field detected, this method fails. It could be more promising, as in general case it is not clear how to proceed with multiple mappings for a field: which one should take priority? But with this fix there are many existing tests failing, so it is not clear if this is a proper way and probably it is not backwards compatible.

What do you think, what is the best way to proceed here?

@grubeninspekteur
Copy link
Contributor

@orange-buffalo if the second solution prevents mappings like

<field>
<a>a</a>
<b>c</b>
</field>
<field>
<a>b</a>
<b>c</b>
</field>

Then this would break our production code. We use these in conjunction with map-null=false to map either-or fields.

@orange-buffalo
Copy link
Contributor

@grubeninspekteur thank you for your feedback! It looks like the only option for us is to have a local fix for @Mapping processing. @leonardoluiz, could you please prepare a PR with this change?

@leonardoluiz
Copy link
Contributor Author

PR submitted. Thanks! @garethahealy @orange-buffalo @grubeninspekteur

@orange-buffalo
Copy link
Contributor

@leonardoluiz thank you!
@garethahealy is there any chance to release Dozer with this fix? Our project needs it to migrate to the 6.x.

@garethahealy
Copy link
Collaborator

@orange-buffalo
Copy link
Contributor

@garethahealy many thanks, we appreciate this!

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

No branches or pull requests

4 participants