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:
andreas-wirth
2024-03-09 08:34:13 +01:00
committed by GitHub
parent 5d959eeae6
commit c87ad561f1
2 changed files with 41 additions and 3 deletions

View File

@@ -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();

View File

@@ -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");
}