/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.servicecomb.swagger.invocation.validator;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;
import javax.validation.groups.Default;

import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation;
import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
import org.apache.servicecomb.swagger.invocation.extension.ProducerInvokeExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ParameterValidator implements ProducerInvokeExtension {

  private static final Logger LOGGER = LoggerFactory.getLogger(ParameterValidator.class);

  private static ExecutableValidator executableValidator;

  @Override
  public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation,
      Object[] args)
      throws ConstraintViolationException {

    if (null == executableValidator) {
      ValidatorFactory factory =
          Validation.byDefaultProvider()
              .configure()
              .parameterNameProvider(new DefaultParameterNameProvider())
              .buildValidatorFactory();
      executableValidator = factory.getValidator().forExecutables();
    }
    Set<ConstraintViolation<Object>> violations =
        executableValidator.validateParameters(producerOperation.getProducerInstance(),
            producerOperation.getProducerMethod(),
            args,
            Default.class);
    if (violations.size() > 0) {
      LOGGER.warn("Parameter validation failed : " + violations.toString());
      throw new ConstraintViolationException(violations);
    }
  }

  @Override
  public int getOrder() {
    return 100;
  }
}

@@ -1150,24 +1150,173 @@
 s.Default;%0A%0A
+import com.netflix.config.DynamicBooleanProperty;%0Aimport com.netflix.config.DynamicPropertyFactory;%0Aimport org.apache.servicecomb.config.ConfigUtil;%0A
 import org.a
@@ -1743,16 +1743,540 @@
 .class);
+%0A  private static final String PARAM_VALIDATION_ENABLED = %22servicecomb.rest.parameter.validation.enabled%22;%0A  private final DynamicBooleanProperty paramValidationEnabled = DynamicPropertyFactory.getInstance()%0A          .getBooleanProperty(PARAM_VALIDATION_ENABLED, true);%0A%0A  public ParameterValidator() %7B%0A    paramValidationEnabled.addCallback(() -%3E %7B%0A      boolean newValue = paramValidationEnabled.get();%0A      LOGGER.info(%22%7B%7D changed from %7B%7D to %7B%7D%22, PARAM_VALIDATION_ENABLED, paramValidationEnabled, newValue);%0A    %7D);%0A  %7D
 %0A%0A  priv
@@ -2516,17 +2516,56 @@
 ption %7B%0A
-%0A
+    if(paramValidationEnabled.get())%7B%0A  
     if (
@@ -2597,24 +2597,26 @@
 or) %7B%0A      
+  
 ValidatorFac
@@ -2630,16 +2630,22 @@
 ctory =%0A
+      
         
@@ -2691,16 +2691,26 @@
         
+    
+      
 .configu
@@ -2710,24 +2710,34 @@
 configure()%0A
+          
             
@@ -2811,16 +2811,26 @@
         
+     
+     
 .buildVa
@@ -2843,24 +2843,26 @@
 rFactory();%0A
+  
       execut
@@ -2926,14 +2926,18 @@
     
+  
 %7D%0A    
+ 
+ 
 Set%3C
@@ -2978,16 +2978,22 @@
 tions =%0A
+      
         
@@ -3084,16 +3084,26 @@
         
+   
+       
 producer
@@ -3145,16 +3145,26 @@
         
+          
 args,%0A  
@@ -3173,16 +3173,26 @@
         
+    
+      
 Default.
@@ -3195,24 +3195,26 @@
 ult.class);%0A
+  
     if (viol
@@ -3230,24 +3230,26 @@
 ze() %3E 0) %7B%0A
+  
       LOGGER
@@ -3315,24 +3315,26 @@
 g());%0A      
+  
 throw new Co
@@ -3369,24 +3369,32 @@
 iolations);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 

Allow users to disable the param validation by configuration (#1571)