-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Evaluate "false" as false #8
Comments
Some real example? |
I'm sorry, it seems I was actually wrong and the System.out.println(Ognl.getValue("true", new Object())); // true
System.out.println(Ognl.getValue("false", new Object())); // false
System.out.println(Ognl.getValue("'true'", new Object())); // true
System.out.println(Ognl.getValue("'false'", new Object())); // false
System.out.println(Ognl.getValue("true and 'true'", new Object())); // true
System.out.println(Ognl.getValue("true and 'false'", new Object())); // false
System.out.println(Ognl.getValue("!'true'", new Object())); // false
// But the ! operator works differently here...
System.out.println(Ognl.getValue("!'false'", new Object())); // false So it is only in the way |
For the record: equivalent code using Spring EL (needs the public static void main(String[] args) {
SpelExpressionParser parser = new SpelExpressionParser();
System.out.println(evaluate(parser, "true"));
System.out.println(evaluate(parser, "false"));
System.out.println(evaluate(parser, "'true'"));
System.out.println(evaluate(parser, "'false'"));
System.out.println(evaluate(parser, "true and true"));
System.out.println(evaluate(parser, "true and false"));
System.out.println(evaluate(parser, "true and 'true'"));
System.out.println(evaluate(parser, "true and 'false'"));
System.out.println(evaluate(parser, "!'true'"));
System.out.println(evaluate(parser, "!'false'"));
}
private static Object evaluate(SpelExpressionParser parser, String expression) {
SpelExpression exp = (SpelExpression) parser.parseExpression(expression);
return exp.getValue();
} Returns:
|
Problem is here non-null object means |
I have added if ( c == String.class )
return Boolean.parseBoolean(String.valueOf(value)); and deployed snapshot to Sonatype, could you test that? |
Works perfectly for me, thanks! |
but this breaks backward compatibility... |
Well, for that specific case ( |
I'm releasing a new version (3.0.12) but without this fix. Then I will release another version (3.1) which will include this fix. |
OGNL currently evaluates
String
objects with any value totrue
, but this makes OGNL's boolean coercion incompatible with two other very used Expression Languages: JSP EL and Spring EL, both of which would evaluate"true"
and"false"
as booleanstrue
andfalse
, respectively (even if they diverge in their evaluation of other non-booleanishString
values).It would be great if OGNL modified this behaviour in order to evaluate
"false"
asfalse
(and better even if"off"
and"no"
were included in the pack, but that might be asking too much ;-)).For me, it would allow me to remove this hugely ugly, javassist-based hack from thymeleaf: https://github.com/thymeleaf/thymeleaf/blob/159f963907e9d5e3943843031845df28934adafc/src/main/java/org/thymeleaf/standard/expression/OgnlVariableExpressionEvaluator.java#L201-L261
The text was updated successfully, but these errors were encountered: