/*
 *
 *  Copyright 2015-2019 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package springfox.documentation.swagger.readers.operation;


import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.spring.web.DescriptionResolver;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static springfox.documentation.swagger.common.SwaggerPluginSupport.*;

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
public class OperationImplicitParametersReader implements OperationBuilderPlugin {
  private final DescriptionResolver descriptions;

  @Autowired
  public OperationImplicitParametersReader(DescriptionResolver descriptions) {
    this.descriptions = descriptions;
  }

  @Override
  public void apply(OperationContext context) {
    context.operationBuilder().parameters(readParameters(context));
  }

  @Override
  public boolean supports(DocumentationType delimiter) {
    return pluginDoesApply(delimiter);
  }

  private List<Parameter> readParameters(OperationContext context) {
    Optional<ApiImplicitParams> annotation = context.findAnnotation(ApiImplicitParams.class);

    List<Parameter> parameters = new ArrayList<>();
    if (annotation.isPresent()) {
      for (ApiImplicitParam param : annotation.get().value()) {
        parameters.add(OperationImplicitParameterReader.implicitParameter(descriptions, param));
      }
    }

    return parameters;
  }
}

@@ -2135,16 +2135,21 @@
 ) %7B%0A    
+List%3C
 Optional
@@ -2167,16 +2167,17 @@
 tParams%3E
+%3E
  annotat
@@ -2179,16 +2179,17 @@
 notation
+s
  = conte
@@ -2196,16 +2196,19 @@
 xt.findA
+llA
 nnotatio
@@ -2208,16 +2208,17 @@
 notation
+s
 (ApiImpl
@@ -2289,16 +2289,144 @@
 st%3C%3E();%0A
+    if (annotations != null && !annotations.isEmpty()) %7B%0A      for (Optional%3CApiImplicitParams%3E annotation : annotations) %7B%0A    
     if (
@@ -2447,24 +2447,28 @@
 resent()) %7B%0A
+    
       for (A
@@ -2515,24 +2515,28 @@
 .value()) %7B%0A
+    
         para
@@ -2620,16 +2620,38 @@
 aram));%0A
+          %7D%0A        %7D%0A
       %7D%0A

support multiple ApiImplicitParameters