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

Implicit mapping from a Map to a Object will be ignored. #370

Closed
chhsiao90 opened this issue Aug 1, 2018 · 1 comment
Closed

Implicit mapping from a Map to a Object will be ignored. #370

chhsiao90 opened this issue Aug 1, 2018 · 1 comment
Labels

Comments

@chhsiao90
Copy link
Member

stackoverflow: https://stackoverflow.com/questions/51625677/model-mapper-mapping-mapstring-object-to-class-which-extends-another-generic-c

unit test

@Test
public class GenericElementMapping2 extends AbstractTest {
  static class Field {
  }

  static class SourceField extends Field {
    String value;

    public SourceField(String value) {
      this.value = value;
    }
  }

  static class DestField extends Field {
    String value;
  }

  static class BaseClass<T extends Field> {
    List<T> fields;
  }

  static class Destination extends BaseClass<DestField> {
    String name;
  }

  public void shouldMap() {
    List<SourceField> sourceFields = Collections.singletonList(new SourceField("foo"));
    Map<String, Object> source = new HashMap<String, Object>();
    source.put("fields", sourceFields);
    source.put("name", "bar");

    Destination destination = modelMapper.map(source, Destination.class);
    assertEquals(destination.name, "bar");
    assertNotNull(destination.fields);
    assertEquals(destination.fields.size(), 1);
    assertEquals(destination.fields.get(0).getClass(), DestField.class);
    assertEquals(destination.fields.get(0).value, "foo");
  }
}
@chhsiao90
Copy link
Member Author

I just release v2.1.1, please have a look, thanks.

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

1 participant