From 189b11c22f7855758390bffb21b724119a1b4737 Mon Sep 17 00:00:00 2001 From: Oleh Kurpiak Date: Wed, 6 Apr 2022 13:22:26 +0300 Subject: [PATCH] Configure apiNameSuffix via plugins --- .../README.adoc | 5 ++ .../gradle/plugin/OpenApiGeneratorPlugin.kt | 1 + .../OpenApiGeneratorGenerateExtension.kt | 6 +++ .../gradle/plugin/tasks/GenerateTask.kt | 11 +++++ .../src/test/kotlin/GenerateTaskDslTest.kt | 49 +++++++++++++++++++ .../openapi-generator-maven-plugin/README.md | 1 + .../codegen/plugin/CodeGenMojo.java | 10 ++++ .../codegen/plugin/CodeGenMojoTest.java | 1 + .../src/test/resources/default/pom.xml | 1 + 9 files changed, 85 insertions(+) diff --git a/modules/openapi-generator-gradle-plugin/README.adoc b/modules/openapi-generator-gradle-plugin/README.adoc index 51f6e08e15f9..d904f4150817 100644 --- a/modules/openapi-generator-gradle-plugin/README.adoc +++ b/modules/openapi-generator-gradle-plugin/README.adoc @@ -204,6 +204,11 @@ apply plugin: 'org.openapi.generator' |None |Suffix that will be appended to all model names. +|apiNameSuffix +|String +|None +|Suffix that will be appended to all api names. + |instantiationTypes |Map(String,String) |None diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt index be865049c770..21436595abd1 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt @@ -106,6 +106,7 @@ class OpenApiGeneratorPlugin : Plugin { modelPackage.set(generate.modelPackage) modelNamePrefix.set(generate.modelNamePrefix) modelNameSuffix.set(generate.modelNameSuffix) + apiNameSuffix.set(generate.apiNameSuffix) instantiationTypes.set(generate.instantiationTypes) typeMappings.set(generate.typeMappings) additionalProperties.set(generate.additionalProperties) diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt index 46aa9540cfa3..69002705d11c 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt @@ -106,6 +106,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) { */ val modelNameSuffix = project.objects.property() + /** + * Suffix that will be appended to all api names. Default is the empty string. + */ + val apiNameSuffix = project.objects.property() + /** * Sets instantiation type mappings. */ @@ -326,6 +331,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) { releaseNote.set("Minor update") modelNamePrefix.set("") modelNameSuffix.set("") + apiNameSuffix.set("") generateModelTests.set(true) generateModelDocumentation.set(true) generateApiTests.set(true) diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt index d7a0c989de2a..cd693b17136b 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt @@ -168,6 +168,13 @@ open class GenerateTask : DefaultTask() { @Input val modelNameSuffix = project.objects.property() + /** + * Suffix that will be appended to all api names. Default is the empty string. + */ + @Optional + @Input + val apiNameSuffix = project.objects.property() + /** * Sets instantiation type mappings. */ @@ -573,6 +580,10 @@ open class GenerateTask : DefaultTask() { configurator.setModelNameSuffix(value) } + apiNameSuffix.ifNotEmpty { value -> + configurator.setApiNameSuffix(value) + } + invokerPackage.ifNotEmpty { value -> configurator.setInvokerPackage(value) } diff --git a/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt b/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt index f8871e12fae8..14c8f358f1bb 100644 --- a/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt +++ b/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt @@ -68,6 +68,55 @@ class GenerateTaskDslTest : TestBase() { "Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}") } + @Test + fun `should apply prefix & suffix config parameters`() { + // Arrange + val projectFiles = mapOf( + "spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml") + ) + withProject(""" + plugins { + id 'org.openapi.generator' + } + openApiGenerate { + generatorName = "java" + inputSpec = file("spec.yaml").absolutePath + outputDir = file("build/java").absolutePath + apiPackage = "org.openapitools.example.api" + invokerPackage = "org.openapitools.example.invoker" + modelPackage = "org.openapitools.example.model" + modelNamePrefix = "ModelPref" + modelNameSuffix = "Suff" + apiNameSuffix = "ApiClassSuffix" + configOptions = [ + dateLibrary: "java8" + ] + } + """.trimIndent(), projectFiles) + + // Act + val result = GradleRunner.create() + .withProjectDir(temp) + .withArguments("openApiGenerate") + .withPluginClasspath() + .build() + + // Assert + assertTrue(result.output.contains("Successfully generated code to"), "User friendly generate notice is missing.") + + listOf( + "build/java/src/main/java/org/openapitools/example/model/ModelPrefPetSuff.java", + "build/java/src/main/java/org/openapitools/example/model/ModelPrefErrorSuff.java", + "build/java/src/main/java/org/openapitools/example/api/PetsApiClassSuffix.java" + ).map { + val f = File(temp, it) + assertTrue(f.exists() && f.isFile, "An expected file was not generated when invoking the generation. - " + f) + } + + assertEquals(TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome, + "Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}") + } + @Test fun `openApiGenerate should used up-to-date instead of regenerate`() { // Arrange diff --git a/modules/openapi-generator-maven-plugin/README.md b/modules/openapi-generator-maven-plugin/README.md index 3102d196ffa3..a9b551e834e1 100644 --- a/modules/openapi-generator-maven-plugin/README.md +++ b/modules/openapi-generator-maven-plugin/README.md @@ -67,6 +67,7 @@ mvn clean compile | `library` | `openapi.generator.maven.plugin.library` | library template (sub-template) | `modelNamePrefix` | `openapi.generator.maven.plugin.modelNamePrefix` | Sets the prefix for model classes and enums | `modelNameSuffix` | `openapi.generator.maven.plugin.modelNameSuffix` | Sets the suffix for model classes and enums +| `apiNameSuffix` | `openapi.generator.maven.plugin.apiNameSuffix` | Sets the suffix for api classes | `ignoreFileOverride` | `openapi.generator.maven.plugin.ignoreFileOverride` | specifies the full path to a `.openapi-generator-ignore` used for pattern based overrides of generated outputs | `httpUserAgent` | `openapi.generator.maven.plugin.httpUserAgent` | Sets custom User-Agent header value | `removeOperationIdPrefix` | `openapi.generator.maven.plugin.removeOperationIdPrefix` | remove operationId prefix (e.g. user_getName => getName) diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index bc32b412f628..2435aab27f33 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -220,6 +220,12 @@ public class CodeGenMojo extends AbstractMojo { @Parameter(name = "modelNameSuffix", property = "openapi.generator.maven.plugin.modelNameSuffix") private String modelNameSuffix; + /** + * Sets the suffix for api classes + */ + @Parameter(name = "apiNameSuffix", property = "openapi.generator.maven.plugin.apiNameSuffix") + private String apiNameSuffix; + /** * Sets an optional ignoreFileOverride path */ @@ -596,6 +602,10 @@ public void execute() throws MojoExecutionException { configurator.setModelNameSuffix(modelNameSuffix); } + if (isNotEmpty(apiNameSuffix)) { + configurator.setApiNameSuffix(apiNameSuffix); + } + if (null != templateDirectory) { configurator.setTemplateDir(templateDirectory.getAbsolutePath()); } diff --git a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java index dd2083d017e3..4dea86da7dc1 100644 --- a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java +++ b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java @@ -46,6 +46,7 @@ public void testCommonConfiguration() throws Exception { mojo.execute(); assertEquals("java", getVariableValueFromObject(mojo, "generatorName")); assertEquals("jersey2", getVariableValueFromObject(mojo, "library")); + assertEquals("Suffix", getVariableValueFromObject(mojo, "apiNameSuffix")); assertEquals("remote.org.openapitools.client.api", getVariableValueFromObject(mojo, "apiPackage")); assertEquals("remote.org.openapitools.client.model", getVariableValueFromObject(mojo, "modelPackage")); assertEquals("remote.org.openapitools.client", getVariableValueFromObject(mojo, "invokerPackage")); diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml index 95ab2451e5d8..277b183629df 100644 --- a/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml +++ b/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml @@ -34,6 +34,7 @@ joda + Suffix jersey2 ${basedir}/target/generated-sources/common-maven/remote-openapi remote.org.openapitools.client.api