package com.inspect.base.swagger.config;

import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;

@Component
public class SwaggerBeanPostProcessor implements BeanPostProcessor {
   public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      if(bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
         this.customizeSpringfoxHandlerMappings(this.getHandlerMappings(bean));
      }

      return bean;
   }

   private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
      List<T> copy = (List)mappings.stream().filter((mapping) -> {
         return mapping.getPatternParser() == null;
      }).collect(Collectors.toList());
      mappings.clear();
      mappings.addAll(copy);
   }

   private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
      try {
         Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
         field.setAccessible(true);
         return (List)field.get(bean);
      } catch (IllegalAccessException | IllegalArgumentException var3) {
         throw new IllegalStateException(var3);
      }
   }
}
