Hello, I've run into a strange problem upgrading f...
# questions
r
Hello, I've run into a strange problem upgrading from Grails 6.1.1 to 6.2.0 - I'm getting a Groovy static type checking error from the
UserRole
class created by the
spring-security-core
plugin, specifically:
Copy code
static UserRole get(long userId, long roleId) {
    criteriaFor(userId, roleId).get()
    ^
    [Static type checking] - cannot return value of type java.lang.Object on method returning type application.UserRole
}
Has anyone seen this before or got any suggestions for how to resolve the issue? It only occurs when I set
grailsVersion=6.2.0
in
gradle.properties
. Thanks, Robert
👍 1
p
Could you please share the definition of
criteriaFor
or try adding
@CompileDynamic
to the method. We have updated to Apache Groovy 3.0.21 which might have been the root cause here but it is difficult say anything from this information only.
r
Thanks @puneetbehl - I wondered if it might have something to do with the version of Groovy that I am using. The
UserRole
class appears to be identical to the version described in the
spring-security-core
plugin manual: https://grails.github.io/grails-spring-security-core/6.0.x/index.html#personAuthorityClass In particular, the
criteriaFor
method looks like this:
Copy code
private static DetachedCriteria criteriaFor(long userId, long roleId) {
    UserRole.where {
        user == User.load(userId) &&
        role == Role.load(roleId)
    }
}
It's not immediately obvious that Grails 6.2.0 depends on Groovy 3.0.21 - in fact, according to the list of External Libraries displayed by IntelliJ IDEA, it still depends on Groovy 3.0.11. How do I persuade Gradle / IDEA to use Groovy 3.0.21? I tried setting
groovyVersion=3.0.21
in my
gradle.properties
, but that didn't seem to help.
Incidentally, adding
@CompileDynamic
to the method cures the problem, but this isn't a very satisfactory solution because the code compiled without any problems under Grails 6.1.1 and was autogenerated by the
spring-security-core
plugin a long time ago...
t
@robertjstroud yes, I ran into this same issue. To get around the static type checking error, I ended up modifying the method so that it explicitly casts the object returned by
criteriaFor
to a
UserRole
.
Copy code
static UserRole get(long userId, long roleId) {
    criteriaFor(userId, roleId).get() as UserRole
}
@puneetbehl would you recommend I report this as a bug with Groovy (and include an example repository that demonstrates the issue) or open up a pull request with
grails-spring-security-core
plugin so that the autogenerated
UserRole
class has my workaround?
r
@tylervz Ah - thanks. Glad to know that I'm not the only person experiencing this problem!
👍 1
My feeling is that this is a Groovy bug rather. than a problem with the Spring Security plugin but I also wondered if it might be a GORM issue. The result of calling the
criteriaFor
method is a
DetachedCriteria
that selects a
UserRole
, so in principle,
criteriaFor(...).get()
should be a
UserRole
object.
Previous versions of Grails appear to have converted the
criteriaFor(...).get()
expression to a
UserRole
object implicitly, otherwise, the code would have generated a static type error.
So it looks as though the implicit type conversion has stopped working for some reason or is no longer recognised by the Groovy compiler
t
@robertjstroud I tried creating a sample Grails 6.2.0 to reproduce the issue and was unable to do so. Then I copy and pasted my
UserRole.groovy
file from my other project where I had the issue you were encountering too and then I noticed one key difference. In my sample application, the
criteriaFor
method has a different signature.
Copy code
private static DetachedCriteria<UserRole> criteriaFor(long userId, long roleId) {
    UserRole.where {
        user == User.load(userId) &&
        role == Role.load(roleId)
    }
}
To reproduce the static compilation error in my sample app, I had to to change the signature to
private static DetachedCriteria criteriaFor(long userId, long roleId)
. I don't know what version of Spring Security Core changed that signature, but I don't think the static compilation error is a Groovy bug.
r
@tylervz Agreed. Treating 'DetachedCriteria' as a parameterised type is a significant change that removes the need for an implicit type conversion. But it's strange that Groovy 3.0.11 didn't complain about the original version. Thanks for investigating further.
👍 1