Class OpenApiValidationFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
com.atlassian.oai.validator.springmvc.OpenApiValidationFilter
All Implemented Interfaces:
jakarta.servlet.Filter, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.EnvironmentAware, org.springframework.core.env.EnvironmentCapable, org.springframework.web.context.ServletContextAware

public class OpenApiValidationFilter extends org.springframework.web.filter.OncePerRequestFilter
A filter for wrapping the HttpServletRequest and HttpServletResponse.

Wrapping is necessary for the validation.
The Swagger Request Validator needs the pure request body for its validation. Additionally the Spring RestController / Controller needs the pure request body to unmarshal the JSON / form data.

But a ServletInputStream can only be read once and needs to be rewind after successful validation against the OpenAPI / Swagger definition. So the controller can then access it again.

The same applies to response validation and writing to the ServletOutputStream.

  • Field Summary

    Fields inherited from class org.springframework.web.filter.OncePerRequestFilter

    ALREADY_FILTERED_SUFFIX

    Fields inherited from class org.springframework.web.filter.GenericFilterBean

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a OpenApiValidationFilter which validates incoming requests.
    OpenApiValidationFilter(boolean validateRequests, boolean validateResponses)
    Creates a OpenApiValidationFilter which validates incoming requests and / or responses.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    doFilterInternal(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, jakarta.servlet.FilterChain filterChain)
     
    protected boolean
     

    Methods inherited from class org.springframework.web.filter.OncePerRequestFilter

    doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterErrorDispatch

    Methods inherited from class org.springframework.web.filter.GenericFilterBean

    addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • OpenApiValidationFilter

      public OpenApiValidationFilter()
      Creates a OpenApiValidationFilter which validates incoming requests.
    • OpenApiValidationFilter

      public OpenApiValidationFilter(boolean validateRequests, boolean validateResponses)
      Creates a OpenApiValidationFilter which validates incoming requests and / or responses.
      Parameters:
      validateRequests - will enable request validation if true
      validateResponses - will enable response validation if true
  • Method Details

    • doFilterInternal

      protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest servletRequest, jakarta.servlet.http.HttpServletResponse servletResponse, jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException
      Specified by:
      doFilterInternal in class org.springframework.web.filter.OncePerRequestFilter
      Throws:
      jakarta.servlet.ServletException
      IOException
    • shouldNotFilterAsyncDispatch

      protected boolean shouldNotFilterAsyncDispatch()
      Overrides:
      shouldNotFilterAsyncDispatch in class org.springframework.web.filter.OncePerRequestFilter