From 03edb6403098cf0b32bc347ed665c09f97e92c96 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 4 Sep 2019 19:01:47 +0800 Subject: [PATCH] Revert "1792 fix remote spec handling and hash calculation (#3440)" This reverts commit 2a2eefe93d81b8d253745b8adb002ab2cb9eee04. --- .../codegen/plugin/CodeGenMojo.java | 87 +++---------------- 1 file changed, 11 insertions(+), 76 deletions(-) diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index aa7ec747a4df..d16295b9327d 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -21,15 +21,6 @@ import static org.apache.commons.lang3.StringUtils.isNotEmpty; import static org.openapitools.codegen.config.CodegenConfiguratorUtils.*; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.channels.Channels; -import java.nio.channels.FileChannel; -import java.nio.channels.ReadableByteChannel; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -438,7 +429,7 @@ public class CodeGenMojo extends AbstractMojo { if (inputSpecFile.exists()) { File storedInputSpecHashFile = getHashFile(inputSpecFile); if(storedInputSpecHashFile.exists()) { - String inputSpecHash = calculateInputSpecHash(inputSpecFile); + String inputSpecHash = Files.asByteSource(inputSpecFile).hash(Hashing.sha256()).toString(); String storedInputSpecHash = Files.asCharSource(storedInputSpecHashFile, Charsets.UTF_8).read(); if (inputSpecHash.equals(storedInputSpecHash)) { getLog().info( @@ -729,7 +720,12 @@ public class CodeGenMojo extends AbstractMojo { // Store a checksum of the input spec File storedInputSpecHashFile = getHashFile(inputSpecFile); - String inputSpecHash = calculateInputSpecHash(inputSpecFile); + ByteSource inputSpecByteSource = + inputSpecFile.exists() + ? Files.asByteSource(inputSpecFile) + : CharSource.wrap(ClasspathHelper.loadFileFromClasspath(inputSpecFile.toString().replaceAll("\\\\","/"))) + .asByteSource(Charsets.UTF_8); + String inputSpecHash =inputSpecByteSource.hash(Hashing.sha256()).toString(); if (storedInputSpecHashFile.getParent() != null && !new File(storedInputSpecHashFile.getParent()).exists()) { File parent = new File(storedInputSpecHashFile.getParent()); @@ -750,70 +746,8 @@ public class CodeGenMojo extends AbstractMojo { } } - /** - * Calculate openapi specification file hash. If specification is hosted on remote resource it is downloaded first - * - * @param inputSpecFile - Openapi specification input file to calculate it's hash. - * Does not taken into account if input spec is hosted on remote resource - * @return openapi specification file hash - * @throws IOException - */ - private String calculateInputSpecHash(File inputSpecFile) throws IOException { - - URL inputSpecRemoteUrl = inputSpecRemoteUrl(); - - File inputSpecTempFile = inputSpecFile; - - if (inputSpecRemoteUrl != null) { - inputSpecTempFile = File.createTempFile("openapi-spec", ".tmp"); - - ReadableByteChannel readableByteChannel = Channels.newChannel(inputSpecRemoteUrl.openStream()); - - FileOutputStream fileOutputStream = new FileOutputStream(inputSpecTempFile); - FileChannel fileChannel = fileOutputStream.getChannel(); - - fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE); - } - - ByteSource inputSpecByteSource = - inputSpecTempFile.exists() - ? Files.asByteSource(inputSpecTempFile) - : CharSource.wrap(ClasspathHelper.loadFileFromClasspath(inputSpecTempFile.toString().replaceAll("\\\\","/"))) - .asByteSource(Charsets.UTF_8); - - return inputSpecByteSource.hash(Hashing.sha256()).toString(); - } - - /** - * Try to parse inputSpec setting string into URL - * @return A valid URL or null if inputSpec is not a valid URL - */ - private URL inputSpecRemoteUrl(){ - try { - return new URI(inputSpec).toURL(); - } catch (URISyntaxException e) { - return null; - } catch (MalformedURLException e) { - return null; - } - } - - /** - * Get specification hash file - * @param inputSpecFile - Openapi specification input file to calculate it's hash. - * Does not taken into account if input spec is hosted on remote resource - * @return a file with previously calculated hash - */ private File getHashFile(File inputSpecFile) { - String name = inputSpecFile.getName(); - - URL url = inputSpecRemoteUrl(); - if (url != null) { - String[] segments = url.getPath().split("/"); - name = Files.getNameWithoutExtension(segments[segments.length - 1]); - } - - return new File(output.getPath() + File.separator + ".openapi-generator" + File.separator + name + ".sha256"); + return new File(output.getPath() + File.separator + ".openapi-generator" + File.separator + inputSpecFile.getName() + ".sha256"); } private String getCompileSourceRoot() { @@ -823,7 +757,8 @@ public class CodeGenMojo extends AbstractMojo { final String sourceFolder = sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString(); - return output.toString() + "/" + sourceFolder; + String sourceJavaFolder = output.toString() + "/" + sourceFolder; + return sourceJavaFolder; } private void addCompileSourceRootIfConfigured() { @@ -868,4 +803,4 @@ public class CodeGenMojo extends AbstractMojo { } } } -} \ No newline at end of file +}