Skip to content

Commit

Permalink
validate indexer expression on Unknown and on non-map non-list TypedC…
Browse files Browse the repository at this point in the history
…lass (#7117)
  • Loading branch information
mslabek authored Oct 31, 2024
1 parent 1847edd commit 1456449
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ private[spel] class Typer(
invalidNodeResult(IllegalIndexingOperation)
case TypedObjectWithValue(underlying, _) => typeIndexer(e, underlying)
case Unknown =>
validNodeResult(Unknown)
withTypedChildren(_ => valid(Unknown))
case _: TypedClass =>
val w = validNodeResult(Unknown)
val w = withTypedChildren(_ => valid(Unknown))
if (dynamicPropertyAccessAllowed) w else w.tell(List(DynamicPropertyAccessError))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ class SpelExpressionSpec extends AnyFunSuite with Matchers with ValidatedValuesD

private val ctx = Context("abc").withVariables(
Map(
"obj" -> testValue,
"strVal" -> "",
"mapValue" -> Map("foo" -> "bar").asJava,
"array" -> Array("a", "b"),
"intArray" -> Array(1, 2, 3),
"nestedArray" -> Array(Array(1, 2), Array(3, 4)),
"arrayOfUnknown" -> Array("unknown".asInstanceOf[Any]),
"unknownString" -> ContainerOfUnknown("unknown"),
"setVal" -> Set("a").asJava,
"obj" -> testValue,
"strVal" -> "",
"mapValue" -> Map("foo" -> "bar").asJava,
"array" -> Array("a", "b"),
"intArray" -> Array(1, 2, 3),
"nestedArray" -> Array(Array(1, 2), Array(3, 4)),
"arrayOfUnknown" -> Array("unknown".asInstanceOf[Any]),
"unknownString" -> ContainerOfUnknown("unknown"),
"setVal" -> Set("a").asJava,
"containerWithUnknownObject" -> ContainerOfUnknown(SampleValue(1)),
"containerWithUnknownObjectWithStaticMethods" -> ContainerOfUnknown(new JavaClassWithStaticParameterlessMethod()),
"containerWithUnknownClassWithStaticMethods" -> ContainerOfUnknown(
Expand Down Expand Up @@ -1398,20 +1398,36 @@ class SpelExpressionSpec extends AnyFunSuite with Matchers with ValidatedValuesD
}
}

test("should not validate array constructor") {
test("should not allow array constructor") {
List("new String[]", "new String[ ]", "new String[0]", "new String[#invalidRef]", "new String[invalidSyntax]").map(
illegalExpr => parse[Any](illegalExpr, ctx).invalidValue shouldBe NonEmptyList.one(ArrayConstructorError)
)
}

test("indexing on maps and lists should validate nodes inside indexer") {
test("indexing on maps and lists should validate expression inside indexer") {
List("#processHelper.stringOnStringMap[#invalidRef]", "{1,2,3}[#invalidRef]").map(expr =>
parse[Any](expr, ctxWithGlobal).invalidValue shouldBe NonEmptyList.one(
UnresolvedReferenceError("invalidRef")
)
)
}

test("indexing on unknown should validate expression inside indexer") {
parse[Any]("#unknownString.value[#invalidRef]", ctx).invalidValue shouldBe NonEmptyList.one(
UnresolvedReferenceError("invalidRef")
)
}

test("indexing on class should validate expression inside indexer") {
parse[Any](
"T(java.time.LocalDate)[#invalidRef]",
ctx,
dynamicPropertyAccessAllowed = true
).invalidValue shouldBe NonEmptyList.one(
UnresolvedReferenceError("invalidRef")
)
}

test("should return correct type in array projection") {
evaluate[Any]("#array.![#this]") shouldBe Array("a", "b")
}
Expand Down

0 comments on commit 1456449

Please sign in to comment.