Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ public Object apply(Object from) {
}

if (targetClass == boolean.class || targetClass == Boolean.class) {
return "true".equals(str);
if ("true".equalsIgnoreCase(str)
|| "T".equalsIgnoreCase(str)
|| "Y".equalsIgnoreCase(str)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}

if (targetClass == BigDecimal.class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.alibaba.fastjson2.issues_7800;

import com.alibaba.fastjson2.util.TypeUtils;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Tag("regression")
public class Issue7842 {
@Test
public void testTrueSpellings() {
for (String s : new String[]{"true", "TRUE", "True", "T", "t", "Y", "y"}) {
assertTrue(TypeUtils.cast(s, Boolean.class), s);
}
}

@Test
public void testFalseSpellings() {
for (String s : new String[]{"false", "FALSE", "False", "0", "F", "f", "N", "n"}) {
assertFalse(TypeUtils.cast(s, Boolean.class), s);
}
}

@Test
public void testLenientFallback() {
assertFalse(TypeUtils.cast("abc", Boolean.class));
assertFalse(TypeUtils.cast("2", Boolean.class));
assertFalse(TypeUtils.cast("YES", Boolean.class));
// "1" stays false: pinned by JSONObjectTest.test_invoke, do not change it here
assertFalse(TypeUtils.cast("1", Boolean.class));
assertNull(TypeUtils.cast("", Boolean.class));
assertNull(TypeUtils.cast("null", Boolean.class));
assertNull(TypeUtils.cast(null, Boolean.class));
}

@Test
public void testPrimitive() {
assertEquals(Boolean.TRUE, TypeUtils.cast("TRUE", boolean.class));
assertEquals(Boolean.FALSE, TypeUtils.cast("1", boolean.class));
assertEquals(Boolean.FALSE, TypeUtils.cast("FALSE", boolean.class));
assertEquals(Boolean.FALSE, TypeUtils.cast("abc", boolean.class));
assertEquals(Boolean.FALSE, TypeUtils.cast("", boolean.class));
}
}