Skip to content

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

@amador-duran-toro

Description

@amador-duran-toro

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

package mypackage

abstract class A {
  String name
  // ...
  // properties
  // ...
}

And you populate it in Bootstrap.groovy, e.g.

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions