Given the following classes:
class A {
B b
int data
int data2
}
class B {
C c
String data
}
class C {
Date data
}
The code works fine:
Date now = new Date()
def a = [ data:42, data2:84, b:[ data:'BBB', c:[ data:now ] ] ] as A
assert a.b.c.data == now
assert a.data == 42
assert a.data2 == 84
Now if I omit the data2:84, the code still works fine except for the last assert of course.
BUT! If I "misspell" the property name like:
def a = [ data:42, data22:84, b:[ data:'BBB', c:[ data:now ] ] ] as A
The 1st assert fails with NPE:
java.lang.NullPointerException: Cannot get property 'c' on null object
and what I see is that only the A class gets instantiated with a no-arg constructor, and b and c properties are all nulls.
To make the behaviour more persistent I can see the following options:
* do hard-stop: throw an UnknownPropertyException in case of unknown property
* do the best-effort cast, i.e. skip the unknown properties only, and fill-in the good ones
* provide a control mechanism to switch between the current "silent" behaviour, maybe by introducing new keywords _asStrict_ and _asLenient_
The case is discussed in
https://stackoverflow.com/questions/59423991/groovy-as-keyword-for-map-class-binding