Skip to content

Commit

Permalink
Fix OpenAPITools#8027 - import the auto generated supporting JSON cla…
Browse files Browse the repository at this point in the history
…ss only when generateSupportingFiles is true
  • Loading branch information
MosheElisha committed Jan 27, 2021
1 parent 13513b3 commit 3f73667
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ void configureGeneratorProperties() {

config.additionalProperties().put(CodegenConstants.GENERATE_APIS, generateApis);
config.additionalProperties().put(CodegenConstants.GENERATE_MODELS, generateModels);
config.additionalProperties().put("generateSupportingFiles", generateSupportingFiles);

if (!generateApiTests && !generateModelTests) {
config.additionalProperties().put(CodegenConstants.EXCLUDE_TESTS, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ import javax.validation.Valid;
{{#performBeanValidation}}
import org.hibernate.validator.constraints.*;
{{/performBeanValidation}}
{{#generateSupportingFiles}}
import {{invokerPackage}}.JSON;
{{/generateSupportingFiles}}

{{#models}}
{{#model}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,81 @@ public void testJdkHttpClient() throws Exception {
"import java.net.http.HttpRequest;");
}

@Test
public void testJdkHttpClientWithSupportingFiles() throws Exception {
Map<String, Object> properties = new HashMap<>();
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
properties.put(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model");
properties.put(CodegenConstants.INVOKER_PACKAGE, "xyz.abcdef.invoker");

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.NATIVE)
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/pingSomeObj.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true");
List<File> files = generator.opts(clientOptInput).generate();

Assert.assertEquals(files.size(), 34);
validateJavaSourceFiles(files);

TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/api/PingApi.java"),
"public class PingApi",
"import java.net.http.HttpClient;",
"import java.net.http.HttpRequest;",
"import java.net.http.HttpResponse;");

TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/model/SomeObj.java"),
"import xyz.abcdef.invoker.JSON;",
"public class SomeObj {");
}

@Test
public void testJdkHttpClientWithoutSupportingFiles() throws Exception {
Map<String, Object> properties = new HashMap<>();
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
properties.put(CodegenConstants.MODEL_PACKAGE, "xyz.abcdef.model");
properties.put(CodegenConstants.INVOKER_PACKAGE, "xyz.abcdef.invoker");

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.NATIVE)
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/pingSomeObj.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
List<File> files = generator.opts(clientOptInput).generate();

Assert.assertEquals(files.size(), 6);
validateJavaSourceFiles(files);

TestUtils.assertFileContains(Paths.get(output + "/src/main/java/xyz/abcdef/api/PingApi.java"),
"public class PingApi",
"import java.net.http.HttpClient;",
"import java.net.http.HttpRequest;",
"import java.net.http.HttpResponse;");

TestUtils.assertFileNotContains(Paths.get(output + "/src/main/java/xyz/abcdef/model/SomeObj.java"),
"import xyz.abcdef.invoker.JSON;");
}

@Test
public void testJdkHttpAsyncClient() throws Exception {
Map<String, Object> properties = new HashMap<>();
Expand Down

0 comments on commit 3f73667

Please sign in to comment.