-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
467dc95
commit 2afae7b
Showing
7 changed files
with
271 additions
and
23 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
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
104 changes: 98 additions & 6 deletions
104
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 |
---|---|---|
@@ -1,31 +1,123 @@ | ||
package io.swagger.v3.parser; | ||
|
||
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.FileOutputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
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); | ||
ArgumentParser parser = ArgumentParsers.newFor("swagger-parser").build() | ||
.defaultHelp(true); | ||
parser.addArgument("-i") | ||
.dest("i") | ||
.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("o") | ||
.type(String.class) | ||
.help("output file parsed"); | ||
parser.addArgument("-l") | ||
.dest("l") | ||
.type(String.class) | ||
.help("output error logs"); | ||
try{ | ||
readFromLocation(parser.parseArgs(args)); | ||
}catch (ArgumentParserException e) { | ||
parser.handleError(e); | ||
System.exit(1); | ||
} | ||
} | ||
} | ||
|
||
public static List<String> readFromLocation(String location) { | ||
private static void generateMessagesFile(List<String> messages, Namespace arguments) { | ||
if ( messages != null && !messages.isEmpty() && arguments != null && arguments.getString("l") != null){ | ||
if(arguments.getString("l") != null) { | ||
generateParsedFile(arguments, "l", messages.toString()); | ||
} | ||
} | ||
} | ||
|
||
public static List<String> readFromLocation(Namespace args) { | ||
List<String> messages = new ArrayList<>(); | ||
ParseOptions options = null; | ||
try { | ||
final SwaggerParseResult result = new OpenAPIV3Parser().readLocation(location, null, null); | ||
options = setOptions(args); | ||
final SwaggerParseResult result = new OpenAPIV3Parser().readLocation(args.get("i"), null, options); | ||
if(args.getString("o") != null) { | ||
if (result.getOpenAPI() != null){ | ||
generateParsedFile(args, "o", Yaml.pretty(result.getOpenAPI())); | ||
} | ||
} | ||
if(result.getOpenAPI() == null || !result.getMessages().isEmpty()){ | ||
messages = result.getMessages(); | ||
generateMessagesFile(messages, args); | ||
} | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
System.exit(1); | ||
} | ||
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; | ||
} | ||
} |
93 changes: 82 additions & 11 deletions
93
modules/swagger-parser-cli/src/test/java/SwaggerParserCLITest.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 |
---|---|---|
@@ -1,28 +1,99 @@ | ||
import io.swagger.v3.parser.SwaggerParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
import net.sourceforge.argparse4j.inf.Namespace; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
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); | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/fileWithNoErrorMessages.yaml"); | ||
Namespace namespace = new Namespace(args); | ||
Assert.assertTrue(SwaggerParser.readFromLocation(namespace).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"); | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/fileWithValidationErrorMessages.yaml"); | ||
Namespace namespace = new Namespace(args); | ||
Assert.assertEquals(SwaggerParser.readFromLocation(namespace).toString(), "[attribute info.version is missing, attribute paths.'/cu'(post).responses.200.description is missing]"); | ||
} | ||
|
||
@Test | ||
public void validateErrorTest(){ | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/fileWithValidationErrorMessages.yaml"); | ||
args.put("resolve", "true"); | ||
args.put("resolvefully", "true"); | ||
args.put("o", "target/test-classes/parsedSpec.yaml"); | ||
args.put("l", "target/test-classes/errorLogs.yaml"); | ||
Namespace namespace = new Namespace(args); | ||
Assert.assertEquals(SwaggerParser.readFromLocation(namespace).toString(), "[attribute info.version is missing, 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`"); | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/WrongLocation.yaml"); | ||
args.put("l", "target/test-classes/errorLogs.yaml"); | ||
Namespace namespace = new Namespace(args); | ||
List<String> messages = new ArrayList<>(); | ||
try { | ||
messages = SwaggerParser.readFromLocation(namespace); | ||
}catch (Exception e){ | ||
Assert.fail("error"); | ||
} | ||
Assert.assertTrue( messages.size() == 1); | ||
Assert.assertEquals(messages.toString(), "[Unable to read location `src/test/resources/WrongLocation.yaml`]"); | ||
} | ||
|
||
@Test | ||
public void validateOKFromLocationWithResolveOptionTest(){ | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/internal-references-in-external-files/main.yaml"); | ||
args.put("resolve", "true"); | ||
Namespace namespace = new Namespace(args); | ||
Assert.assertTrue(SwaggerParser.readFromLocation(namespace).size() == 0); | ||
} | ||
|
||
@Test | ||
public void validateOKFromLocationWithResolveFullyOptionTest(){ | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/internal-references-in-external-files/main.yaml"); | ||
args.put("resolve", "true"); | ||
args.put("resolvefully", "true"); | ||
args.put("o", "target/test-classes/parsedSpec.yaml"); | ||
Namespace namespace = new Namespace(args); | ||
|
||
Assert.assertTrue(SwaggerParser.readFromLocation(namespace).size() == 0); | ||
} | ||
|
||
@Test | ||
public void validateOKFromLocationWithOnlyResolveFullyOptionTest(){ | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/internal-references-in-external-files/main.yaml"); | ||
args.put("resolvefully","true"); | ||
Namespace namespace = new Namespace(args); | ||
Assert.assertTrue(SwaggerParser.readFromLocation(namespace).size() == 0); | ||
} | ||
|
||
@Test | ||
public void validateOKFromLocationWithFlattenOptionTest() throws ArgumentParserException { | ||
Map<String,Object> args = new HashMap(); | ||
args.put("i","src/test/resources/internal-references-in-external-files/main.yaml"); | ||
args.put("resolve", "true"); | ||
args.put("resolvefully", "true"); | ||
args.put("flatten", "true"); | ||
args.put("o", "target/test-classes/parsedSpec.yaml"); | ||
args.put("l", "target/test-classes/errorLogs.yaml"); | ||
Namespace namespace = new Namespace(args); | ||
Assert.assertTrue(SwaggerParser.readFromLocation(namespace).size() == 0); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...swagger-parser-cli/src/test/resources/internal-references-in-external-files/external.yaml
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,27 @@ | ||
common: | ||
allOf: | ||
- $ref: "#/core" | ||
- type: object | ||
properties: | ||
attribute: | ||
type: string | ||
direct: | ||
$ref: "#/core" | ||
referenced: | ||
type: array | ||
items: | ||
$ref: "#/core" | ||
|
||
core: | ||
type: object | ||
properties: | ||
coreAttribute: | ||
type: string | ||
inner: | ||
$ref: "#/innerCore" | ||
|
||
innerCore: | ||
type: object | ||
properties: | ||
innerCoreAttribute: | ||
type: string |
29 changes: 29 additions & 0 deletions
29
...les/swagger-parser-cli/src/test/resources/internal-references-in-external-files/main.yaml
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,29 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 0.0.1 | ||
title: API | ||
paths: | ||
/: | ||
post: | ||
summary: Create | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/create' | ||
responses: | ||
'200': | ||
description: Successful response | ||
components: | ||
schemas: | ||
create: | ||
type: object | ||
properties: | ||
attribute: | ||
type: string | ||
direct: | ||
$ref: "./external.yaml#/common" | ||
referenced: | ||
type: array | ||
items: | ||
$ref: "./external.yaml#/common" |