apache / apache/grails-core

Column sort for index crashes on fields containing multiple values

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

Description

I'm having a hard time describing this, but basically in the index view on any table in my project, if I click on the column heading to sort fields, things work fine, except when I click on a column whose elements are many-to-many; that generates a crash.

I'm at the very earliest stages in the application, just prototyping the data model. The views, controllers and services are the stock ones generated by "grails generate-all". The image attached shows the index page; clicking on either of the two rightmost columns causes the code to crash; all other columns sort just fine.

![Screenshot from 2020-09-14 16-04-44](https://user-images.githubusercontent.com/7864478/93146638-31df2b00-f6a4-11ea-8bbf-75fa689f4bd6.png)

The crash looks like this:

```
ArrayIndexOutOfBoundsException occurred when processing request: [GET] /ocupante/index - parameters:
sort: actividades
max: 10
order: asc
0. Stacktrace follows:

java.lang.reflect.InvocationTargetException: null
at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at org.hibernate.criterion.Order.toSqlString(Order.java:119)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getOrderBy(CriteriaQueryTranslator.java:414)
at org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:106)
at org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:75)
at org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:80)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1773)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
at org.grails.orm.hibernate.query.AbstractHibernateQuery.listForCriteria(AbstractHibernateQuery.java:719)
at org.grails.orm.hibernate.query.AbstractHibernateQuery.list(AbstractHibernateQuery.java:709)
at grails.gorm.PagedResultList.(PagedResultList.java:43)
at grails.gorm.DetachedCriteria$_list_closure2.doCall(DetachedCriteria.groovy:137)
at grails.gorm.DetachedCriteria$_withPopulatedQuery_closure8.doCall(DetachedCriteria.groovy:769)
at org.grails.datastore.gorm.GormStaticApi$_withDatastoreSession_closure24.doCall(GormStaticApi.groovy:862)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:319)
at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:40)
at org.grails.datastore.gorm.GormStaticApi.withDatastoreSession(GormStaticApi.groovy:861)
at grails.gorm.DetachedCriteria.withPopulatedQuery(DetachedCriteria.groovy:740)
at grails.gorm.DetachedCriteria.list(DetachedCriteria.groovy:135)
at grails.gorm.transactions.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:94)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.gorm.transactions.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:91)
at sgs.OcupanteController.index(OcupanteController.groovy:14)
... 14 common frames omitted
```

The code in the controller that provokes this is the "respond" below at line 14:

```
package sgs

import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*

class OcupanteController {

OcupanteService ocupanteService

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond ocupanteService.list(params), model:[ocupanteCount: ocupanteService.count()]
}

```

I'm at the very earliest stages in the application, just prototyping the data model. The views, controllers and services are the stock ones generated by "grails generate-all". The image attached shows the index page; clicking on either of the two rightmost columns causes the code to crash; all other columns sort just fine.

![Screenshot from 2020-09-14 16-04-44](https://user-images.githubusercontent.com/7864478/93146638-31df2b00-f6a4-11ea-8bbf-75fa689f4bd6.png)

gradle.properties:
grailsVersion=3.3.9
gormVersion=6.1.11.RELEASE
gradleWrapperVersion=3.5

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the failure from the generated OcupanteController.groovy index action at line 14, using the many-to-many column sort described in the request. Trace ocupanteService.list(params) through the Hibernate Order and DetachedCriteria stack frames. Done means sorting those columns no longer raises ArrayIndexOutOfBoundsException while the other index sorting still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.