Skip to content
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
<version>1.4.2</version>
<version>1.4.3</version>
<packaging>jar</packaging>

<name>tea</name>
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/aliyun/tea/TeaModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,27 +359,27 @@ public static Object confirmType(Class expect, Object object, String objectName)
BigDecimal bigDecimal;
if (String.class.isAssignableFrom(expect)) {
if (object instanceof Number || object instanceof Boolean) {
logger.info("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, String.class.getName(), object.getClass().getName(), object.toString());
return object.toString();
}
if (object instanceof Map || object instanceof List) {
logger.info("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, String.class.getName(), object.getClass().getName(), object.toString());
return new Gson().toJson(object);
}
} else if (Boolean.class.isAssignableFrom(expect)) {
if (object instanceof String) {
logger.info("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Boolean.class.getName(), object.getClass().getName(), object.toString());
return Boolean.parseBoolean(String.valueOf(object));
} else if (object instanceof Number) {
if (object.toString().equals("1")) {
logger.info("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Boolean.class.getName(), object.getClass().getName(), object.toString());
return true;
} else if (object.toString().equals("0")) {
logger.info("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Boolean.class.getName(), object.getClass().getName(), object.toString());
return false;
}
Expand All @@ -389,28 +389,28 @@ public static Object confirmType(Class expect, Object object, String objectName)
try {
Integer.parseInt(object.toString());
} catch (NumberFormatException e) {
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Integer.class.getName(), object.getClass().getName(), object.toString());
}
bigDecimal = new BigDecimal(object.toString());
return bigDecimal.intValue();
}
if (object instanceof Boolean) {
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Integer.class.getName(), object.getClass().getName(), object.toString());
return object.toString().equalsIgnoreCase("true") ? 1 : 0;
}
if (object instanceof Long) {
if ((Long) object > Integer.MAX_VALUE) {
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Integer.class.getName(), object.getClass().getName(), object.toString());
}
bigDecimal = new BigDecimal(object.toString());
return bigDecimal.intValue();
}
if (object instanceof Float || object instanceof Double) {
bigDecimal = new BigDecimal(object.toString());
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Integer.class.getName(), object.getClass().getName(), object.toString());
return bigDecimal.intValue();
}
Expand All @@ -419,15 +419,15 @@ public static Object confirmType(Class expect, Object object, String objectName)
try {
Long.parseLong(object.toString());
} catch (NumberFormatException e) {
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Long.class.getName(), object.getClass().getName(), object.toString());
}
bigDecimal = new BigDecimal(object.toString());
return bigDecimal.longValue();
}
if (object instanceof Float || object instanceof Double) {
bigDecimal = new BigDecimal(object.toString());
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Long.class.getName(), object.getClass().getName(), object.toString());
return bigDecimal.longValue();
}
Expand All @@ -436,23 +436,23 @@ public static Object confirmType(Class expect, Object object, String objectName)
try {
Float.parseFloat(object.toString());
} catch (NumberFormatException e) {
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Float.class.getName(), object.getClass().getName(), object.toString());
}
bigDecimal = new BigDecimal(object.toString());
return bigDecimal.floatValue();
}
if (object instanceof Double) {
if ((Double) object > Float.MAX_VALUE) {
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Float.class.getName(), object.getClass().getName(), object.toString());
}
bigDecimal = new BigDecimal(object.toString());
return bigDecimal.floatValue();
}
if (object instanceof Integer || object instanceof Long) {
bigDecimal = new BigDecimal(object.toString());
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Float.class.getName(), object.getClass().getName(), object.toString());
return bigDecimal.floatValue();
}
Expand All @@ -461,15 +461,15 @@ public static Object confirmType(Class expect, Object object, String objectName)
try {
Double.parseDouble(object.toString());
} catch (NumberFormatException e) {
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Double.class.getName(), object.getClass().getName(), object.toString());
}
bigDecimal = new BigDecimal(object.toString());
return bigDecimal.doubleValue();
}
if (object instanceof Integer || object instanceof Long) {
bigDecimal = new BigDecimal(object.toString());
logger.warning("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
logger.verbose("[{}] There are some cast events happening. expect: {}, but: {}, value: {}.",
objectName, Double.class.getName(), object.getClass().getName(), object.toString());
return bigDecimal.doubleValue();
}
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/com/aliyun/tea/TeaModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,51 @@ public void confirmTypeTest() {
Assert.assertEquals("[\"1\",2,true,{\"sub\":\"sub\"}]", object6);

}

@Test
public void confirmTypeShrinkFiltersMapToString() {
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("Operator", "IN");
List<String> values = new ArrayList<String>();
values.add("po-000001bctotzplhahu1d");
filter.put("Value", values);
filter.put("Name", "policyId");

List<Object> filters = new ArrayList<Object>();
filters.add(filter);

Map<String, Object> filtersWrapper = new HashMap<String, Object>();
filtersWrapper.put("Filters", filters);

Object result = TeaModel.confirmType(String.class, filtersWrapper,
"com.aliyun.hbrpolicyinner20260527.models.ListPolicyBindingsShrinkRequest.filtersShrink");
Assert.assertEquals(String.class, result.getClass());
String json = (String) result;
Assert.assertTrue(json.contains("\"Filters\""));
Assert.assertTrue(json.contains("\"Operator\":\"IN\""));
Assert.assertTrue(json.contains("\"Name\":\"policyId\""));
Assert.assertTrue(json.contains("po-000001bctotzplhahu1d"));

Object listJson = TeaModel.confirmType(String.class, filters,
"com.aliyun.hbrpolicyinner20260527.models.ListPolicyBindingsShrinkRequest.filtersShrink");
Assert.assertEquals(String.class, listJson.getClass());
Assert.assertTrue(((String) listJson).startsWith("["));
Assert.assertTrue(((String) listJson).contains("\"Operator\":\"IN\""));
}

@Test
public void confirmTypeInvalidFloatDoubleStringStillLogsVerboseThenFails() {
try {
TeaModel.confirmType(Float.class, "not-a-number", "field.float");
Assert.fail("expected NumberFormatException");
} catch (NumberFormatException e) {
// Float.parseFloat fails → verbose cast log; BigDecimal then throws
}
try {
TeaModel.confirmType(Double.class, "not-a-number", "field.double");
Assert.fail("expected NumberFormatException");
} catch (NumberFormatException e) {
// same path for Double
}
}
}
Loading