Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/main/java/com/aliyun/tea/TeaModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ private static Object buildObject(Object o, Class self, Type subType, String obj
Map<String, Object> valueMap = (Map<String, Object>) o;
Map<String, Object> result = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : valueMap.entrySet()) {
if (null == subType || subType instanceof WildcardType) {
if (null == entry.getValue()) {
result.put(entry.getKey(), null);
} else if (null == subType || subType instanceof WildcardType) {
result.put(entry.getKey(), entry.getValue());
} else if (subType instanceof Class) {
result.put(entry.getKey(), buildObject(entry.getValue(), (Class) subType, null, objectName));
Expand All @@ -132,7 +134,9 @@ private static Object buildObject(Object o, Class self, Type subType, String obj
List<Object> valueList = (List<Object>) o;
List<Object> result = new ArrayList<Object>();
for (Object object : valueList) {
if (null == subType || subType instanceof WildcardType) {
if (null == object) {
result.add(null);
} else if (null == subType || subType instanceof WildcardType) {
result.add(object);
} else if (subType instanceof Class) {
result.add(buildObject(object, (Class) subType, null, objectName));
Expand Down
30 changes: 28 additions & 2 deletions src/test/java/com/aliyun/tea/TeaModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public static class SubModel extends TeaModel {

@NameInMap("writeable")
public OutputStream writeable;

@NameInMap("mapTest")
public Map<String, Object> mapTest;

@NameInMap("listMapTest")
public List<Map<String, Object>> listMapTest;
}

@Test
Expand Down Expand Up @@ -109,6 +115,26 @@ public void toModelTest() {
submodel = TeaModel.toModel(map, new SubModel());
Assert.assertEquals("2", submodel.baseDriveResponse.driveId);

Map<String, Object> mapTest = new HashMap<>();
mapTest.put("str", "test");
mapTest.put("bool", false);
mapTest.put("num", 0);
mapTest.put("null", null);
map.put("mapTest", mapTest);
List<Map<String, Object>> listMapTest = new ArrayList<>();
listMapTest.add(null);
listMapTest.add(mapTest);
listMapTest.add(null);
map.put("listMapTest", listMapTest);
submodel = TeaModel.toModel(map, new SubModel());
Assert.assertEquals("test", submodel.mapTest.get("str"));
Assert.assertEquals(false, submodel.mapTest.get("bool"));
Assert.assertEquals(0, submodel.mapTest.get("num"));
Assert.assertNull(submodel.mapTest.get("null"));
Assert.assertNull(submodel.listMapTest.get(0));
Assert.assertEquals(4, submodel.listMapTest.get(1).size());
Assert.assertNull(submodel.listMapTest.get(2));

map.clear();
List<BaseDriveResponse> baseDriveResponseList = new ArrayList<>();
baseDriveResponseList.add(baseDriveResponse);
Expand Down Expand Up @@ -161,7 +187,7 @@ public void toMap() {

Map<String, Object> map = submodel.toMap();
System.out.println(map.toString());
Assert.assertEquals(9, map.size());
Assert.assertEquals(11, map.size());
Assert.assertEquals("the access key id", map.get("access_key_id"));
Assert.assertEquals("the access token", map.get("accessToken"));
ArrayList list = (ArrayList) map.get("listTest");
Expand All @@ -179,7 +205,7 @@ public void toMapAdvance() {
model.subModel = submodel;
Map<String, Object> m = TeaModel.toMap(model);
Map<String, Object> sub = (Map<String, Object>) m.get("sub_model");
Assert.assertEquals(9, sub.size()); // except Stream properties
Assert.assertEquals(11, sub.size()); // except Stream properties
}

@Test
Expand Down