apache / apache/grails-core

Services are not injected in generated controllers if the length of the class name is one

Open
#14,276 2 comments 0 reactions 0 assignees View on GitHub
relates-to: scaffolding
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

In Grails 4.0.3 and Grails 3.3.11, if you have a domain class named `A`, e.g.

```{groovy}
package mypackage

abstract class A {
String name
// ...
// properties
// ...
}
```
And you populate it in `Bootstrap.groovy`, e.g.

```{groovy}
package mypackage

class BootStrap {

def init = { servletContext ->
A.withTransaction { status ->
A.saveAll(
new A( name: "a1" ),
new A( name: "a2" ),
new A( name: "a3" )
)
}
}
def destroy = {
}
}
```

If you use scaffolding (`create-scaffold-controller A`), everything works correctly.

But if you delete the scaffolding controller and `generate-all` for your class `A`, everything fails throwing a `java.lang.NullPointerException` because the `AService aService` object is `null` in `AController.groovy`, i.e. the service object is not properly injected in the controller.

Now, if you delete all the generated files except the domain class file (`A.groovy`), rename the file and the class as, let's say, `A1`, and `generate-all` again, everything works!

You can try with any one-letter named classes, e.g. `A`, `B`, `X`, etc., and the generated code always fails for the same reason. Add another character to the class name, regenerate, and everything works!

I suppose there must be some name clash somewhere when a class is named with a single character.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the one-letter domain class A, Bootstrap.groovy, and generate-all, then compare AController.groovy and AService injection with the working A1 case. The fix is complete when generated controllers for one-letter classes receive their service and no longer fail with a NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.