forked from loafle/openapi-generator-original
inputSpec should not be mandatory when inputSpecRootDirectory is set (#18000)
* Fix inputSpec to not be mandatory in maven plugin * Add unit tests
This commit is contained in:
@@ -115,14 +115,14 @@ public class CodeGenMojo extends AbstractMojo {
|
||||
/**
|
||||
* Location of the OpenAPI spec, as URL or file.
|
||||
*/
|
||||
@Parameter(name = "inputSpec", property = "openapi.generator.maven.plugin.inputSpec", required = true)
|
||||
private String inputSpec;
|
||||
@Parameter(name = "inputSpec", property = "openapi.generator.maven.plugin.inputSpec")
|
||||
protected String inputSpec;
|
||||
|
||||
/**
|
||||
* Local root folder with spec files
|
||||
*/
|
||||
@Parameter(name = "inputSpecRootDirectory", property = "openapi.generator.maven.plugin.inputSpecRootDirectory")
|
||||
private String inputSpecRootDirectory;
|
||||
protected String inputSpecRootDirectory;
|
||||
|
||||
/**
|
||||
* Name of the file that will contain all merged specs
|
||||
@@ -557,6 +557,11 @@ public class CodeGenMojo extends AbstractMojo {
|
||||
|
||||
@Override
|
||||
public void execute() throws MojoExecutionException {
|
||||
if (StringUtils.isBlank(inputSpec) && StringUtils.isBlank(inputSpecRootDirectory)) {
|
||||
LOGGER.error("inputSpec or inputSpecRootDirectory must be specified");
|
||||
throw new MojoExecutionException("inputSpec or inputSpecRootDirectory must be specified");
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(inputSpecRootDirectory)) {
|
||||
inputSpec = new MergedSpecBuilder(inputSpecRootDirectory, mergedFileName)
|
||||
.buildMergedSpec();
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.openapitools.codegen.plugin;
|
||||
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -31,6 +33,7 @@ import org.apache.maven.execution.DefaultMavenExecutionRequest;
|
||||
import org.apache.maven.execution.MavenExecutionRequest;
|
||||
import org.apache.maven.execution.MavenSession;
|
||||
import org.apache.maven.plugin.MojoExecution;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.project.MavenProject;
|
||||
import org.apache.maven.project.ProjectBuilder;
|
||||
import org.apache.maven.project.ProjectBuildingRequest;
|
||||
@@ -181,6 +184,36 @@ public class CodeGenMojoTest extends BaseTestCase {
|
||||
assertEquals(1, matchingArtifacts.size());
|
||||
}
|
||||
|
||||
public void testAnyInputSpecMustBeProvided() throws Exception {
|
||||
// GIVEN
|
||||
Path folder = Files.createTempDirectory("test");
|
||||
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
|
||||
mojo.inputSpec = null;
|
||||
mojo.inputSpecRootDirectory = null;
|
||||
|
||||
// WHEN
|
||||
MojoExecutionException e = assertThrows(MojoExecutionException.class, mojo::execute);
|
||||
|
||||
// THEN
|
||||
assertEquals("inputSpec or inputSpecRootDirectory must be specified", e.getMessage());
|
||||
}
|
||||
|
||||
public void testInputSpecRootDirectoryDoesNotRequireInputSpec() throws Exception {
|
||||
// GIVEN
|
||||
Path folder = Files.createTempDirectory("test");
|
||||
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
|
||||
mojo.inputSpec = null;
|
||||
mojo.inputSpecRootDirectory = "src/test/resources/default";
|
||||
|
||||
// WHEN
|
||||
mojo.execute();
|
||||
|
||||
// THEN
|
||||
/* Check the hash file was created */
|
||||
final Path hashFolder = folder.resolve("target/generated-sources/common-maven/remote-openapi/.openapi-generator");
|
||||
assertTrue(hashFolder.resolve("_merged_spec.yaml-executionId.sha256").toFile().exists());
|
||||
}
|
||||
|
||||
protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot) throws Exception {
|
||||
return loadMojo(temporaryFolder, projectRoot, "default");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user