Skip to content

Commit 03733f8

Browse files
authored
Merge pull request #1773 from swagger-api/parser-cli
initial Swagger Parser CLI
2 parents 7208a54 + d018ed5 commit 03733f8

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed

modules/swagger-parser-cli/pom.xml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>swagger-parser-project</artifactId>
7+
<groupId>io.swagger.parser.v3</groupId>
8+
<version>2.1.2-SNAPSHOT</version>
9+
<relativePath>../..</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>swagger-parser-cli</artifactId>
14+
<packaging>jar</packaging>
15+
<name>swagger-parser (executable)</name>
16+
17+
<build>
18+
<finalName>swagger-parser-cli</finalName>
19+
<resources>
20+
<resource>
21+
<directory>src/main/resources</directory>
22+
<filtering>true</filtering>
23+
<excludes>
24+
<exclude>logback.xml</exclude>
25+
</excludes>
26+
</resource>
27+
</resources>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-shade-plugin</artifactId>
32+
<version>2.3</version>
33+
<executions>
34+
<execution>
35+
<id>process-resources</id>
36+
<phase>package</phase>
37+
<goals>
38+
<goal>shade</goal>
39+
</goals>
40+
<configuration>
41+
<minimizeJar>false</minimizeJar>
42+
<createDependencyReducedPom>true</createDependencyReducedPom>
43+
<dependencyReducedPomLocation>
44+
${java.io.tmpdir}/dependency-reduced-pom.xml
45+
</dependencyReducedPomLocation>
46+
<transformers>
47+
<transformer
48+
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
49+
</transformers>
50+
</configuration>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
<dependencies>
58+
<dependency>
59+
<groupId>io.swagger.parser.v3</groupId>
60+
<artifactId>swagger-parser-v3</artifactId>
61+
<version>2.1.2-SNAPSHOT</version>
62+
<scope>compile</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.testng</groupId>
66+
<artifactId>testng</artifactId>
67+
</dependency>
68+
</dependencies>
69+
70+
<properties>
71+
<maven.compiler.source>8</maven.compiler.source>
72+
<maven.compiler.target>8</maven.compiler.target>
73+
</properties>
74+
75+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.swagger.v3.parser;
2+
3+
import io.swagger.v3.parser.core.models.SwaggerParseResult;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class SwaggerParser {
8+
public static void main(String[] args) {
9+
if (args.length > 0){
10+
List<String> messages = readFromLocation(args[0]);
11+
if ( messages.size() > 0){
12+
messages.forEach(System.out::println);
13+
System.exit(1);
14+
}
15+
}
16+
}
17+
18+
public static List<String> readFromLocation(String location) {
19+
List<String> messages = new ArrayList<>();
20+
try {
21+
final SwaggerParseResult result = new OpenAPIV3Parser().readLocation(location, null, null);
22+
if(result.getOpenAPI() == null || !result.getMessages().isEmpty()){
23+
messages = result.getMessages();
24+
}
25+
}catch (Exception e){
26+
e.printStackTrace();
27+
System.exit(1);
28+
}
29+
return messages;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import io.swagger.v3.parser.SwaggerParser;
2+
import org.testng.Assert;
3+
import org.testng.annotations.Test;
4+
5+
public class SwaggerParserCLITest {
6+
@Test
7+
public void validateOKFromLocationTest(){
8+
String []args = new String[1];
9+
args[0] = "src/test/resources/fileWithNoErrorMessages.yaml";
10+
Assert.assertTrue(SwaggerParser.readFromLocation(args[0]).size() == 0);
11+
}
12+
13+
@Test
14+
public void validateErrorFromLocationTest(){
15+
String []args = new String[1];
16+
args[0] = "src/test/resources/fileWithValidationErrorMessages.yaml";
17+
Assert.assertEquals(SwaggerParser.readFromLocation(args[0]).get(0), "attribute info.version is missing");
18+
Assert.assertEquals(SwaggerParser.readFromLocation(args[0]).get(1), "attribute paths.'/cu'(post).responses.200.description is missing");
19+
}
20+
21+
@Test
22+
public void validateFileNotFoundInLocationTest(){
23+
String []args = new String[1];
24+
args[0] = "src/test/resources/WrongLocation.yaml";
25+
Assert.assertTrue(SwaggerParser.readFromLocation(args[0]).size() == 1);
26+
Assert.assertEquals(SwaggerParser.readFromLocation(args[0]).get(0), "Unable to read location `src/test/resources/WrongLocation.yaml`");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
openapi: 3.0.0
2+
info:
3+
description: test
4+
title: test
5+
version: 1.0
6+
paths:
7+
/cu:
8+
post:
9+
operationId: savecu
10+
responses:
11+
"200":
12+
description: successful operation
13+
content:
14+
application/json:
15+
schema:
16+
$ref: "#/components/schemas/AbTestFoo"
17+
"/bar":
18+
put:
19+
operationId: updateBar
20+
responses:
21+
"200":
22+
description: successful operation
23+
content:
24+
application/json:
25+
schema:
26+
$ref: "#/components/schemas/CoTestBar"
27+
servers:
28+
- url: /foo/bar
29+
components:
30+
schemas:
31+
Thing:
32+
type: object
33+
properties:
34+
moreThings:
35+
type: array
36+
uniqueItems: true
37+
items:
38+
$ref: "#/components/schemas/ThingAs"
39+
ThingAs:
40+
type: object
41+
properties:
42+
concept:
43+
$ref: "#/components/schemas/Thing"
44+
AbTestFoo:
45+
type: object
46+
properties:
47+
moreThings:
48+
type: array
49+
uniqueItems: true
50+
items:
51+
$ref: "#/components/schemas/ThingAs"
52+
readOnly: true
53+
CoTestBar:
54+
allOf:
55+
- $ref: "#/components/schemas/Thing"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
openapi: 3.0.0
2+
info:
3+
description: test
4+
title: test
5+
paths:
6+
/cu:
7+
post:
8+
operationId: savecu
9+
responses:
10+
"200":
11+
content:
12+
application/json:
13+
schema:
14+
$ref: "#/components/schemas/AbTestFoo"
15+
"/bar":
16+
put:
17+
operationId: updateBar
18+
responses:
19+
"200":
20+
description: successful operation
21+
content:
22+
application/json:
23+
schema:
24+
$ref: "#/components/schemas/CoTestBar"
25+
servers:
26+
- url: /foo/bar
27+
components:
28+
schemas:
29+
Thing:
30+
type: object
31+
properties:
32+
moreThings:
33+
type: array
34+
uniqueItems: true
35+
items:
36+
$ref: "#/components/schemas/ThingAs"
37+
ThingAs:
38+
type: object
39+
properties:
40+
concept:
41+
$ref: "#/components/schemas/Thing"
42+
AbTestFoo:
43+
type: object
44+
properties:
45+
moreThings:
46+
type: array
47+
uniqueItems: true
48+
items:
49+
$ref: "#/components/schemas/ThingAs"
50+
readOnly: true
51+
CoTestBar:
52+
allOf:
53+
- $ref: "#/components/schemas/Thing"

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
<module>modules/swagger-parser-v3</module>
378378
<module>modules/swagger-parser-v2-converter</module>
379379
<module>modules/swagger-parser</module>
380+
<module>modules/swagger-parser-cli</module>
380381
</modules>
381382
<repositories>
382383
<repository>

0 commit comments

Comments
 (0)