grails-core: Grails 6.2.2: ClassCastException for inheritance in Commands

Expected Behavior

Grails throws exception ClassCastException when we have 2 endpoints with inherited commands. It worked fine on 6.1.2.

Looks like this is a critical issue.

class UrlMappings {
        "/my/$patientId/procedure"(controller: "my", action: "createLedgerProcedure", method: "POST")
        "/my/$patientId/procedure/insuranceEstimates"(controller: "my", action: "getInsuranceEstimates", method: "POST")
}

class MyBaseCommand implements Validateable {

    int testId
}

class MyCommand extends MyBaseCommand {

    int myId
}

class MyController {
    def getInsuranceEstimates(MyCommand command, long patientId) {
        log.info('test2 method')
        render([response: "test2 method"] as JSON)
    }

    def createLedgerProcedure(MyBaseCommand command, long patientId) {
        log.info('test1 method')
        render([response: "test1 method"] as JSON)
    }

}

Actual Behaviour

Exception:

2024-12-26 18:13:10.639 ERROR --- [nio-8080-exec-2] StackTrace                               : Full Stack Trace:

java.lang.reflect.InvocationTargetException: null
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	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:1072)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
	at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.ClassCastException: class com.example.MyBaseCommand cannot be cast to class com.example.MyCommand (com.example.MyBaseCommand and com.example.MyCommand are in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @8f4db8)
	... 13 common frames omitted

2024-12-26 18:13:10.690 ERROR --- [nio-8080-exec-2] o.g.web.errors.GrailsExceptionResolver   : ClassCastException occurred when processing request: [POST] /my/123/procedure
class com.example.MyBaseCommand cannot be cast to class com.example.MyCommand (com.example.MyBaseCommand and com.example.MyCommand are in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @8f4db8). Stacktrace follows:

java.lang.reflect.InvocationTargetException: null
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	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:1072)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
	at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.ClassCastException: class com.example.MyBaseCommand cannot be cast to class com.example.MyCommand (com.example.MyBaseCommand and com.example.MyCommand are in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @8f4db8)
	... 13 common frames omitted

Steps To Reproduce

Reproduces on Grails 6.2.2. It worked fine on 6.1.2.

grails_6_2_2.zip

An example of the application has been attached:

  • run the attached application: ./gradlew bootRun
  • Send the request:

curl --location --request POST 'http://localhost:8080/my/123/procedure' \
--header 'Content-Type: application/json' \
--data '{
    "testId": 321
}'

Environment Information

  • Windows 10
  • Java:
openjdk version "11.0.22" 2024-01-16
OpenJDK Runtime Environment Temurin-11.0.22+7 (build 11.0.22+7)
OpenJDK 64-Bit Server VM Temurin-11.0.22+7 (build 11.0.22+7, mixed mode)

Example Application

No response

Version

6.2.2

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (12 by maintainers)

Commits related to this issue

Most upvoted comments

We have released 6.2.3 to address this issue.

I’m going to close this ticket as fixed. I’m discussing with the other maintainers on when best to release this. Hopefully it’s soon.

The 6.2.3-SNAPSHOT build is now published. @agre1981 can you please set your grails version to this version and let me know if it fixes this issue for you?

Could you try running the tests with clean?

@agre1981 Thanks, I managed to get the tests to fail now.

@jdaugherty Could you advise the tool for decompiling .class you use?

IntelliJ fails to decompile controller classes image

I use http://java-decompiler.github.io/ to decompile groovy code.

It can make sense to add Validatable to the test to cover my “not failing” case

I changed the code that calls validate in the action transformer, and this now is generated: image

Which makes the test pass.

I figured out the cause of this. I’ll push the tests here shortly. The name of the method & the arguments to that method seem to matter. The actual cast exception is happening on the validate() call:

image