diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 32e007608571..11b3599af37f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -395,12 +395,7 @@ private void generateModelTests(List files, Map models, St this.templateProcessor.skip(modelTestFile.toPath(), "Test files never overwrite an existing file of the same name."); } else { File written = processTemplateToFile(models, templateName, filename, generateModelTests, CodegenConstants.MODEL_TESTS, config.modelTestFileFolder()); - if (written != null) { - files.add(written); - if (config.isEnablePostProcessFile() && !dryRun) { - config.postProcessFile(written, "model-test"); - } - } + addGeneratedFile(files, written, "model-test"); } } else if (dryRun) { Path skippedPath = java.nio.file.Paths.get(filename); @@ -416,12 +411,7 @@ private void generateModelDocumentation(List files, Map mo String filename = config.modelDocFileFolder() + File.separator + config.toModelDocFilename(modelName) + suffix; File written = processTemplateToFile(models, templateName, filename, generateModelDocumentation, CodegenConstants.MODEL_DOCS); - if (written != null) { - files.add(written); - if (config.isEnablePostProcessFile() && !dryRun) { - config.postProcessFile(written, "model-doc"); - } - } + addGeneratedFile(files, written, "model-doc"); } } @@ -436,12 +426,7 @@ private void generateModel(List files, Map models, String String filename = config.modelFilename(templateName, modelName); written = processTemplateToFile(models, templateName, filename, generateModels, CodegenConstants.MODELS); } - if (written != null) { - files.add(written); - if (config.isEnablePostProcessFile() && !dryRun) { - config.postProcessFile(written, "model"); - } - } + addGeneratedFile(files, written, "model"); } } @@ -797,12 +782,7 @@ void generateApis(List files, List allOperations, List files, List allOperations, List files, List allOperations, List files, List allWebhooks, List files, List allWebhooks, List files, List allWebhooks, List files, Map bundl } File written = processTemplateToFile(bundle, support.getTemplateFile(), outputFilename, shouldGenerate, CodegenConstants.SUPPORTING_FILES); - if (written != null) { - files.add(written); - if (config.isEnablePostProcessFile() && !dryRun) { - config.postProcessFile(written, "supporting-file"); - } - } + addGeneratedFile(files, written, "supporting-file"); } catch (Exception e) { throw new RuntimeException("Could not generate supporting file '" + support + "'", e); } @@ -1137,12 +1087,7 @@ private void generateSupportingFiles(List files, Map bundl shouldGenerate = supportingFilesToGenerate.contains(openapiGeneratorIgnore); } File written = processTemplateToFile(bundle, openapiGeneratorIgnore, ignoreFileNameTarget, shouldGenerate, CodegenConstants.SUPPORTING_FILES); - if (written != null) { - files.add(written); - if (config.isEnablePostProcessFile() && !dryRun) { - config.postProcessFile(written, "openapi-generator-ignore"); - } - } + addGeneratedFile(files, written, "openapi-generator-ignore"); } catch (Exception e) { throw new RuntimeException("Could not generate supporting file '" + ignoreFileNameTarget + "'", e); } @@ -1444,6 +1389,22 @@ private void processUserDefinedTemplates() { } } + /** + * Tracks a generated file and post-processes it if enabled (never on a dry run). + * + * @param files The list tracking generated files + * @param written The file that was written, or null if no file was written + * @param fileType The file type passed to {@link CodegenConfig#postProcessFile(File, String)} + */ + private void addGeneratedFile(List files, File written, String fileType) { + if (written != null) { + files.add(written); + if (config.isEnablePostProcessFile() && !dryRun) { + config.postProcessFile(written, fileType); + } + } + } + protected File processTemplateToFile(Map templateData, String templateName, String outputFilename, boolean shouldGenerate, String skippedByOption) throws IOException { return processTemplateToFile(templateData, templateName, outputFilename, shouldGenerate, skippedByOption, this.config.getOutputDir()); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java index 5da01b91254f..24d1e3d5e370 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultGeneratorTest.java @@ -27,6 +27,11 @@ import java.nio.file.StandardOpenOption; import java.util.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -79,6 +84,57 @@ public void testPostProcessRunsWhenQuietIsDisabled() throws IOException { } } + @Test + public void testPostProcessFileReceivesFileTypes() throws IOException { + Path target = Files.createTempDirectory("test"); + File output = target.toFile(); + try { + ClientOptInput clientOptInput = new CodegenConfigurator() + .setGeneratorName("java") + .setInputSpec("src/test/resources/3_0/petstore.yaml") + .setOutputDir(target.toAbsolutePath().toString()) + .setEnablePostProcessFile(true) + .toClientOptInput(); + + CodegenConfig configSpy = spy(clientOptInput.getConfig()); + doNothing().when(configSpy).postProcessFile(any(), any()); + clientOptInput.config(configSpy); + + DefaultGenerator generator = new DefaultGenerator(); + generator.opts(clientOptInput).generate(); + + verify(configSpy).postProcessFile(argThat(f -> f.getName().equals("Pet.java")), eq("model")); + verify(configSpy).postProcessFile(argThat(f -> f.getName().equals("PetApi.java")), eq("api")); + verify(configSpy).postProcessFile(argThat(f -> f.getName().equals("pom.xml")), eq("supporting-file")); + } finally { + output.deleteOnExit(); + } + } + + @Test + public void testPostProcessFileIsNotCalledOnDryRun() throws IOException { + Path target = Files.createTempDirectory("test"); + File output = target.toFile(); + try { + ClientOptInput clientOptInput = new CodegenConfigurator() + .setGeneratorName("java") + .setInputSpec("src/test/resources/3_0/pingSomeObj.yaml") + .setOutputDir(target.toAbsolutePath().toString()) + .setEnablePostProcessFile(true) + .toClientOptInput(); + + CodegenConfig configSpy = spy(clientOptInput.getConfig()); + clientOptInput.config(configSpy); + + DefaultGenerator generator = new DefaultGenerator(true); + generator.opts(clientOptInput).generate(); + + verify(configSpy, never()).postProcessFile(any(), any()); + } finally { + output.deleteOnExit(); + } + } + @Test public void testIgnoreFileProcessing() throws IOException { Path target = Files.createTempDirectory("test");