Force @GrailsCompileStatic check request as AbstractMultipartHttpServletRequest

134 views Asked by At

I'm trying to apply @GrailsCompileStatic to controller that has an action that retrieves MultipartFiles from request:

request.getFile('foo')

But get the following:

[Static type checking] - Cannot find matching method javax.servlet.http.HttpServletRequest#getFile(java.lang.String)

Is there any chance to force compiler to verify request against AbstractMultipartHttpServletRequest (that has the getFile(java.lang.String) method) instead of HttpServletRequest?

UPD This solution works:

MultipartFile multipartFile = ((StandardMultipartHttpServletRequest) request).getFile('myFile')

But has some strange behaviour when trying to test it:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.grails.plugins.testing.GrailsMockHttpServletRequest@2bcf856f' with class 'org.grails.plugins.testing.GrailsMockHttpServletRequest' to class 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest'

1

There are 1 answers

0
daggett On BEST ANSWER

http://docs.grails.org/2.2.1/api/org/codehaus/groovy/grails/plugins/testing/GrailsMockHttpServletRequest.html

and

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.html

both implements an interface

org.springframework.web.multipart.MultipartHttpServletRequest

so just use this

import org.springframework.web.multipart.MultipartHttpServletRequest
...

MultipartFile multipartFile = ((MultipartHttpServletRequest) request).getFile('myFile')