Fix python generation when custom files and templates are specified (#9572)

* fix python generation when custom files and templates are specified

* add test for processUserDefinedTemplates
This commit is contained in:
Shyla Srinivas 2021-06-02 09:13:10 -07:00 committed by GitHub
parent 7bb7c72cc5
commit 3c866fb4a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 2 deletions

View File

@ -938,7 +938,7 @@ public class DefaultGenerator implements Generator {
// TODO: initial behavior is "merge" user defined with built-in templates. consider offering user a "replace" option.
if (userDefinedTemplates != null && !userDefinedTemplates.isEmpty()) {
Map<String, SupportingFile> supportingFilesMap = config.supportingFiles().stream()
.collect(Collectors.toMap(TemplateDefinition::getTemplateFile, Function.identity()));
.collect(Collectors.toMap(TemplateDefinition::getTemplateFile, Function.identity(), (oldValue, newValue) -> oldValue));
// TemplateFileType.SupportingFiles
userDefinedTemplates.stream()

View File

@ -662,5 +662,62 @@ public class DefaultGeneratorTest {
Assert.assertEquals(servers.get(1).url, "http://trailingshlash.io:80/v1");
Assert.assertEquals(servers.get(2).url, "http://notrailingslash.io:80/v2");
}
}
@Test
public void testProcessUserDefinedTemplatesWithConfig() throws IOException {
Path target = Files.createTempDirectory("test");
Path templates = Files.createTempDirectory("templates");
File output = target.toFile();
try {
// Create custom template
File customTemplate = new File(templates.toFile(), "README.mustache");
new File(customTemplate.getParent()).mkdirs();
Files.write(customTemplate.toPath(),
"# {{someKey}}".getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE);
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("python")
.setInputSpec("src/test/resources/3_0/petstore.yaml")
.setPackageName("io.something")
.setTemplateDir(templates.toAbsolutePath().toString())
.addAdditionalProperty("files", "src/test/resources/sampleConfig.json:\n\t folder: supportingjson "+
"\n\t destinationFilename: supportingconfig.json \n\t templateType: SupportingFiles")
.setSkipOverwrite(false)
.setOutputDir(target.toAbsolutePath().toString());
final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator(false);
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "true");
generator.setGeneratorPropertyDefault(CodegenConstants.API_DOCS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
generator.setGeneratorPropertyDefault(CodegenConstants.API_TESTS, "false");
List<File> files = generator.opts(clientOptInput).generate();
// remove commented code based on review - files does not seem to be supported in CodegenConfigurator
// supporting files sanity check
// TestUtils.ensureContainsFile(files, output, "sampleConfig.json");
// Assert.assertTrue(new File(output, "sampleConfig.json").exists());
// Generator should report api_client.py as a generated file
TestUtils.ensureContainsFile(files, output, "io/something/api_client.py");
// Generated file should exist on the filesystem after generation
File apiClient = new File(output, "io/something/api_client.py");
Assert.assertTrue(apiClient.exists());
// Generated file should contain our custom packageName
TestUtils.assertFileContains(apiClient.toPath(),
"from io.something import rest"
);
} finally {
output.delete();
templates.toFile().delete();
}
}
}