Class OpenapiValidationGatewayFilterFactory

java.lang.Object
org.springframework.cloud.gateway.support.AbstractConfigurable<OpenapiValidationGatewayFilterFactory.Config>
org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory<OpenapiValidationGatewayFilterFactory.Config>
ch.nexsol.gateway.validation.factory.OpenapiValidationGatewayFilterFactory
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory<OpenapiValidationGatewayFilterFactory.Config>, org.springframework.cloud.gateway.support.Configurable<OpenapiValidationGatewayFilterFactory.Config>, org.springframework.cloud.gateway.support.ShortcutConfigurable, org.springframework.context.ApplicationEventPublisherAware

public class OpenapiValidationGatewayFilterFactory extends org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory<OpenapiValidationGatewayFilterFactory.Config>
Gateway filter factory validating the requests and the responses of a route against an OpenAPI contract. Reference it in a route with the OpenapiValidation filter name, giving it the location of the contract.

What happens to a message that breaks its contract is configured globally, per direction, through OpenapiValidationProperties: a request is rejected with 400 Bad Request and a response replaced by 502 Bad Gateway in ENFORCE mode, while REPORT mode forwards both and only records the violations. Either way the outcome is stamped on the exchange for the auditing plugin and counted in ValidationMetrics.

A body is only ever buffered when there is something to gain from it: it has to be JSON, uncompressed, and announce a length within the configured maximum. That is what keeps a file upload streaming straight through — a multipart/form-data or application/octet-stream payload carries nothing a JSON schema applies to, so it is never read into memory, however large it is. A chunked body announcing no length is left alone for the same reason: buffering it could not be bounded. Each of these is counted, so what is not being validated stays visible.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 

    Nested classes/interfaces inherited from class org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory

    org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory.NameConfig

    Nested classes/interfaces inherited from interface org.springframework.cloud.gateway.support.ShortcutConfigurable

    org.springframework.cloud.gateway.support.ShortcutConfigurable.GatewayBeanFactoryResolver, org.springframework.cloud.gateway.support.ShortcutConfigurable.GatewayEvaluationContext, org.springframework.cloud.gateway.support.ShortcutConfigurable.RestrictivePropertyAccessor, org.springframework.cloud.gateway.support.ShortcutConfigurable.ShortcutType
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Configuration key holding the gateway side path prefix to strip.
    static final String
    Configuration key holding the location of the contract.

    Fields inherited from interface org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory

    NAME_KEY, VALUE_KEY
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates the factory.
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.cloud.gateway.filter.GatewayFilter
    Builds the filter validating the exchange against the configured contract.

    Methods inherited from class org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory

    enableBodyCaching, getPublisher, setApplicationEventPublisher

    Methods inherited from class org.springframework.cloud.gateway.support.AbstractConfigurable

    getConfigClass, newConfig, toString

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory

    apply, apply, apply, getConfigClass, name, newConfig

    Methods inherited from interface org.springframework.cloud.gateway.support.ShortcutConfigurable

    shortcutFieldPrefix, shortcutType
  • Field Details

    • SPEC_URL_KEY

      public static final String SPEC_URL_KEY
      Configuration key holding the location of the contract.
      See Also:
    • PATH_PREFIX_KEY

      public static final String PATH_PREFIX_KEY
      Configuration key holding the gateway side path prefix to strip.
      See Also:
  • Constructor Details

    • OpenapiValidationGatewayFilterFactory

      public OpenapiValidationGatewayFilterFactory(OpenapiContractRegistry registry, OpenapiValidationProperties properties, ValidationMetrics metrics)
      Creates the factory.
      Parameters:
      registry - the registry the contracts are read through
      properties - the per direction validation settings
      metrics - the counters the outcomes are published to
  • Method Details

    • shortcutFieldOrder

      public List<String> shortcutFieldOrder()

      Allows the filter to be configured as OpenapiValidation=<specUrl> or OpenapiValidation=<specUrl>,<pathPrefix>.

    • apply

      public org.springframework.cloud.gateway.filter.GatewayFilter apply(OpenapiValidationGatewayFilterFactory.Config config)
      Builds the filter validating the exchange against the configured contract.
      Parameters:
      config - the filter configuration holding the contract location
      Returns:
      the gateway filter