Details
Description
This is a suggestion to make the behaviour of Grails Domain Classes more like Groovy POGOs.
In Groovy:
class Tester {
}
new Tester(myProp:'hello')
leads to the expected Exception: groovy.lang.MissingPropertyException: No such property: myProb for class: Tester
But when defining the Tester class as Domain class (grails-app/domain) and instantiate it as above, everything
works fine: No exception and the object is created.
It would be much more save throwing a MissingPropertyException, if a non existing property is used in the
named parameter list of a constructor.
Without, it gets very hard to detect wrong parameter lists and could lead to an error-prone application.
Currently new Tester(params) is used to bind request parameters.
The second alternative for this is tester.properties=params.
Suggestion:
- new Tester(params) would throw an MissingPropertyException
- tester.properties=params works unchanged. This doesn't conflict to Groovy because tester.properties is read-only and doesn't allow any assignments at all
Workaround for 1.0.x:
- Use something like Tester.metaClass.constructor = { Map m -> // your code here } to check for missing properties
For discussion on this topic check out:
http://www.nabble.com/Grails-doesn%27t-check-%27named-parameter%27-when-constructing-a-Domain-Object-td18256758.html#a18258465
It seems to me there should be a difference between constructor-with-one-unnamed-map-param versus constructor-with-several-named-params, e.g.
new Tester(params) – works as it does now (no warnings)
new Tester(param1:value1, param2:value2, param3:value3) – works as desired by this JIRA, warning for missing properties
In addition, I think the latter syntax should invoke individual setter methods on each param. See http://www.nabble.com/constructor-setter-override-td18852761.html