Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inputSpec should not be mandatory when inputSpecRootDirectory is set #18000

Merged
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
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
Loading