This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch import-oas-validator
in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git

commit 6163e7d9cd59442899070ea75e704974f081819c
Author: Daniel Qian <chanjars...@gmail.com>
AuthorDate: Fri Nov 1 16:33:47 2019 +0800

    SCB-1553 Integrate oas-validator compliance check to cli
    Add compliance check capability to cli
---
 cli/pom.xml                                        |   5 +
 .../apache/servicecomb/toolkit/cli/CheckStyle.java | 125 +++++++++++++++++++++
 .../servicecomb/toolkit/cli/ToolkitMain.java       |   2 +-
 pom.xml                                            |  43 +++++++
 4 files changed, 174 insertions(+), 1 deletion(-)

diff --git a/cli/pom.xml b/cli/pom.xml
index 750c82f..6a222d7 100755
--- a/cli/pom.xml
+++ b/cli/pom.xml
@@ -80,6 +80,11 @@
       <artifactId>log4j-core</artifactId>
       <version>${log4j2.version}</version>
     </dependency>
+
+    <dependency>
+      <groupId>org.apache.servicecomb.toolkit</groupId>
+      <artifactId>oas-validator-compliance</artifactId>
+    </dependency>
   </dependencies>
 
   <build>
diff --git 
a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java 
b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java
new file mode 100644
index 0000000..2ea2f99
--- /dev/null
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/CheckStyle.java
@@ -0,0 +1,125 @@
+/*
+ * 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.toolkit.cli;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.servicecomb.toolkit.oasv.common.OasObjectProperty;
+import org.apache.servicecomb.toolkit.oasv.common.OasObjectPropertyLocation;
+import org.apache.servicecomb.toolkit.oasv.compliance.ComplianceCheckParser;
+import 
org.apache.servicecomb.toolkit.oasv.compliance.factory.DefaultOasSpecValidatorFactory;
+import org.apache.servicecomb.toolkit.oasv.validation.api.OasSpecValidator;
+import org.apache.servicecomb.toolkit.oasv.validation.api.OasValidationContext;
+import org.apache.servicecomb.toolkit.oasv.validation.api.OasViolation;
+import 
org.apache.servicecomb.toolkit.oasv.validation.factory.OasSpecValidatorFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import 
org.springframework.context.annotation.AnnotationConfigApplicationContext;
+
+import io.airlift.airline.Arguments;
+import io.airlift.airline.Command;
+import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.parser.core.models.SwaggerParseResult;
+
+@Command(name = "checkstyle",
+    description = "Check style of OpenAPI v3 spec yaml")
+public class CheckStyle implements Runnable {
+
+  private final static Logger LOGGER = 
LoggerFactory.getLogger(CheckStyle.class);
+
+  @Arguments(
+      title = "file", required = true,
+      description = "OpenAPI v3 spec yaml"
+  )
+  private String filePath;
+
+  @Override
+  public void run() {
+
+    String yaml = null;
+    try {
+      yaml = loadFileContent(filePath);
+    } catch (IOException e) {
+      LOGGER.error(e.getMessage());
+      return;
+    }
+
+    SwaggerParseResult parseResult = ComplianceCheckParser.parseYaml(yaml);
+    OpenAPI openAPI = parseResult.getOpenAPI();
+    if (openAPI == null) {
+      if (CollectionUtils.isNotEmpty(parseResult.getMessages())) {
+        for (String message : parseResult.getMessages()) {
+          LOGGER.error(message);
+        }
+      }
+      LOGGER.error("Parse error");
+      return;
+    }
+
+    OasSpecValidator oasSpecValidator = createOasSpecValidator();
+
+    List<OasViolation> violations = 
oasSpecValidator.validate(createContext(openAPI), openAPI);
+    if (CollectionUtils.isNotEmpty(violations)) {
+      for (OasViolation violation : violations) {
+        LOGGER.info("{}: {}", toPathString(violation.getLocation()), 
violation.getError());
+      }
+      return;
+    }
+    LOGGER.info("Everything is good");
+  }
+
+  private String toPathString(OasObjectPropertyLocation location) {
+    StringBuilder sb = new StringBuilder();
+    List<OasObjectProperty> path = location.getPath();
+    for (OasObjectProperty property : path) {
+      sb.append(property.getName()).append('.');
+    }
+    sb.deleteCharAt(sb.length() - 1);
+    return sb.toString();
+  }
+
+  private OasSpecValidator createOasSpecValidator() {
+    AnnotationConfigApplicationContext ctx = new 
AnnotationConfigApplicationContext(
+        DefaultOasSpecValidatorFactory.class.getPackage().getName());
+    try {
+      OasSpecValidatorFactory oasSpecValidatorFactory = 
ctx.getBean(OasSpecValidatorFactory.class);
+      return oasSpecValidatorFactory.create();
+    } finally {
+      ctx.close();
+    }
+  }
+
+
+  private String loadFileContent(String filePath) throws IOException {
+    Path specPath = Paths.get(filePath);
+    specPath.toAbsolutePath().toString();
+    return FileUtils.readFileToString(specPath.toFile());
+  }
+
+  private OasValidationContext createContext(OpenAPI openAPI) {
+
+    OasValidationContext oasValidationContext = new 
OasValidationContext(openAPI);
+    return oasValidationContext;
+  }
+}
diff --git 
a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java 
b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
index b5da899..7bd38f9 100755
--- a/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
+++ b/cli/src/main/java/org/apache/servicecomb/toolkit/cli/ToolkitMain.java
@@ -37,7 +37,7 @@ public class ToolkitMain {
     builder.withDescription("Microservice development toolkit(version " + 
projectVersion
         + "). ");
     builder.withDefaultCommand(Help.class);
-    builder.withCommands(CodeGenerate.class, DocGenerate.class, Help.class);
+    builder.withCommands(CodeGenerate.class, DocGenerate.class, 
CheckStyle.class, Help.class);
     Runnable cmd = builder.build().parse(args);
 
     cmd.run();
diff --git a/pom.xml b/pom.xml
index f06893e..768f030 100755
--- a/pom.xml
+++ b/pom.xml
@@ -156,6 +156,49 @@
         <artifactId>toolkit-maven-plugin</artifactId>
         <version>${project.version}</version>
       </dependency>
+
+      <dependency>
+        <groupId>org.apache.servicecomb.toolkit</groupId>
+        <artifactId>oas-validator-core</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
+      <dependency>
+        <groupId>org.apache.servicecomb.toolkit</groupId>
+        <artifactId>oas-validator-core-spring</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
+      <dependency>
+        <groupId>org.apache.servicecomb.toolkit</groupId>
+        <artifactId>oas-validator-test</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
+      <dependency>
+        <groupId>org.apache.servicecomb.toolkit</groupId>
+        <artifactId>oas-validator-compliance</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
+      <dependency>
+        <groupId>org.apache.servicecomb.toolkit</groupId>
+        <artifactId>oas-validator-compliance-spring</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
+      <dependency>
+        <groupId>org.apache.servicecomb.toolkit</groupId>
+        <artifactId>oas-validator-compatibility</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
+      <dependency>
+        <groupId>org.apache.servicecomb.toolkit</groupId>
+        <artifactId>oas-validator-compatibility-spring</artifactId>
+        <version>${project.version}</version>
+      </dependency>
+
     </dependencies>
   </dependencyManagement>
 

Reply via email to