Skip to content

Commit

Permalink
Allow 1 and 0 for boolean values in Value#asBoolean
Browse files Browse the repository at this point in the history
in https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#boolean 1 and 0 are also allowed as valid boolean values.
In Some places we read from XML and have StructuredNode.queryValue("VALUE_WITH_0_OR_1").asBoolean(false) which should also return true if the value is 1

- fixes: SE-13483
  • Loading branch information
ymo-sci committed Mar 21, 2024
1 parent 1e093ee commit f77b852
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
23 changes: 14 additions & 9 deletions src/main/java/sirius/kernel/commons/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public String asSmartRoundedString() {
*
* @param defaultValue the value to be used if the wrapped value cannot be converted to a boolean.
* @return <tt>true</tt> if the wrapped value is <tt>true</tt>
* or if the string representation of it is {@code "true"}. Returns <tt>false</tt> otherwise,
* or if the string representation of it is {@code "true"} or {@code "1!"}. Returns <tt>false</tt> otherwise,
* especially if the wrapped value is <tt>null</tt>
*/
public boolean asBoolean(boolean defaultValue) {
Expand All @@ -813,16 +813,21 @@ public boolean asBoolean(boolean defaultValue) {
return booleanValue;
}

// fast-track for common cases without the need to involve NLS framework
if ("true".equalsIgnoreCase(String.valueOf(data))) {
return true;
String stringValue = String.valueOf(data);
return fastPathBoolean(stringValue).orElseGet(() -> {
return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, stringValue.trim()), defaultValue);
});
}

// fast-track for common boolean cases without the need to involve NLS framework
private Optional<Boolean> fastPathBoolean(String value) {
if ("true".equalsIgnoreCase(value) || "1".equals(value)) {
return Optional.of(true);
}
if ("false".equalsIgnoreCase(String.valueOf(data))) {
return false;
if ("false".equalsIgnoreCase(value) || "0".equals(value)) {
return Optional.of(false);
}

return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, String.valueOf(data).trim()),
defaultValue);
return Optional.empty();
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/test/kotlin/sirius/kernel/commons/ValueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,16 @@ class ValueTest {
@Test
fun `Test asBoolean with default`() {
assertEquals(false, Value.of("").asBoolean(false))
assertEquals(false, Value.of("false").asBoolean(false))
assertEquals(true, Value.of("true").asBoolean(false))
assertEquals(false, Value.of(false).asBoolean(false))
assertEquals(false, Value.of("false").asBoolean(false))
assertEquals(false, Value.of(0).asBoolean(false))
assertEquals(false, Value.of("0").asBoolean(false))
assertEquals(false, Value.of(NLS.get("NLS.no")).asBoolean(false))
assertEquals(true, Value.of(true).asBoolean(false))
assertEquals(true, Value.of("true").asBoolean(false))
assertEquals(true, Value.of(1).asBoolean(false))
assertEquals(true, Value.of("1").asBoolean(false))
assertEquals(true, Value.of(NLS.get("NLS.yes")).asBoolean(false))
assertEquals(false, Value.of(NLS.get("NLS.no")).asBoolean(false))
}

@Test
Expand Down

0 comments on commit f77b852

Please sign in to comment.