Repository navigation
Reject a sort by an alias that the query does not define - #17074
jdaugherty wants to merge 1 commit into
Conversation
|
This one is for 9.0.x, not 7.0.x: it makes a call that runs today throw, which the rule from the dev@ thread sends to the next major. Its base is 7.0.x only because it is stacked on #17073. Please review it, but do not merge it here; I'll rebase it onto 9.0.x and change the base once the parts below it have been merged up. |
With GORM for Hibernate 5, a sort whose path starts with a name that is neither a property of the queried class nor an alias of the query was ignored: the query returned its rows in the same order for asc and desc, without an error. Any other invalid sort property throws IllegalArgumentException: Invalid sort property, and such a sort now does the same, in where queries, detached criteria, dynamic finders, list, the example queries and the sort argument of a criteria query. A sort that starts with an alias that the query defines, or with the alias of the queried class, is left as it is. Part 14 of the split of apache#16853. It makes a call that runs today throw, so it goes to the next major and not to 7.0.x.
cc8c7a0 to
9d65dad
Compare
|
Done: this pull request now targets 9.0.x and is one commit on 9.0.x (9d65dad). It no longer depends on the other parts of the split, so it can be reviewed and merged on its own. The earlier 7.0.x based version is kept at |
🚨 All tests passed but jobs failed 🚨Failed Jobs without Test Failures❌ Groovy Snapshot Canary Build / Build Grails (shard 0) 🏷️ Commit: 9d65dad Rerun ControlsClick the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A sort by an alias the query defined is refused.
| PersistentProperty property = entity.getPropertyByName(sortHead); | ||
| if (property == null && !sortHead.equals(c.getAlias())) { | ||
| // not a property of the entity, so a sort by it, such as by an alias that the query does not define, can only be a mistake | ||
| throw new IllegalArgumentException("Invalid sort property"); |
There was a problem hiding this comment.
The query aliases club as c and sorts by c.name. That sort should run. It is refused, because this check only knows the root alias. Accept an alias the query defined.
There was a problem hiding this comment.
Could you provide the public GORM call and fixture for this? I reran UndefinedAliasSortSpec on this PR: all 12 cases pass, including a where query with a declared association alias, a criteria query with createAlias("club", "c") and sort: "c.name", and a detached criteria with a root alias.
The code cited here handles static list and the example/findWhere paths, which create fresh criteria and process the sort before publishing PreQueryEvent. The criteria-builder path already checks the aliases declared by the criteria. I would like to establish an end-user regression before changing this helper. An older alias-sorting omission belongs in the relevant sort PR or a follow-up, rather than expanding this PR beyond rejecting undefined aliases.
matrei
left a comment
There was a problem hiding this comment.
Reviewed at 9d65dad (base 9.0.x). I probed sorts by an alias that the query doesn't define, by one that it defines, by the alias of the queried class, by an association and by properties that don't exist, in list, where queries, detached criteria, dynamic finders, findAll with an example, the sort argument and order of a criteria query, a map of sorts, a paged list, count and get:
- with Hibernate 5 at this head and at its parent on 9.0.x (e51343f);
- with Hibernate 7 on 9.0.x;
- with Hibernate 5 on the 7.0.x branch kept for the merge-up (cc8c7a0), at the top of the 7.0.x stack (#17073, 16bd465) and on 7.0.x (aee4c04).
The change does what the description says:
| Hibernate 5 | 9.0.x | This head |
|---|---|---|
Team.list(sort: 'x.name'), also a where query, a detached criteria, sort('x.name'), a dynamic finder, findAll with an example, the sort argument of a criteria query, a map, order: 'desc' and max: 2 |
the rows unsorted | IllegalArgumentException: Invalid sort property |
Team.where { … }.sort('x.name').count(), also get() |
7, and the team | Invalid sort property |
Team.where { def c = club; … }.list(sort: 'c.name'), createCriteria().list(sort: 'c.name') { createAlias('club', 'c') }, new DetachedCriteria(Team, 't').list(sort: 't.name') and Team.list(sort: 'club.name') |
sorted | the same |
The other invalid sort properties, sort: 'unknown' and sort: 'club.unknown', already throw Invalid sort property with Hibernate 5 on 9.0.x, so this makes the message the same for all of them. The tests of grails-data-hibernate5-core pass at this head (1294 tests, 29 skipped). It's a call that runs today and now throws, so I agree that it belongs in 9.0.x, with the note.
Needs a fix
1. Resolve the conflicts with 9.0.x. upgrading90x.adoc and the skill conflict with the notes merged into 9.0.x since. The note is "26." here, and 9.0.x has notes up to "31." now, so it becomes "32." when it's rebased.
2. Hibernate 7 on 9.0.x fails for the same sorts, but not with this exception:
| Hibernate 7 on 9.0.x | |
|---|---|
Team.list(sort: 'x.name'), also a map |
Invalid sort property |
a where query, a detached criteria, a dynamic finder, the sort argument of a criteria query, order: 'desc', max: 2 |
NullPointerException: Cannot invoke "jakarta.persistence.criteria.Expression.getJavaType()" |
sort('x.name').list(), sort('x.name').get(), order('x.name') in a criteria query |
AssertionError |
Team.where { … }.sort('x.name').count() |
7, the sort ignored |
Team.findAll(new Team(), [sort: 'x.name']) |
null |
With this change, Hibernate 5 rejects such a sort cleanly on 9.0.x, while Hibernate 7 fails in five different ways or not at all. The note says "With GORM for Hibernate 5". Hibernate 7 should throw the same exception, in this pull request or one next to it, and the note then covers both. If that's left for later, an issue for it, linked from the note, would keep it in view. A findAll with an example that returns null looks like a bug of its own.
3. The branch for the merge-up is built on an older part 13. fix/16853-14-reject-undefined-sort-alias-7.0.x (cc8c7a0) is on e359719. The 7.0.x stack has moved on since, with 5dc4d34, b3ae39f, 429b6d5, f2e1cc6 and 35242f9 and the merges of today. 35242f9 changes the same lines of AbstractHibernateQuery that this change does, so the resolution on that branch no longer applies as it is. It also returns other rows than the stack top for an alias that is also the name of an association:
new DetachedCriteria(Team, 'club').list(sort: 'club.name') |
Stack top | Branch for the merge-up |
|---|---|---|
| 7 teams, by the name of the team | 5, by the name of the club |
That's point 2 of my review of #17073. The branch should be rebuilt on the stack once #17073 is settled.
Notes
-
On James's comment: the static
list,findWhereand the example queries can't define an alias, and a sort by an alias that a where query or a criteria query defines works here (the last row of the first table). I couldn't reproduce a refused sort by a defined alias either, so I agree with your reply. -
"Results that change" and the note could name
countandget: they reject the sort too, although a count doesn't sort. A count from a sortable table with optional filters, built from the same sort parameter, then throws as well. -
At the top of the 7.0.x stack,
new DetachedCriteria(Team).list(sort: 'club.unknown')fails withcould not resolve property: club_alias0 of: Team, and the same holds forPlayerwithsort: 'team.x.name'. On 7.0.x, the error namesunknown of: Club. With this change on 9.0.x, it'sInvalid sort property. The resolution of the merge-up should keep the 9.0.x message for it.
Description
Part 14 of the split of #16853, as agreed there and in the dev@ thread Fixes that change results on patch and minor branches. Relates to #15589.
This part makes a call that runs today throw, so under the rule from the thread it goes to the next major, 9.0.x, and not to 7.0.x. It is one commit on 9.0.x, 9d65dad, and no longer depends on the other parts of the split.
With GORM for Hibernate 5, a sort whose path starts with a name that is neither a property of the queried class nor an alias of the query was ignored: the query returned its rows in the same order for
ascanddesc, without an error. Any other invalid sort property throwsIllegalArgumentException: Invalid sort property, and such a sort now does the same, in where queries, detached criteria, dynamic finders,list, the example queries and thesortargument of a criteria query.A sort that starts with an alias that the query defines, or with the alias of the queried class, is left as it is.
Results that change
IllegalArgumentException: Invalid sort property. The query ignored the sort and returned its results unsorted.The upgrade guide has a note in "Upgrading from Grails 8 to Grails 9", and the upgrade skill has the same entry.
Merge-up
Parts 1 to 13 change the same methods on 7.0.x. When they are merged up into 9.0.x they will conflict with this change in
AbstractHibernateQuery,GrailsHibernateQueryUtilsandGrailsHibernateUtil. The resolution is the 7.0.x code plus the rejection, which is kept for reference atfix/16853-14-reject-undefined-sort-alias-7.0.xon my fork.Verification
The new
UndefinedAliasSortSpecfails for all nine queries on 9.0.x without the change and passes with it. The sort, order, criteria and where query specs ofgrails-data-hibernate5-corepass, andcodeStylepasses. The full build was not run locally.Contributor Checklist
Please review the following checklist before submitting your pull request. Pull requests that do not meet these requirements may be closed without review.
Issue and Scope
7.0.x): Bug fixes only. No new features or API changes.7.1.x): New features are welcome, but breaking existing APIs must be avoided.8.0.x): Reserved for major changes. Breaking API changes are permitted.Code Quality
./gradlew build --rerun-tasks../gradlew codeStyleand resolved any violations. See Code Style for details.Licensing and Attribution
Documentation