Skip to content

Commit

Permalink
inputSpec should not be mandatory when inputSpecRootDirectory is set (#…
Browse files Browse the repository at this point in the history
…18000)

* Fix inputSpec to not be mandatory in maven plugin

* Add unit tests
  • Loading branch information
andreas-wirth committed Mar 9, 2024
1 parent 5d959ee commit c87ad56
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -557,6 +557,11 @@ public void setBuildContext(BuildContext buildContext) {

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +33,7 @@
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;
Expand Down Expand Up @@ -181,6 +184,36 @@ public void testCollapsedSpecAddedToArtifacts() throws Exception {
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");
}
Expand Down

0 comments on commit c87ad56

Please sign in to comment.