From 2afae7b605961eedfb8547ad79cddc05b527d5d2 Mon Sep 17 00:00:00 2001 From: gracekarina Date: Tue, 23 Aug 2022 12:28:26 -0500 Subject: [PATCH] validate java version before build --- .github/actions/action.yml | 23 +++- .github/workflows/validate-workflow.yml | 8 +- modules/swagger-parser-cli/pom.xml | 10 ++ .../io/swagger/v3/parser/SwaggerParser.java | 104 +++++++++++++++++- .../src/test/java/SwaggerParserCLITest.java | 93 ++++++++++++++-- .../external.yaml | 27 +++++ .../main.yaml | 29 +++++ 7 files changed, 271 insertions(+), 23 deletions(-) create mode 100644 modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/external.yaml create mode 100644 modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/main.yaml diff --git a/.github/actions/action.yml b/.github/actions/action.yml index 2ef5baea21..3dc2340516 100644 --- a/.github/actions/action.yml +++ b/.github/actions/action.yml @@ -4,15 +4,23 @@ inputs: inputSpec: description: 'file of the openapi definition' required: true - + options: + description: 'parser options' + required: false + logsPath: + description: 'file of the logs messages or errors' + required: true + parserSpecPath: + description: 'path where a file can be found with parsed definition' + required: false runs: using: "composite" steps: - id: java-version - run: 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 + if: steps.java-version == null || steps.java-version < 1.8 uses: actions/setup-java@v3 with: distribution: 'zulu' @@ -20,6 +28,11 @@ runs: - 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=LATEST" -o swagger-parser-cli.jar shell: bash - - name: Execute Jar - run: java -jar ./swagger-parser-cli.jar ${{ inputs.inputSpec }} + - id: execute + name: Execute Jar + run: java -jar swagger-parser-cli.jar ${{ inputs.inputSpec }} ${{ inputs.options }} ${{ inputs.parserSpecPath }} ${{ inputs.logsPath }} + shell: bash + - id: test + name: test file + run: cat ${{ inputs.parserSpecPath }} shell: bash \ No newline at end of file diff --git a/.github/workflows/validate-workflow.yml b/.github/workflows/validate-workflow.yml index ce298b50f8..9ee1f182b1 100644 --- a/.github/workflows/validate-workflow.yml +++ b/.github/workflows/validate-workflow.yml @@ -12,4 +12,10 @@ jobs: ref: action - uses: ./.github/actions/ with: - inputSpec: '/home/runner/work/swagger-parser/swagger-parser/modules/swagger-parser-cli/src/test/resources/fileWithValidationErrorMessages.yaml' + inputSpec: '/home/runner/work/swagger-parser/swagger-parser/modules/swagger-parser-cli/src/test/resources/fileWithNoErrorMessages.yaml' + options: '-resolve -resolveFully' + logsPath: '/home/runner/work/swagger-parser/swagger-parser/modules/swagger-parser-cli/target/test-classes/parserLogs.yaml' + parserSpecPath: '/home/runner/work/swagger-parser/swagger-parser/modules/swagger-parser-cli/target/test-classes/specParsed.yaml' + + + diff --git a/modules/swagger-parser-cli/pom.xml b/modules/swagger-parser-cli/pom.xml index 5ba74616ca..00f993ae61 100644 --- a/modules/swagger-parser-cli/pom.xml +++ b/modules/swagger-parser-cli/pom.xml @@ -76,6 +76,16 @@ org.testng testng + + net.sourceforge.argparse4j + argparse4j + 0.9.0 + + + org.slf4j + slf4j-simple + 2.0.0 + diff --git a/modules/swagger-parser-cli/src/main/java/io/swagger/v3/parser/SwaggerParser.java b/modules/swagger-parser-cli/src/main/java/io/swagger/v3/parser/SwaggerParser.java index 8fa2ec402a..888ffbc7d8 100644 --- a/modules/swagger-parser-cli/src/main/java/io/swagger/v3/parser/SwaggerParser.java +++ b/modules/swagger-parser-cli/src/main/java/io/swagger/v3/parser/SwaggerParser.java @@ -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 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 readFromLocation(String location) { + private static void generateMessagesFile(List 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 readFromLocation(Namespace args) { List 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; + } } \ No newline at end of file diff --git a/modules/swagger-parser-cli/src/test/java/SwaggerParserCLITest.java b/modules/swagger-parser-cli/src/test/java/SwaggerParserCLITest.java index fa68bd9568..eed2e255d1 100644 --- a/modules/swagger-parser-cli/src/test/java/SwaggerParserCLITest.java +++ b/modules/swagger-parser-cli/src/test/java/SwaggerParserCLITest.java @@ -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 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 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 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 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 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 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 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 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 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); } } \ No newline at end of file diff --git a/modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/external.yaml b/modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/external.yaml new file mode 100644 index 0000000000..361c56fffd --- /dev/null +++ b/modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/external.yaml @@ -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 diff --git a/modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/main.yaml b/modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/main.yaml new file mode 100644 index 0000000000..95e501c614 --- /dev/null +++ b/modules/swagger-parser-cli/src/test/resources/internal-references-in-external-files/main.yaml @@ -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"