-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from swagger-api/master
Get latest changes from swagger-parser origin repository
- Loading branch information
Showing
29 changed files
with
994 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: 'Swagger Parser' | ||
description: 'Parses, validates, dereferences an OpenAPI definition' | ||
inputs: | ||
inputSpec: | ||
description: 'path to OpenAPI definition file' | ||
required: true | ||
options: | ||
description: 'parser options' | ||
required: false | ||
serializationType: | ||
description: 'result file serialization type' | ||
required: false | ||
logsPath: | ||
description: 'path to validation result file' | ||
required: false | ||
parserSpecPath: | ||
description: 'output path of the serialized parsed definition' | ||
required: false | ||
parserVersion: | ||
description: 'parser version' | ||
required: false | ||
runs: | ||
using: "composite" | ||
steps: | ||
- id: java-version | ||
run: java -version 2>&1 | fgrep -i version | cut -d'"' -f2 | sed -e 's/^1\./1\%/' -e 's/\..*//' -e 's/%/./' | ||
shell: bash | ||
- name: Build Java | ||
if: steps.java-version == null || steps.java-version < 1.8 | ||
uses: actions/setup-java@v3 | ||
with: | ||
distribution: 'zulu' | ||
java-version: '11' | ||
- name: Download JAR | ||
run: curl -L 'https://repository.sonatype.org/service/local/artifact/maven/content?r=central-proxy&g=io.swagger.parser.v3&a=swagger-parser-cli&e=jar&v=${{ inputs.parserVersion }}' -o swagger-parser-cli.jar | ||
shell: bash | ||
- id: execute | ||
name: Execute Jar | ||
run: java -jar swagger-parser-cli.jar -i /home/runner/work/${{ inputs.inputSpec }} ${{ inputs.options }} ${{ inputs.serializationType }} -o /home/runner/work/${{ inputs.parserSpecPath }} -l /home/runner/work/${{ inputs.logsPath }} | ||
shell: bash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?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.5-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-jar-plugin</artifactId> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>io.swagger.v3.parser.SwaggerParser</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<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.5-SNAPSHOT</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.sourceforge.argparse4j</groupId> | ||
<artifactId>argparse4j</artifactId> | ||
<version>0.9.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-simple</artifactId> | ||
<version>2.0.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
</project> |
154 changes: 154 additions & 0 deletions
154
modules/swagger-parser-cli/src/main/java/io/swagger/v3/parser/SwaggerParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package io.swagger.v3.parser; | ||
|
||
import io.swagger.v3.core.util.Json; | ||
import io.swagger.v3.core.util.Yaml; | ||
|
||
import io.swagger.v3.parser.core.models.ParseOptions; | ||
import io.swagger.v3.parser.core.models.SwaggerParseResult; | ||
import net.sourceforge.argparse4j.ArgumentParsers; | ||
import net.sourceforge.argparse4j.impl.Arguments; | ||
import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
import net.sourceforge.argparse4j.inf.Namespace; | ||
|
||
|
||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class SwaggerParser { | ||
|
||
public static final String RESOLVE = "resolve"; | ||
public static final String RESOLVEFULLY = "resolvefully"; | ||
public static final String FLATTEN = "flatten"; | ||
public static final String JSON = "json"; | ||
public static final String YAML = "yaml"; | ||
public static final String LOG_ERRORS = "l"; | ||
public static final String OUTPUT_FILE = "o"; | ||
public static final String TRUE = "true"; | ||
public static final String INPUT_FILE = "i"; | ||
|
||
public static void main(String[] args) { | ||
if (args.length > 0){ | ||
ArgumentParser parser = ArgumentParsers.newFor("swagger-parser").build() | ||
.defaultHelp(true); | ||
parser.addArgument("-i") | ||
.dest(INPUT_FILE) | ||
.required(true) | ||
.type(String.class) | ||
.help("input file to be parsed"); | ||
parser.addArgument("-resolve") | ||
.dest(RESOLVE) | ||
.type(Boolean.class) | ||
.action(Arguments.storeTrue()) | ||
.setDefault(false) | ||
.help("resolve remote or local references"); | ||
parser.addArgument("-resolveFully") | ||
.dest(RESOLVEFULLY) | ||
.type(Boolean.class) | ||
.action(Arguments.storeTrue()) | ||
.setDefault(false) | ||
.help(""); | ||
parser.addArgument("-flatten") | ||
.dest(FLATTEN) | ||
.type(Boolean.class) | ||
.action(Arguments.storeTrue()) | ||
.setDefault(false) | ||
.help(""); | ||
parser.addArgument("-o") | ||
.dest(OUTPUT_FILE) | ||
.type(String.class) | ||
.help("output file parsed"); | ||
parser.addArgument("-l") | ||
.dest(LOG_ERRORS) | ||
.type(String.class) | ||
.help("output error logs"); | ||
parser.addArgument("-json") | ||
.dest(JSON) | ||
.type(Boolean.class) | ||
.action(Arguments.storeTrue()) | ||
.setDefault(false) | ||
.help("generate file as JSON"); | ||
parser.addArgument("-yaml") | ||
.dest(YAML) | ||
.type(Boolean.class) | ||
.action(Arguments.storeTrue()) | ||
.setDefault(false) | ||
.help("generate file as YAML"); | ||
try{ | ||
readFromLocation(parser.parseArgs(args)); | ||
}catch (ArgumentParserException e) { | ||
parser.handleError(e); | ||
System.exit(1); | ||
} | ||
} | ||
} | ||
|
||
private static void generateMessagesFile(List<String> messages, Namespace arguments) { | ||
if ( messages != null && !messages.isEmpty() && arguments != null && arguments.getString(LOG_ERRORS) != null){ | ||
if(arguments.getString(LOG_ERRORS) != null) { | ||
generateParsedFile(arguments, LOG_ERRORS, messages.toString()); | ||
} | ||
} | ||
} | ||
|
||
public static List<String> readFromLocation(Namespace args) { | ||
List<String> messages = new ArrayList<>(); | ||
ParseOptions options; | ||
try { | ||
options = setOptions(args); | ||
final SwaggerParseResult result = new OpenAPIV3Parser().readLocation(args.get(INPUT_FILE), null, options); | ||
if(args.getString(OUTPUT_FILE) != null) { | ||
if (result.getOpenAPI() != null){ | ||
String output; | ||
if(args.getString(JSON) != null && args.getString(JSON).equals(TRUE)){ | ||
output = Json.pretty(result.getOpenAPI()); | ||
}else if(args.getString(YAML) != null && args.getString(YAML).equals(TRUE)){ | ||
output = Yaml.pretty(result.getOpenAPI()); | ||
}else{ | ||
output= Yaml.pretty(result.getOpenAPI()); | ||
} | ||
generateParsedFile(args, OUTPUT_FILE, output ); | ||
} | ||
} | ||
if(result.getOpenAPI() == null || !result.getMessages().isEmpty()){ | ||
messages = result.getMessages(); | ||
generateMessagesFile(messages, args); | ||
} | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
return messages; | ||
} | ||
|
||
private static void generateParsedFile(Namespace args, String o, String result) { | ||
try { | ||
if(result != null) { | ||
OutputStream out = Files.newOutputStream(Paths.get(args.getString(o))); | ||
byte[] specBytes = result.getBytes(); | ||
out.write(specBytes); | ||
out.close(); | ||
} | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static ParseOptions setOptions(Namespace parseOptions) { | ||
ParseOptions options = new ParseOptions(); | ||
|
||
if (parseOptions.getString(RESOLVE) !=null && parseOptions.getString(RESOLVE).equals(TRUE)) { | ||
options.setResolve(true); | ||
} | ||
if (parseOptions.getString(RESOLVEFULLY) != null && parseOptions.getString(RESOLVEFULLY).equals(TRUE)) { | ||
options.setResolveFully(true); | ||
} | ||
if (parseOptions.getString(FLATTEN) != null && parseOptions.getString(FLATTEN).equals(TRUE)) { | ||
options.setFlatten(true); | ||
} | ||
return options; | ||
} | ||
} |
Oops, something went wrong.