jdaugherty commented on code in PR #16003:
URL: https://github.com/apache/grails-core/pull/16003#discussion_r3610597474


##########
grails-test-suite-web/src/test/groovy/org/grails/web/binding/DefaultDatabindingWhitelistBehaviorSpec.groovy:
##########
@@ -0,0 +1,117 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.binding
+
+import grails.artefact.Artefact
+import grails.persistence.Entity
+import grails.testing.web.controllers.ControllerUnitTest
+import grails.validation.Validateable
+import grails.web.databinding.DataBindingUtils
+import spock.lang.Specification
+
+class DefaultDatabindingWhitelistBehaviorSpec extends Specification implements 
ControllerUnitTest<WhitelistBehaviorController> {
+
+    void 'domain binding includes simple and association properties but 
excludes special and unlisted properties'() {
+        given:
+        Date dateCreated = new Date()
+        Date lastUpdated = new Date()
+        Map source = [
+                name: 'Ada',
+                address: [street: 'Analytical Engine Way'],
+                id: 99L,
+                version: 7L,
+                dateCreated: dateCreated,
+                lastUpdated: lastUpdated,
+                ignored: 'not bindable'
+        ]
+
+        when:
+        WhitelistDomain domain = new WhitelistDomain()
+        DataBindingUtils.bindObjectToInstance(domain, source)
+
+        then:
+        domain.name == 'Ada'
+        domain.address.street == 'Analytical Engine Way'
+        domain.id == null
+        domain.version == null
+        domain.dateCreated == null
+        domain.lastUpdated == null
+        domain.ignored == null
+    }
+
+    void 'Validateable command binding includes declared special properties 
but excludes unlisted properties'() {
+        given:
+        Date dateCreated = new Date()
+        Date lastUpdated = new Date()
+        params.name = 'Grace'
+        params.'address.street' = 'Compiler Lane'
+        params.id = '99'
+        params.version = '7'
+        params.dateCreated = dateCreated
+        params.lastUpdated = lastUpdated
+        params.ignored = 'not bindable'
+
+        when:
+        WhitelistCommand command = controller.bindCommand().command
+
+        then:
+        command.name == 'Grace'
+        command.address.street == 'Compiler Lane'
+        command.id == 99L
+        command.version == 7L
+        command.dateCreated == dateCreated
+        command.lastUpdated == lastUpdated
+        command.ignored == null
+    }
+}
+
+@Entity
+class WhitelistDomain {
+    String name
+    WhitelistAddress address = new WhitelistAddress()
+    Long id
+    Long version
+    Date dateCreated
+    Date lastUpdated
+    Object ignored

Review Comment:
   "Unlisted" / "non-allowlisted" slightly mischaracterizes why this property 
doesn't bind: `shouldFieldBeInWhiteList` (DefaultASTDatabindingHelper.java:277) 
excludes it specifically because its static type is `Object` without generics. 
There is no general "unlisted" category — every eligible typed, non-transient, 
non-special property is auto-whitelisted, so e.g. an extra `String` property 
here *would* bind. Suggest naming this `Object untypedProperty` (or a `def` 
field) and wording the test name as "excludes special and Object/def-typed 
properties" so the pinned rule reads as what it is.



##########
grails-test-suite-web/src/test/groovy/org/grails/web/binding/DefaultDatabindingWhitelistBehaviorSpec.groovy:
##########
@@ -0,0 +1,117 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.binding
+
+import grails.artefact.Artefact
+import grails.persistence.Entity
+import grails.testing.web.controllers.ControllerUnitTest
+import grails.validation.Validateable
+import grails.web.databinding.DataBindingUtils
+import spock.lang.Specification
+
+class DefaultDatabindingWhitelistBehaviorSpec extends Specification implements 
ControllerUnitTest<WhitelistBehaviorController> {
+
+    void 'domain binding includes simple and association properties but 
excludes special and unlisted properties'() {

Review Comment:
   Partial overlap: 
`DefaultASTDatabindingHelperDomainClassSpecialPropertiesSpec` in this same 
package already pins the domain special-property exclusions (id/version via 
#15681, dateCreated/lastUpdated via GRAILS-11173), also through observable 
binding behavior. The genuinely new coverage here is the Object-typed 
exclusion, nested association binding, and the Validateable command *including* 
id/version/dateCreated/lastUpdated. Worth a cross-reference comment, or 
trimming this first test to the non-duplicated assertions, so the two specs 
don't drift apart when the contract changes.



##########
grails-test-suite-web/src/test/groovy/org/grails/web/binding/DefaultDatabindingWhitelistBehaviorSpec.groovy:
##########
@@ -0,0 +1,117 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.grails.web.binding
+
+import grails.artefact.Artefact
+import grails.persistence.Entity
+import grails.testing.web.controllers.ControllerUnitTest
+import grails.validation.Validateable
+import grails.web.databinding.DataBindingUtils
+import spock.lang.Specification
+
+class DefaultDatabindingWhitelistBehaviorSpec extends Specification implements 
ControllerUnitTest<WhitelistBehaviorController> {
+
+    void 'domain binding includes simple and association properties but 
excludes special and unlisted properties'() {
+        given:
+        Date dateCreated = new Date()
+        Date lastUpdated = new Date()
+        Map source = [
+                name: 'Ada',
+                address: [street: 'Analytical Engine Way'],
+                id: 99L,
+                version: 7L,
+                dateCreated: dateCreated,
+                lastUpdated: lastUpdated,
+                ignored: 'not bindable'
+        ]
+
+        when:
+        WhitelistDomain domain = new WhitelistDomain()
+        DataBindingUtils.bindObjectToInstance(domain, source)
+
+        then:
+        domain.name == 'Ada'
+        domain.address.street == 'Analytical Engine Way'
+        domain.id == null
+        domain.version == null
+        domain.dateCreated == null
+        domain.lastUpdated == null
+        domain.ignored == null
+    }
+
+    void 'Validateable command binding includes declared special properties 
but excludes unlisted properties'() {
+        given:
+        Date dateCreated = new Date()
+        Date lastUpdated = new Date()
+        params.name = 'Grace'
+        params.'address.street' = 'Compiler Lane'
+        params.id = '99'
+        params.version = '7'
+        params.dateCreated = dateCreated
+        params.lastUpdated = lastUpdated
+        params.ignored = 'not bindable'
+
+        when:
+        WhitelistCommand command = controller.bindCommand().command
+
+        then:
+        command.name == 'Grace'
+        command.address.street == 'Compiler Lane'
+        command.id == 99L
+        command.version == 7L
+        command.dateCreated == dateCreated
+        command.lastUpdated == lastUpdated
+        command.ignored == null
+    }
+}
+
+@Entity
+class WhitelistDomain {
+    String name
+    WhitelistAddress address = new WhitelistAddress()
+    Long id
+    Long version
+    Date dateCreated
+    Date lastUpdated
+    Object ignored
+
+    static hasOne = [address: WhitelistAddress]

Review Comment:
   `hasOne` declares a bidirectional association whose foreign key lives on the 
child, and `WhitelistAddress` has no back-reference — GORM would reject this 
mapping if the domain were ever registered with a mapping context (it isn't 
here, since the spec uses ControllerUnitTest without DataTest, which is why 
nothing complains). The typed `address` property alone is what puts 
`address`/`address.*` in the generated whitelist, so `hasOne` adds nothing to 
the contract under test. Suggest dropping it (and possibly the field 
initializer tells the nested-binding story on its own).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to