Skip to content

Commit f26f726

Browse files
fujigonhokamoto
authored andcommitted
Following up for OpenAPITools#3440 (1792 fix remote spec handling and hash calculation) (OpenAPITools#3826)
* This patch fixes the bug that we cannot access to remote files when checking file updates. Following up OpenAPITools#3440, supporting auth. * 1792 fix remote spec handling and hash calculation (OpenAPITools#3440) (cherry picked from commit 2a2eefe) * fix detecting remote file / local file logic while finding the hash file, taking care of IllegalArgumentException for local files. * add testcase
1 parent 8c637fb commit f26f726

File tree

2 files changed

+115
-11
lines changed

2 files changed

+115
-11
lines changed

modules/openapi-generator-maven-plugin/examples/java-client.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<!-- /RELEASE_VERSION -->
1818
<executions>
1919
<execution>
20+
<id>default</id>
2021
<goals>
2122
<goal>generate</goal>
2223
</goals>
@@ -39,6 +40,36 @@
3940
<library>jersey2</library>
4041
</configuration>
4142
</execution>
43+
<execution>
44+
<id>remote</id>
45+
<phase>generate-sources</phase>
46+
<goals>
47+
<goal>generate</goal>
48+
</goals>
49+
<configuration>
50+
<!-- specify the swagger yaml -->
51+
<inputSpec>https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml</inputSpec>
52+
53+
<!-- target to generate java client code -->
54+
<generatorName>java</generatorName>
55+
56+
<!-- hint: if you want to generate java server code, e.g. based on Spring Boot,
57+
you can use the following target: <generatorName>spring</generatorName> -->
58+
59+
<!-- pass any necessary config options -->
60+
<configOptions>
61+
<dateLibrary>joda</dateLibrary>
62+
</configOptions>
63+
64+
<!-- override the default library to jersey2 -->
65+
<library>jersey2</library>
66+
67+
<output>${project.build.directory}/generated-sources/remote-openapi</output>
68+
<apiPackage>remote.org.openapitools.client.api</apiPackage>
69+
<modelPackage>remote.org.openapitools.client.model</modelPackage>
70+
<invokerPackage>remote.org.openapitools.client</invokerPackage>
71+
</configuration>
72+
</execution>
4273
</executions>
4374
</plugin>
4475
<plugin>

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@
2020
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
2121
import static org.openapitools.codegen.config.CodegenConfiguratorUtils.*;
2222

23+
import io.swagger.v3.parser.core.models.AuthorizationValue;
2324
import java.io.File;
25+
import java.io.FileOutputStream;
26+
import java.io.IOException;
27+
import java.net.MalformedURLException;
28+
import java.net.URI;
29+
import java.net.URISyntaxException;
30+
import java.net.URL;
31+
import java.net.URLConnection;
32+
import java.nio.channels.Channels;
33+
import java.nio.channels.FileChannel;
34+
import java.nio.channels.ReadableByteChannel;
2435
import java.util.HashMap;
2536
import java.util.List;
2637
import java.util.Map;
@@ -42,6 +53,7 @@
4253
import org.openapitools.codegen.CodegenConfig;
4354
import org.openapitools.codegen.CodegenConstants;
4455
import org.openapitools.codegen.DefaultGenerator;
56+
import org.openapitools.codegen.auth.AuthParser;
4557
import org.openapitools.codegen.config.CodegenConfigurator;
4658
import org.openapitools.codegen.config.GlobalSettings;
4759
import org.sonatype.plexus.build.incremental.BuildContext;
@@ -429,7 +441,7 @@ public void execute() throws MojoExecutionException {
429441
if (inputSpecFile.exists()) {
430442
File storedInputSpecHashFile = getHashFile(inputSpecFile);
431443
if(storedInputSpecHashFile.exists()) {
432-
String inputSpecHash = Files.asByteSource(inputSpecFile).hash(Hashing.sha256()).toString();
444+
String inputSpecHash = calculateInputSpecHash(inputSpecFile);
433445
String storedInputSpecHash = Files.asCharSource(storedInputSpecHashFile, Charsets.UTF_8).read();
434446
if (inputSpecHash.equals(storedInputSpecHash)) {
435447
getLog().info(
@@ -720,12 +732,7 @@ public void execute() throws MojoExecutionException {
720732

721733
// Store a checksum of the input spec
722734
File storedInputSpecHashFile = getHashFile(inputSpecFile);
723-
ByteSource inputSpecByteSource =
724-
inputSpecFile.exists()
725-
? Files.asByteSource(inputSpecFile)
726-
: CharSource.wrap(ClasspathHelper.loadFileFromClasspath(inputSpecFile.toString().replaceAll("\\\\","/")))
727-
.asByteSource(Charsets.UTF_8);
728-
String inputSpecHash =inputSpecByteSource.hash(Hashing.sha256()).toString();
735+
String inputSpecHash = calculateInputSpecHash(inputSpecFile);
729736

730737
if (storedInputSpecHashFile.getParent() != null && !new File(storedInputSpecHashFile.getParent()).exists()) {
731738
File parent = new File(storedInputSpecHashFile.getParent());
@@ -746,8 +753,75 @@ public void execute() throws MojoExecutionException {
746753
}
747754
}
748755

756+
/**
757+
* Calculate openapi specification file hash. If specification is hosted on remote resource it is downloaded first
758+
*
759+
* @param inputSpecFile - Openapi specification input file to calculate it's hash.
760+
* Does not taken into account if input spec is hosted on remote resource
761+
* @return openapi specification file hash
762+
* @throws IOException
763+
*/
764+
private String calculateInputSpecHash(File inputSpecFile) throws IOException {
765+
766+
URL inputSpecRemoteUrl = inputSpecRemoteUrl();
767+
768+
File inputSpecTempFile = inputSpecFile;
769+
770+
if (inputSpecRemoteUrl != null) {
771+
inputSpecTempFile = File.createTempFile("openapi-spec", ".tmp");
772+
773+
URLConnection conn = inputSpecRemoteUrl.openConnection();
774+
if (isNotEmpty(auth)) {
775+
List<AuthorizationValue> authList = AuthParser.parse(auth);
776+
for (AuthorizationValue auth : authList) {
777+
conn.setRequestProperty(auth.getKeyName(), auth.getValue());
778+
}
779+
}
780+
ReadableByteChannel readableByteChannel = Channels.newChannel(conn.getInputStream());
781+
782+
FileOutputStream fileOutputStream = new FileOutputStream(inputSpecTempFile);
783+
FileChannel fileChannel = fileOutputStream.getChannel();
784+
785+
fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
786+
}
787+
788+
ByteSource inputSpecByteSource =
789+
inputSpecTempFile.exists()
790+
? Files.asByteSource(inputSpecTempFile)
791+
: CharSource.wrap(ClasspathHelper.loadFileFromClasspath(inputSpecTempFile.toString().replaceAll("\\\\","/")))
792+
.asByteSource(Charsets.UTF_8);
793+
794+
return inputSpecByteSource.hash(Hashing.sha256()).toString();
795+
}
796+
797+
/**
798+
* Try to parse inputSpec setting string into URL
799+
* @return A valid URL or null if inputSpec is not a valid URL
800+
*/
801+
private URL inputSpecRemoteUrl(){
802+
try {
803+
return new URI(inputSpec).toURL();
804+
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
805+
return null;
806+
}
807+
}
808+
809+
/**
810+
* Get specification hash file
811+
* @param inputSpecFile - Openapi specification input file to calculate it's hash.
812+
* Does not taken into account if input spec is hosted on remote resource
813+
* @return a file with previously calculated hash
814+
*/
749815
private File getHashFile(File inputSpecFile) {
750-
return new File(output.getPath() + File.separator + ".openapi-generator" + File.separator + inputSpecFile.getName() + ".sha256");
816+
String name = inputSpecFile.getName();
817+
818+
URL url = inputSpecRemoteUrl();
819+
if (url != null) {
820+
String[] segments = url.getPath().split("/");
821+
name = Files.getNameWithoutExtension(segments[segments.length - 1]);
822+
}
823+
824+
return new File(output.getPath() + File.separator + ".openapi-generator" + File.separator + name + ".sha256");
751825
}
752826

753827
private String getCompileSourceRoot() {
@@ -757,8 +831,7 @@ private String getCompileSourceRoot() {
757831
final String sourceFolder =
758832
sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString();
759833

760-
String sourceJavaFolder = output.toString() + "/" + sourceFolder;
761-
return sourceJavaFolder;
834+
return output.toString() + "/" + sourceFolder;
762835
}
763836

764837
private void addCompileSourceRootIfConfigured() {
@@ -803,4 +876,4 @@ private void adjustAdditionalProperties(final CodegenConfig config) {
803876
}
804877
}
805878
}
806-
}
879+
}

0 commit comments

Comments
 (0)