Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions modules/swagger-parser-cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>swagger-parser-project</artifactId>
<groupId>io.swagger.parser.v3</groupId>
<version>2.1.2-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>swagger-parser-cli</artifactId>
<packaging>jar</packaging>
<name>swagger-parser (executable)</name>

<build>
<finalName>swagger-parser-cli</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>logback.xml</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>process-resources</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>false</minimizeJar>
<createDependencyReducedPom>true</createDependencyReducedPom>
<dependencyReducedPomLocation>
${java.io.tmpdir}/dependency-reduced-pom.xml
</dependencyReducedPomLocation>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-v3</artifactId>
<version>2.1.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.swagger.v3.parser;

import io.swagger.v3.parser.core.models.SwaggerParseResult;
import java.util.ArrayList;
import java.util.List;

public class SwaggerParser {
public static void main(String[] args) {
if (args.length > 0){
List<String> messages = readFromLocation(args[0]);
if ( messages.size() > 0){
messages.forEach(System.out::println);
System.exit(1);
}
}
}

public static List<String> readFromLocation(String location) {
List<String> messages = new ArrayList<>();
try {
final SwaggerParseResult result = new OpenAPIV3Parser().readLocation(location, null, null);
if(result.getOpenAPI() == null || !result.getMessages().isEmpty()){
messages = result.getMessages();
}
}catch (Exception e){
e.printStackTrace();
System.exit(1);
}
return messages;
}
}
28 changes: 28 additions & 0 deletions modules/swagger-parser-cli/src/test/java/SwaggerParserCLITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import io.swagger.v3.parser.SwaggerParser;
import org.testng.Assert;
import org.testng.annotations.Test;

public class SwaggerParserCLITest {
@Test
public void validateOKFromLocationTest(){
String []args = new String[1];
args[0] = "src/test/resources/fileWithNoErrorMessages.yaml";
Assert.assertTrue(SwaggerParser.readFromLocation(args[0]).size() == 0);
}

@Test
public void validateErrorFromLocationTest(){
String []args = new String[1];
args[0] = "src/test/resources/fileWithValidationErrorMessages.yaml";
Assert.assertEquals(SwaggerParser.readFromLocation(args[0]).get(0), "attribute info.version is missing");
Assert.assertEquals(SwaggerParser.readFromLocation(args[0]).get(1), "attribute paths.'/cu'(post).responses.200.description is missing");
}

@Test
public void validateFileNotFoundInLocationTest(){
String []args = new String[1];
args[0] = "src/test/resources/WrongLocation.yaml";
Assert.assertTrue(SwaggerParser.readFromLocation(args[0]).size() == 1);
Assert.assertEquals(SwaggerParser.readFromLocation(args[0]).get(0), "Unable to read location `src/test/resources/WrongLocation.yaml`");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
openapi: 3.0.0
info:
description: test
title: test
version: 1.0
paths:
/cu:
post:
operationId: savecu
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: "#/components/schemas/AbTestFoo"
"/bar":
put:
operationId: updateBar
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: "#/components/schemas/CoTestBar"
servers:
- url: /foo/bar
components:
schemas:
Thing:
type: object
properties:
moreThings:
type: array
uniqueItems: true
items:
$ref: "#/components/schemas/ThingAs"
ThingAs:
type: object
properties:
concept:
$ref: "#/components/schemas/Thing"
AbTestFoo:
type: object
properties:
moreThings:
type: array
uniqueItems: true
items:
$ref: "#/components/schemas/ThingAs"
readOnly: true
CoTestBar:
allOf:
- $ref: "#/components/schemas/Thing"
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
openapi: 3.0.0
info:
description: test
title: test
paths:
/cu:
post:
operationId: savecu
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/AbTestFoo"
"/bar":
put:
operationId: updateBar
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: "#/components/schemas/CoTestBar"
servers:
- url: /foo/bar
components:
schemas:
Thing:
type: object
properties:
moreThings:
type: array
uniqueItems: true
items:
$ref: "#/components/schemas/ThingAs"
ThingAs:
type: object
properties:
concept:
$ref: "#/components/schemas/Thing"
AbTestFoo:
type: object
properties:
moreThings:
type: array
uniqueItems: true
items:
$ref: "#/components/schemas/ThingAs"
readOnly: true
CoTestBar:
allOf:
- $ref: "#/components/schemas/Thing"
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@
<module>modules/swagger-parser-v3</module>
<module>modules/swagger-parser-v2-converter</module>
<module>modules/swagger-parser</module>
<module>modules/swagger-parser-cli</module>
</modules>
<repositories>
<repository>
Expand Down