Skip to content

Commit

Permalink
Configure apiNameSuffix via plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
borsch committed Apr 6, 2022
1 parent edfb3e1 commit 189b11c
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions modules/openapi-generator-gradle-plugin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val modelNameSuffix = project.objects.property<String>()

/**
* Suffix that will be appended to all api names. Default is the empty string.
*/
val apiNameSuffix = project.objects.property<String>()

/**
* Sets instantiation type mappings.
*/
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ open class GenerateTask : DefaultTask() {
@Input
val modelNameSuffix = project.objects.property<String>()

/**
* Suffix that will be appended to all api names. Default is the empty string.
*/
@Optional
@Input
val apiNameSuffix = project.objects.property<String>()

/**
* Sets instantiation type mappings.
*/
Expand Down Expand Up @@ -573,6 +580,10 @@ open class GenerateTask : DefaultTask() {
configurator.setModelNameSuffix(value)
}

apiNameSuffix.ifNotEmpty { value ->
configurator.setApiNameSuffix(value)
}

invokerPackage.ifNotEmpty { value ->
configurator.setInvokerPackage(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions modules/openapi-generator-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<configOptions>
<dateLibrary>joda</dateLibrary>
</configOptions>
<apiNameSuffix>Suffix</apiNameSuffix>
<library>jersey2</library>
<output>${basedir}/target/generated-sources/common-maven/remote-openapi</output>
<apiPackage>remote.org.openapitools.client.api</apiPackage>
Expand Down

0 comments on commit 189b11c

Please sign in to comment.