Git Product home page Git Product logo

Comments (7)

jhalterman avatar jhalterman commented on July 3, 2024

Good catch. Did you come across this in practice or just notice from checking out the code? Do you happen to have any tests that demonstrate the issue?

from modelmapper.

jhalterman avatar jhalterman commented on July 3, 2024

I'm not sure if this would happen in practice (feel free to post a test if you can think of one), but the proposed fix is good regardless. Thanks.

from modelmapper.

janmatejka avatar janmatejka commented on July 3, 2024

Hello Jonathan,
incorrect source detection we observerd in our project functional tests.
Unfortunately at this moment I have no simple test to reproduce bad
behavior. But after change from HashMap to IdentityHashMap mapping in our
test works correctly.

Jan

2012/10/17 Jonathan Halterman [email protected]

I'm not sure if this would happen in practice (feel free to post a test if
you can think of one), but the proposed fix is good regardless. Thanks.


Reply to this email directly or view it on GitHubhttps://github.com//issues/15#issuecomment-9510817.

from modelmapper.

jonas-lauber avatar jonas-lauber commented on July 3, 2024

I ran into this problem and this is really cumbersome.. Using an IdentityHashMap only solve part of the problem... Because of wrapper classes caching (https://www.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching), any Integers between -128 to 127 share the same instances...
I have an object with multiple Integer fields that are mapped through custom Converters to different object types and when the integer fields are the same value in the source, this get all messed up... "D circularDest = contextImpl.destinationForSource();" in MappingEngineImpl#map:83 (version 0.6.5) returns the first mapped field even if the destination type is not the good one...

In my opinion, one solution would be to have "if (circularDest != null && contextImpl.getDestinationType().equals(circularDest)) return circularDest;" in MappingEngineImpl#map:84. What do you think about it ?

Jonas

from modelmapper.

jhalterman avatar jhalterman commented on July 3, 2024

@jonas-lauber Do you have a failing test you can share that reproduces the problem you're seeing?

from modelmapper.

jonas-lauber avatar jonas-lauber commented on July 3, 2024

Yeah sure! Here it is:

public class Test {

    public static void main(String args[]){
        // Create mapper
        ModelMapper mapper = new ModelMapper();

        // Add custom converter mapping on Integer
        mapper.addConverter(new AbstractConverter<Integer, ComplexObject>() {
            @Override
            protected ComplexObject convert(Integer source) {;
                return new ComplexObject(source);
            }
        });

        // Create a source object with two integers wrapping the SAME VALUE
        SourceObject sourceObject = new SourceObject(10, 10);

        // Create a destination (no matter its params)
        DestinationObject destinationObject =  new DestinationObject(30, new ComplexObject(40));

        // Map the source to the destination
        // => Because of https://www.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching
        // complexObject and id are seen like the same value by the circular reference mechanism.
        mapper.map(sourceObject, destinationObject);
    }

    static class SourceObject {
        private Integer complexObject;
        private Integer id;
        public SourceObject(Integer id, Integer complexObject) {
            this.id = id;
            this.complexObject = complexObject;
        }
        public Integer getComplexObject() {
            return complexObject;
        }
        void setComplexObject(Integer complexObject) {
            this.complexObject = complexObject;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
    }

    static class DestinationObject {
        private ComplexObject complexObject;
        private Integer id;
        public DestinationObject(){/*non-private no-argument constructor for mapping*/};
        public DestinationObject(Integer id, ComplexObject complexObject) {
            this.id = id;
            this.complexObject = complexObject;
        }
        public ComplexObject getComplexObject() {
            return complexObject;
        }
        public void setComplexObject(ComplexObject complexObject) {
            this.complexObject = complexObject;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
    }

    static class ComplexObject {
        private Integer id;
        public ComplexObject(Integer id){
            this.id = id;
        }
    }
}

from modelmapper.

jhalterman avatar jhalterman commented on July 3, 2024

Well found. I created a separate issue to track this. What I think I'd like to do is simply not track boxed primitives for the purposes of circular references since primitives will never cause circular references (AFAIK) in the way that other model classes can.

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.