Skip to content

Commit

Permalink
Fix issue Set cannot be cast to List(#830) (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangytm authored Jul 22, 2024
1 parent 36d6245 commit 8efd82f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,13 @@ private Object mapCollection(Object srcObj, Object srcCollectionValue, FieldMap
result = addToSet(srcObj, fieldMap, Arrays.asList((Object[])srcCollectionValue), destObj);
} else if (CollectionUtils.isCollection(srcFieldType) && CollectionUtils.isSet(destCollectionType)) {
// Collection to Set
result = addToSet(srcObj, fieldMap, (Collection<?>)srcCollectionValue, destObj);
} else if (CollectionUtils.isCollection(srcFieldType) && MappingUtils.isSupportedMap(destCollectionType)) {
result = addToSet(srcObj, fieldMap, (Collection<?>) srcCollectionValue, destObj);
} else if (CollectionUtils.isList(srcFieldType) && MappingUtils.isSupportedMap(destCollectionType)) {
// List to Map value
result = mapListToList(srcObj, (List<?>)srcCollectionValue, fieldMap, destObj);
result = mapListToList(srcObj, (List<?>) srcCollectionValue, fieldMap, destObj);
} else if (CollectionUtils.isSet(srcFieldType) && MappingUtils.isSupportedMap(destCollectionType)) {
// Set to Map value
result = mapListToList(srcObj, (Set<?>) srcCollectionValue, fieldMap, destObj);
} else if (CollectionUtils.isCollection(srcFieldType) && CollectionUtils.isList(destCollectionType)) {
// List to List
// Set to List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package com.github.dozermapper.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.github.dozermapper.core.vo.A;
import com.github.dozermapper.core.vo.B;
Expand Down Expand Up @@ -156,5 +160,40 @@ public int hashCode() {
return id;
}
}
public static class Source {
Map<String, Set<String>> map;

}
public Map<String, Set<String>> getMap() {
return map;
}

public void setMap(Map<String, Set<String>> map) {
this.map = map;
}
}

public static class Target {
Map<String, Set<String>> map;

public Map<String, Set<String>> getMap() {
return map;
}

public void setMap(Map<String, Set<String>> map) {
this.map = map;
}
}

@Test
public void testSourceToTargetMapping() {
final Mapper mapper = DozerBeanMapperBuilder.create().build();
Source source = new Source();
Map<String, Set<String>> sourceMap = new HashMap<>();
sourceMap.put("foo", new HashSet<>(List.of("bar")));
source.setMap(sourceMap);
Target target = mapper.map(source, Target.class);
assertNotNull(target.getMap());
assertEquals(source.getMap().size(), target.getMap().size());

}
}

0 comments on commit 8efd82f

Please sign in to comment.