Skip to content
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions modules/openapi-generator-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<!-- <version>4.12</version> -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
<version>3.6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-verifier</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -42,13 +44,15 @@
import com.google.common.io.CharSource;
import io.swagger.v3.parser.util.ClasspathHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

import org.openapitools.codegen.CliOption;
Expand All @@ -71,7 +75,7 @@
* Goal which generates client/server code from a OpenAPI json/yaml definition.
*/
@SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"})
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class CodeGenMojo extends AbstractMojo {

private final Logger LOGGER = LoggerFactory.getLogger(CodeGenMojo.class);
Expand Down Expand Up @@ -500,7 +504,13 @@ public void execute() throws MojoExecutionException {
}

if (isNotEmpty(inputSpec)) {
configurator.setInputSpec(inputSpec);
URL url = inputSpecRemoteUrl();

if ((! inputSpecFile.exists()) && url != null) {
configurator.setInputSpec(url.toString());
} else {
configurator.setInputSpec(inputSpec);
}
}

if (isNotEmpty(gitHost)) {
Expand Down Expand Up @@ -825,15 +835,35 @@ private String calculateInputSpecHash(File inputSpecFile) throws IOException {
}

/**
* Try to parse inputSpec setting string into URL
* Try to parse inputSpec setting string into URL (truly remote or resource)
* @return A valid URL or null if inputSpec is not a valid URL
*/
private URL inputSpecRemoteUrl(){
try {
return new URI(inputSpec).toURL();
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
return null;
private URL inputSpecRemoteUrl() {
URL url = dependencyClassLoader().getResource(inputSpec);

if (url == null) {
try {
url = new URI(inputSpec).toURL();
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
}
}

return url;
}

private ClassLoader dependencyClassLoader() {
List<URL> list = new ArrayList<>();

for (Artifact artifact : project.getArtifacts()) {
try {
if (artifact.isResolved() && artifact.getType().equals("jar")) {
list.add(new URL("jar:" + artifact.getFile().toURI() + "!/"));
}
} catch (Exception e) {
}
}

return new URLClassLoader(list.toArray(new URL[] { }), getClass().getClassLoader());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.LocalRepository;
import org.openapitools.codegen.plugin.stubs.StubUtility;

import java.io.File;
Expand All @@ -38,10 +42,22 @@ protected void setUp() throws Exception {
super.setUp();
}

public void testCommonConfigurationWithFileInputSpec() throws Exception {
testCommonConfiguration("file");
}

public void testCommonConfigurationWithResourceInputSpec() throws Exception {
testCommonConfiguration("resource");
}

public void testCommonConfigurationWithURLInputSpec() throws Exception {
testCommonConfiguration("url");
}

@SuppressWarnings("unchecked")
public void testCommonConfiguration() throws Exception {
private void testCommonConfiguration(String profile) throws Exception {
File folder = Files.createTempDirectory("test").toFile();
CodeGenMojo mojo = loadMojo(folder, "src/test/resources/default");
CodeGenMojo mojo = loadMojo(folder, "src/test/resources/default", profile);
mojo.execute();
assertEquals("java", getVariableValueFromObject(mojo, "generatorName"));
assertEquals("jersey2", getVariableValueFromObject(mojo, "library"));
Expand All @@ -57,7 +73,7 @@ public void testCommonConfiguration() throws Exception {
public void testHashGenerationFileContainsExecutionId() throws Exception {
// GIVEN
Path folder = Files.createTempDirectory("test");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "file", "executionId");

// WHEN
mojo.execute();
Expand All @@ -67,14 +83,14 @@ public void testHashGenerationFileContainsExecutionId() throws Exception {
assertTrue(hashFolder.resolve("petstore.yaml-executionId.sha256").toFile().exists());
}

protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot) throws Exception {
return loadMojo(temporaryFolder, projectRoot, "default");
protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String profile) throws Exception {
return loadMojo(temporaryFolder, projectRoot, profile, "default");
}

protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String executionId) throws Exception {
protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String profile, String executionId) throws Exception {
File file = new File(projectRoot);
FileUtils.copyDirectory(file, temporaryFolder);
MavenProject project = readMavenProject(temporaryFolder);
MavenProject project = readMavenProject(temporaryFolder, profile);
MavenSession session = newMavenSession(project);
MojoExecution execution = newMojoExecution("generate");
MojoExecution executionWithId = copyWithExecutionId(executionId, execution);
Expand All @@ -87,15 +103,24 @@ private MojoExecution copyWithExecutionId(String executionId, MojoExecution exec
return executionWithId;
}

protected MavenProject readMavenProject(File basedir)
protected MavenProject readMavenProject(File basedir, String profile)
throws Exception {
File pom = new File(basedir, "pom.xml");
LocalRepository localRepo = new LocalRepository(new File(basedir, "local-repo"));
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
RepositorySystem system = locator.getService(RepositorySystem.class);
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory(basedir);
if (profile != null) {
request.addActiveProfile(profile);
}
ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
configuration.setRepositorySession(new DefaultRepositorySystemSession());
configuration.setRepositorySession(session);
configuration.setResolveDependencies(true);
MavenProject project = lookup(ProjectBuilder.class).build(pom, configuration).getProject();
assertNotNull(project);
return project;
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<version>1</version>
<description>POM was created from install:install-file</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<versioning>
<release>1</release>
<versions>
<version>1</version>
</versions>
<lastUpdated>20210724220722</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright 2020 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -22,14 +22,41 @@
<version>1.0.0-SNAPSHOT</version>
<name>OpenAPI Generator Configuration Test</name>
<url>https://openapi-generator.tech/</url>
<profiles>
<profile>
<id>file</id>
<properties>
<inputSpec>${basedir}/src/main/resources/petstore.yaml</inputSpec>
</properties>
</profile>
<profile>
<id>resource</id>
<properties>
<inputSpec>petstore.yaml</inputSpec>
</properties>
</profile>
<profile>
<id>url</id>
<properties>
<inputSpec>jar:file:${basedir}/local-repo/petstore/schema/1/schema-1.jar!/petstore.yaml</inputSpec>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<finalName>common-maven</finalName>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<inputSpec>${basedir}/petstore.yaml</inputSpec>
<inputSpec>${inputSpec}</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<dateLibrary>joda</dateLibrary>
Expand All @@ -52,4 +79,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<settings>
<activeProfiles>
<activeProfile>default</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>default</id>
<localRepository>${basedir}/local-repo</localRepository>
</profile>
</profiles>
</settings>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<swagger-core-version>2.1.2</swagger-core-version>
<swagger-parser-groupid>io.swagger.parser.v3</swagger-parser-groupid>
<swagger-parser-version>2.0.26</swagger-parser-version>
<swagger-parser-version>2.0.28-SNAPSHOT</swagger-parser-version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will need to wait for the 2.0.28 release before merging this enhancement.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that 2.1.12 has already been released

<felix-version>3.3.1</felix-version>
<commons-io-version>2.4</commons-io-version>
<commons-cli-version>1.2</commons-cli-version>
Expand Down