apache / apache/grails-core

Populations of abstract classes are not listed in index

Open
#14,277 0 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 an inheritance hierarchy with an abstract superclass, e.g.

```{groovy}
package mypackage

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

```{groovy}
package mypackage

class B extends A {
String b
// ...
// properties
// ...
}
```

```{groovy}
package mypackage

class C extends A {
String c
// ...
// properties
// ...
}
```

And you populate the concrete classes in `Bootstrap.groovy`, e.g.

```{groovy}
package mypackage

class BootStrap {

def init = { servletContext ->
A.withTransaction { status ->
def listOfB = []
(1..25).each { i ->
listOfB << new B(name:"b${i}",b:"${i}st")
}

B.saveAll( listOfB )

def listOfC = []
(1..25).each { i ->
listOfC << new C(name:"c${i}",c:"${i}nd")
}

C.saveAll( listOfC )
}
}
def destroy = {
}
}
```

If you use scaffolding, the _index_ page for `A` shows an empty list instead of a paginated list of 50 objects (25 B objects and other 25 C objects).

Apart from that, it shows a _new_ button that generates a `java.lang.InstantiationException` when clicked because it tries to instantiate an abstract class.

A quick and dirty workaround for this problem is the following:

1. Generate views for the abstract class (only `index.gsp` is relevant)
2. Edit the generated `index.gsp` in the following way:

* Comment the _new_ button code
```{html}

```

* Comment the `` element and add the following code
```{html}


```

With respect to `` attributes:
* The `aList` variable originally passed to `` is `null`.
* Do not include `domainClass="mypackage.A"`, it generates a `java.lang.InstantiationException`.
* The `properties` attribute must be present and contain the list of properties of the abstract class **only**. If it is not present, and objects of classes `A` and `B` appear in the same pagination, a `org.springframework.beans.NotReadablePropertyException` is thrown because `` tries to read properties of class `B` on objects of class `C` and viceversa.

Apart from that, the `create` and `edit` pages are still available for `A`. I have `generated-all` for `A` and tried to delete those methods from the generated controller, but my knowledge of Grails is limited and I am having lots of errors with that, so I went back to scaffolding (which is my main reason for using Grails).

I think that Grails should not show empty lists for abstract classes in scaffolding, but I do not know if that behaviour is inside the scaffolding controller, the `` tag, or harcoded in `_table.gsp`.

I also think that Grails should not generate `create` and `update` methods in the scaffolding controllers for abstract classes.

Contributor guide

Open the contributing guide

Research direction

Reproduce the scaffolding case with abstract A and concrete B and C, then trace the generated controller, index.gsp, f:table, and _table.gsp to find where abstract classes are handled. Check the existing create, edit, and update actions as well as index data generation; done means the index lists inherited concrete instances without instantiation or property errors, and abstract-class creation actions are not generated.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.