Skip to content

Commit 8efd82f

Browse files
authored
Fix issue Set cannot be cast to List(#830) (#864)
1 parent 36d6245 commit 8efd82f

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

core/src/main/java/com/github/dozermapper/core/MappingProcessor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,13 @@ private Object mapCollection(Object srcObj, Object srcCollectionValue, FieldMap
630630
result = addToSet(srcObj, fieldMap, Arrays.asList((Object[])srcCollectionValue), destObj);
631631
} else if (CollectionUtils.isCollection(srcFieldType) && CollectionUtils.isSet(destCollectionType)) {
632632
// Collection to Set
633-
result = addToSet(srcObj, fieldMap, (Collection<?>)srcCollectionValue, destObj);
634-
} else if (CollectionUtils.isCollection(srcFieldType) && MappingUtils.isSupportedMap(destCollectionType)) {
633+
result = addToSet(srcObj, fieldMap, (Collection<?>) srcCollectionValue, destObj);
634+
} else if (CollectionUtils.isList(srcFieldType) && MappingUtils.isSupportedMap(destCollectionType)) {
635635
// List to Map value
636-
result = mapListToList(srcObj, (List<?>)srcCollectionValue, fieldMap, destObj);
636+
result = mapListToList(srcObj, (List<?>) srcCollectionValue, fieldMap, destObj);
637+
} else if (CollectionUtils.isSet(srcFieldType) && MappingUtils.isSupportedMap(destCollectionType)) {
638+
// Set to Map value
639+
result = mapListToList(srcObj, (Set<?>) srcCollectionValue, fieldMap, destObj);
637640
} else if (CollectionUtils.isCollection(srcFieldType) && CollectionUtils.isList(destCollectionType)) {
638641
// List to List
639642
// Set to List

core/src/test/java/com/github/dozermapper/core/MappingProcessorTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
package com.github.dozermapper.core;
1717

1818
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.HashSet;
1921
import java.util.List;
22+
import java.util.Map;
23+
import java.util.Set;
2024

2125
import com.github.dozermapper.core.vo.A;
2226
import com.github.dozermapper.core.vo.B;
@@ -156,5 +160,40 @@ public int hashCode() {
156160
return id;
157161
}
158162
}
163+
public static class Source {
164+
Map<String, Set<String>> map;
159165

160-
}
166+
public Map<String, Set<String>> getMap() {
167+
return map;
168+
}
169+
170+
public void setMap(Map<String, Set<String>> map) {
171+
this.map = map;
172+
}
173+
}
174+
175+
public static class Target {
176+
Map<String, Set<String>> map;
177+
178+
public Map<String, Set<String>> getMap() {
179+
return map;
180+
}
181+
182+
public void setMap(Map<String, Set<String>> map) {
183+
this.map = map;
184+
}
185+
}
186+
187+
@Test
188+
public void testSourceToTargetMapping() {
189+
final Mapper mapper = DozerBeanMapperBuilder.create().build();
190+
Source source = new Source();
191+
Map<String, Set<String>> sourceMap = new HashMap<>();
192+
sourceMap.put("foo", new HashSet<>(List.of("bar")));
193+
source.setMap(sourceMap);
194+
Target target = mapper.map(source, Target.class);
195+
assertNotNull(target.getMap());
196+
assertEquals(source.getMap().size(), target.getMap().size());
197+
198+
}
199+
}

0 commit comments

Comments
 (0)