Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remote spec handling and hash calculation #3440

Merged
merged 1 commit into from
Sep 4, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

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;
Expand Down Expand Up @@ -422,7 +431,7 @@ public void execute() throws MojoExecutionException {
if (inputSpecFile.exists()) {
File storedInputSpecHashFile = getHashFile(inputSpecFile);
if(storedInputSpecHashFile.exists()) {
String inputSpecHash = Files.asByteSource(inputSpecFile).hash(Hashing.sha256()).toString();
String inputSpecHash = calculateInputSpecHash(inputSpecFile);
String storedInputSpecHash = Files.asCharSource(storedInputSpecHashFile, Charsets.UTF_8).read();
if (inputSpecHash.equals(storedInputSpecHash)) {
getLog().info(
Expand Down Expand Up @@ -697,12 +706,7 @@ public void execute() throws MojoExecutionException {

// Store a checksum of the input spec
File storedInputSpecHashFile = getHashFile(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();
String inputSpecHash = calculateInputSpecHash(inputSpecFile);

if (storedInputSpecHashFile.getParent() != null && !new File(storedInputSpecHashFile.getParent()).exists()) {
File parent = new File(storedInputSpecHashFile.getParent());
Expand All @@ -723,8 +727,70 @@ public void execute() throws MojoExecutionException {
}
}

/**
* 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) {
return new File(output.getPath() + File.separator + ".openapi-generator" + File.separator + inputSpecFile.getName() + ".sha256");
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");
}

private String getCompileSourceRoot() {
Expand All @@ -734,8 +800,7 @@ private String getCompileSourceRoot() {
final String sourceFolder =
sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString();

String sourceJavaFolder = output.toString() + "/" + sourceFolder;
return sourceJavaFolder;
return output.toString() + "/" + sourceFolder;
}

private void addCompileSourceRootIfConfigured() {
Expand Down Expand Up @@ -780,4 +845,4 @@ private void adjustAdditionalProperties(final CodegenConfig config) {
}
}
}
}
}