Git Product home page Git Product logo

Comments (5)

jhalterman avatar jhalterman commented on July 22, 2024

What objects are you mapping to each other? I guessed that you might be mapping CatalogOne to CatalogTwo which works out ok:

    CatalogOne source = new CatalogOne();
    source.label = "catalog1";
    source.product = new ProductOne();
    source.product.label = "product1";
    source.product.catalog = source;

    CatalogTwo dest = modelMapper.map(source, CatalogTwo.class);

    assertTrue(dest.product instanceof ProductTwo);
    assertEquals(dest.label, source.label);
    assertEquals(dest.product.label, source.product.label);
    assertEquals(dest.product.catalog, dest);

Let me know what your mapping looks like so I can reproduce your issue.

from modelmapper.

dbundy avatar dbundy commented on July 22, 2024

Hello Jonathan,

Yes for the mapping with the two concrete Catalogs. I do the mapping with ProductOne and ProductTwo too.

I know, it works with this simple example.
My real code is much more complex, i should work on it in a dichotomous way to eliminate the useless noise.

For the moment, I have replaced the implementation by Dozzer because we have not a lot of time to finish our application.

I find your api great to handle generics in the abstract classes, I will come back on the subject when I could take time to propose you a simple example which reproduces the issue.

Cheers
David

from modelmapper.

jhalterman avatar jhalterman commented on July 22, 2024

Hey David - Yes, please do post something that reproduces the issue you're hitting whenever you get a chance. It would be much appreciated.

from modelmapper.

jwicks avatar jwicks commented on July 22, 2024

Jonathan -- I think I can recreate this issue. I'm using 0.5.5. Here is my setup:

Company and Location objects map to CompanyModel and LocationModel. Company/CompanyModel has a property for Location/LocationModel. All objects have a Date field createdAt. ModelMapper throws configuration error because destination property Location.setCreatedAt() matches source property LocationModel.getCreatedAt() and CompanyModel.getCreatedAt(). I have explicitly mapped destination and source in PropertyMaps. I am using STRICT matching strategy.

public class Company {
    private String name;
    private Location location;
    private Date createdAt;
    //getters/setters removed for brevity
}
public class Location {
    private String address;
    private Date createdAt;
}

public class CompanyModel {
    private String name;
    private LocationModel location;
    private Date createdAt;
}
public class LocationModel {
    private String address;
    private Date createdAt;
}

public class CompanyModelMap extends PropertyMap<CompanyModel, Company> {
    @Override
    protected void configure() {
        //Explicit mappings for createdAt/updatedAt
        map().setCreatedAt(source.getCreatedAt());
    }
}
public class LocationModelMap extends PropertyMap<LocationModel, Location> {
    @Override
    protected void configure() {
        //Explicit mappings for createdAt/updatedAt
        map().setCreatedAt(source.getCreatedAt());
    }
}

public class MapTest {
    @Test
    public void companyModelShouldMap() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration()
            .setMatchingStrategy(MatchingStrategies.STRICT);

        modelMapper.addMappings(new CompanyModelMap()); //Exception thrown here
        modelMapper.addMappings(new LocationModelMap());

        Company company = new Company();
        company.setName("Pepsi Co.");
        Location location = new Location();
        company.setLocation(location);
        location.setAddress("1234 Main St.");

        CompanyModel model = modelMapper.map(company, CompanyModel.class);

        assertNotNull(model);
        assertEquals(company.getName(), model.getName());
        assertNotNull(model.getLocation());
        assertEquals(company.getLocation().getAddress(), model.getLocation().getAddress());
    }
}

org.modelmapper.ConfigurationException: ModelMapper configuration errors:

  1. The destination property com.github.modelmapper.test.Location.setCreatedAt() matches multiple source property hierarchies:
    com.github.modelmapper.test.CompanyModel.getLocation()/com.github.modelmapper.test.LocationModel.getCreatedAt()
    com.github.modelmapper.test.CompanyModel.getLocation()/com.github.modelmapper.test.CompanyModel.getCreatedAt()

1 error
at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:225)
at org.modelmapper.internal.PropertyMappingBuilder.matchDestination(PropertyMappingBuilder.java:142)
at org.modelmapper.internal.PropertyMappingBuilder.matchDestination(PropertyMappingBuilder.java:135)
at org.modelmapper.internal.PropertyMappingBuilder.build(PropertyMappingBuilder.java:80)
at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:95)
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:92)
at com.github.modelmapper.test.MapTest.companyModelShouldMap(MapTest.java:25)

from modelmapper.

jhalterman avatar jhalterman commented on July 22, 2024

Thanks for reporting this. I found an issue with the way things were being handled when a match was found using the EXACT matching strategy. @dbundy and @jwicks Let me know if this does not solve your issue.

from modelmapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.