davydotcom opened a new pull request, #15482:
URL: https://github.com/apache/grails-core/pull/15482

   ## Problem
   
   Calling `new DomainClass(null)` on a GORM domain class throws:
   
   ```
   groovy.lang.GroovyRuntimeException: Could not find matching constructor for: 
DomainClass(null)
   ```
   
   This is a regression from Grails 6 / Groovy 3, where `new DomainClass(null)` 
was treated equivalently to `new DomainClass()`.
   
   ## Root Cause
   
   In Groovy 3, the runtime resolved `new SomeClass(null)` by matching it to 
the implicit map-based constructor that Groovy provides for POGOs. When `null` 
was passed, the runtime simply created the object via the no-arg constructor 
without setting any properties.
   
   Groovy 4 changed how constructor resolution works at runtime. The implicit 
map constructor is no longer matched when `null` is passed as the argument, 
causing the `GroovyRuntimeException`.
   
   This is a common pattern in Grails applications — for example, when a 
variable that may be `null` is passed to a domain constructor:
   
   ```groovy
   def props = maybeGetProperties() // could return null
   def obj = new MyDomain(props)    // fails in Grails 7 / Groovy 4
   ```
   
   ## Fix
   
   The `GormEntityTransformation` (the `@Entity` AST transform) now injects an 
explicit `Map` constructor into every GORM entity class. The constructor:
   
   - Accepts a `Map` parameter, allowing `null` to match at runtime
   - If the map is not `null`, sets properties via 
`InvokerHelper.setProperties(this, args)` — the same mechanism Groovy uses 
internally for map constructors
   - If `null`, does nothing (equivalent to the no-arg constructor)
   - Also ensures a no-arg constructor is preserved, since adding any explicit 
constructor prevents Groovy from auto-generating the default one
   
   The constructor is injected early in the transformation (before trait 
composition), so trait `\$init\$` methods are properly wired into it by the 
Groovy trait composer.
   
   ## Tests
   
   Added `NullConstructorArgSpec` in `grails-data-hibernate5/core` with three 
tests:
   1. No-arg constructor works (baseline)
   2. Null constructor argument works the same as no-arg
   3. Domain created with null constructor argument can be saved
   
   All 598 existing hibernate5 core tests continue to pass with 0 failures.
   
   Co-Authored-By: Oz <[email protected]>


-- 
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