- marked as enhancement
Support for ServletRequest version 2
I'm using ServletRequest version 2.5 and I'm unable to run Swagger validation filter. As "SwaggerValidationFilter" is using function
servletRequest.getContentLengthLong()
which is available in Servlet version 3.0 and above.
So can you please guide me any work around or over haul this issue so that Swagger works fine with Servlet version 2.0.
Thanks.
Comments (6)
-
reporter -
OMG, do you reference this file? https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-springmvc/src/main/java/com/atlassian/oai/validator/springmvc/SwaggerValidationFilter.java
That's the code I wrote. Is there really someone out there using the Spring MVC adapter. I'm so hyped right now.
-
The
SwaggerValidationFilter
simply wraps incoming requests into theResettableRequestServletWrapper
. This filter does nothing more.You can try copying that filter and use
getContentLength()
instead ofgetContentLengthLong()
on your version of the filter. On the Spring configuration simply register your filter.Let me hear from you if it works.
Edit: But be aware. If you send requests having more than ~2.1GB of content the Spring MVC adapter will throw an exception.
-
Could you please try this filter?
import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.web.cors.CorsUtils; import org.springframework.web.filter.OncePerRequestFilter; public class SwaggerValidationCustomFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final FilterChain filterChain) throws ServletException, IOException { final HttpServletRequest requestToUse = wrapValidatableServletRequest(servletRequest); filterChain.doFilter(requestToUse, servletResponse); } private HttpServletRequest wrapValidatableServletRequest(final HttpServletRequest servletRequest) { // wrap only validatable requests final long contentLengthLong = getContentLength(servletRequest); final boolean doValidationStep = contentLengthLong <= Integer.MAX_VALUE && !CorsUtils.isPreFlightRequest(servletRequest); return doValidationStep ? new ResettableRequestServletWrapper(servletRequest) : servletRequest; } private long getContentLength(final HttpServletRequest servletRequest) { final String contentLength = servletRequest.getHeader("content-length"); if (StringUtils.isNotBlank(contentLength)) { try { return Long.parseLong(contentLength); } catch (final NumberFormatException e) { // either no content-length was set or the content-length exceeded Long.MAX_VALUE } } return -1L; } }
If this works, I'll add it to the source code. This should work for ServletRequest v2 and v3.
-
reporter lols :D I have been using this file for around more than 6 months and its working perfectly fine
I have used the recommended approach and it worked for me .
Thanks
-
- changed status to resolved
Available in v1.4.5
- Log in to comment