Skip to content
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

Sets root on context if null #337

Merged
merged 2 commits into from
Jan 4, 2025
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
7 changes: 4 additions & 3 deletions src/main/java/ognl/Ognl.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,18 @@ public static Object getValue(Object tree, OgnlContext context, Object root) thr
*/
public static Object getValue(Object tree, OgnlContext context, Object root, Class<?> resultType) throws OgnlException {
Object result;
OgnlContext ognlContext = addDefaultContext(root, context);

Node node = (Node) tree;

if (node.getAccessor() != null) {
result = node.getAccessor().get(context, root);
result = node.getAccessor().get(ognlContext, root);
} else {
result = node.getValue(context, root);
result = node.getValue(ognlContext, root);
}

if (resultType != null) {
result = getTypeConverter(context).convertValue(context, root, null, null, result, resultType);
result = getTypeConverter(ognlContext).convertValue(ognlContext, root, null, null, result, resultType);
}
return result;
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/ognl/test/ShortCircuitingExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ void shouldThrowException() {
}
}

public static class B {
private String b;

public B(String b) {
this.b = b;
}

public String getB() {
return b;
}
}

public static class Params {
public B[] params;

public Params(B[] params) {
this.params = params;
}

public B[] getParams() {
return params;
}
}

@Test
void shouldCompare() throws OgnlException {
Object root = new Params(new B[]{new B("a")});

OgnlContext ctx = Ognl.createDefaultContext(null);

Object val1 = Ognl.getValue("\"a\".equals(params[0].b)", ctx, root);
Object val2 = Ognl.getValue("\"a\".equals(params[0].b)", root);

assertEquals(Boolean.TRUE, val1);
assertEquals(Boolean.TRUE, val2);
}

private static Stream<Arguments> testValues() {
return Stream.of(
Arguments.of("#root ? someProperty : 99", 99),
Expand Down
Loading