ruthst00 commented on code in PR #16653:
URL: https://github.com/apache/grails-core/pull/16653#discussion_r4297262853
##########
grails-datamapping-core/src/test/groovy/org/grails/compiler/gorm/GormEntityTransformSpec.groovy:
##########
@@ -36,6 +36,67 @@ import
org.grails.datastore.mapping.dirty.checking.DirtyCheckable
*/
class GormEntityTransformSpec extends Specification{
+ void 'generated fields do not leak into a shared non-domain superclass'() {
+ given:
+ def loader = new GroovyClassLoader()
+ loader.parseClass("""
+ import grails.gorm.annotation.Entity
+
+ $declarations
+
+ abstract class SharedDetails {
+ String name
+ }
+
+ class PlainCommand extends SharedDetails { }
+ """)
+
+ when:
+ def root = loader.loadClass('RootEntity')
+ def child = loader.loadClass('ChildEntity')
+ def command =
loader.loadClass('PlainCommand').getDeclaredConstructor().newInstance()
+
+ then:
+ root.getDeclaredField('id').type == Long
+ root.getDeclaredField('version').type == Long
+ child.getMethod('getId').declaringClass == root
+ child.getMethod('getVersion').declaringClass == root
+ !child.declaredFields*.name.contains('id')
+ !child.declaredFields*.name.contains('version')
+ command.hasProperty('id') == null
+ command.hasProperty('version') == null
+
+ cleanup:
+ loader.close()
+
+ where:
+ declarations << [
Review Comment:
Fixed in
https://github.com/apache/grails-core/pull/16653/changes/c06f0af83bf36bd6bf2d3835d281b95e89d31856
##########
grails-doc/src/en/guide/theWebLayer/controllers/commandObjects.adoc:
##########
@@ -59,6 +59,39 @@ class AuthorSearchCommand implements
grails.validation.Validateable {
In this example, both `name` and `age` will allow null values during
validation.
+==== Sharing Properties With Domain Classes
+
+A command object and a domain class can extend the same non-domain superclass
to share properties.
+Keep that superclass in `src/main/groovy`, outside `grails-app/domain`, and do
not annotate it as an entity.
+
+[source,groovy]
+----
+abstract class PersonDetails {
+ String name
+}
+
[email protected]
+class Person extends PersonDetails {
+ static constraints = {
+ name blank: false
+ }
+}
+
+class PersonCommand extends PersonDetails implements
grails.validation.Validateable {
+ static constraints = {
+ name blank: false
+ }
+}
+----
+
+Grails adds the generated `id` and `version` properties to the domain class,
not to the shared non-domain superclass.
+Consequently, `PersonCommand` validates `name` without acquiring persistence
properties from `Person`.
+When domain classes extend other domain classes, generated persistence
properties remain on the domain superclass.
+Properties explicitly declared on a superclass are still inherited normally.
+
+After upgrading an application affected by persistence properties being added
to a shared non-domain superclass, run a clean build to regenerate the affected
classes.
Review Comment:
Fixed in
https://github.com/apache/grails-core/pull/16653/changes/c06f0af83bf36bd6bf2d3835d281b95e89d31856
--
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]