Configure apiNameSuffix via plugins (#12062)

This commit is contained in:
Oleh Kurpiak 2022-04-09 04:34:06 +03:00 committed by GitHub
parent 5c1ad2a40b
commit 346dfbc102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 85 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -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.
*/
@ -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)

View File

@ -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.
*/
@ -573,6 +580,10 @@ open class GenerateTask : DefaultTask() {
configurator.setModelNameSuffix(value)
}
apiNameSuffix.ifNotEmpty { value ->
configurator.setApiNameSuffix(value)
}
invokerPackage.ifNotEmpty { value ->
configurator.setInvokerPackage(value)
}

View File

@ -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

View File

@ -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)

View File

@ -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 class CodeGenMojo extends AbstractMojo {
configurator.setModelNameSuffix(modelNameSuffix);
}
if (isNotEmpty(apiNameSuffix)) {
configurator.setApiNameSuffix(apiNameSuffix);
}
if (null != templateDirectory) {
configurator.setTemplateDir(templateDirectory.getAbsolutePath());
}

View File

@ -46,6 +46,7 @@ public class CodeGenMojoTest extends BaseTestCase {
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"));

View File

@ -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>