Comments (7)
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.
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.
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.
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.
@jonas-lauber Do you have a failing test you can share that reproduces the problem you're seeing?
from modelmapper.
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.
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)
- How to skip not loaded Lazy fields?
- Test fails after upgrading from 3.1.0 to 3.1.1
- Java 21 support HOT 18
- Support of Java Module System
- Custom mapping in nested objects HOT 4
- Skip field of generic class HOT 1
- Mapping Source object to Destination object
- Mapping nested generic object with generic HOT 1
- Use specific converter for one field
- Model Mapper does not map values correctly (Intermittent - once in a few tries)
- Model Mapper Low performance
- Jackson Module: @JsonTypeInfo support for automatically handling inheritance HOT 1
- ModelMapper fails to map nested entity user field HOT 1
- When condition does not work for null-field
- How to ignore mapping errors HOT 1
- ModelMapper not working with nested generics
- modelmapper Hot deployment HOT 1
- How to Throw Validation exception when Source has a property that cannot be mapped to Destination HOT 1
- Test dependencies upgrade
- Basic Mapping doesn't work as expected.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modelmapper.