mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-02 21:50:55 +00:00
Configure apiNameSuffix via plugins (#12062)
This commit is contained in:
parent
5c1ad2a40b
commit
346dfbc102
@ -204,6 +204,11 @@ apply plugin: 'org.openapi.generator'
|
|||||||
|None
|
|None
|
||||||
|Suffix that will be appended to all model names.
|
|Suffix that will be appended to all model names.
|
||||||
|
|
||||||
|
|apiNameSuffix
|
||||||
|
|String
|
||||||
|
|None
|
||||||
|
|Suffix that will be appended to all api names.
|
||||||
|
|
||||||
|instantiationTypes
|
|instantiationTypes
|
||||||
|Map(String,String)
|
|Map(String,String)
|
||||||
|None
|
|None
|
||||||
|
@ -106,6 +106,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
|
|||||||
modelPackage.set(generate.modelPackage)
|
modelPackage.set(generate.modelPackage)
|
||||||
modelNamePrefix.set(generate.modelNamePrefix)
|
modelNamePrefix.set(generate.modelNamePrefix)
|
||||||
modelNameSuffix.set(generate.modelNameSuffix)
|
modelNameSuffix.set(generate.modelNameSuffix)
|
||||||
|
apiNameSuffix.set(generate.apiNameSuffix)
|
||||||
instantiationTypes.set(generate.instantiationTypes)
|
instantiationTypes.set(generate.instantiationTypes)
|
||||||
typeMappings.set(generate.typeMappings)
|
typeMappings.set(generate.typeMappings)
|
||||||
additionalProperties.set(generate.additionalProperties)
|
additionalProperties.set(generate.additionalProperties)
|
||||||
|
@ -106,6 +106,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
|
|||||||
*/
|
*/
|
||||||
val modelNameSuffix = project.objects.property<String>()
|
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.
|
* Sets instantiation type mappings.
|
||||||
*/
|
*/
|
||||||
@ -326,6 +331,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
|
|||||||
releaseNote.set("Minor update")
|
releaseNote.set("Minor update")
|
||||||
modelNamePrefix.set("")
|
modelNamePrefix.set("")
|
||||||
modelNameSuffix.set("")
|
modelNameSuffix.set("")
|
||||||
|
apiNameSuffix.set("")
|
||||||
generateModelTests.set(true)
|
generateModelTests.set(true)
|
||||||
generateModelDocumentation.set(true)
|
generateModelDocumentation.set(true)
|
||||||
generateApiTests.set(true)
|
generateApiTests.set(true)
|
||||||
|
@ -168,6 +168,13 @@ open class GenerateTask : DefaultTask() {
|
|||||||
@Input
|
@Input
|
||||||
val modelNameSuffix = project.objects.property<String>()
|
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.
|
* Sets instantiation type mappings.
|
||||||
*/
|
*/
|
||||||
@ -573,6 +580,10 @@ open class GenerateTask : DefaultTask() {
|
|||||||
configurator.setModelNameSuffix(value)
|
configurator.setModelNameSuffix(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
apiNameSuffix.ifNotEmpty { value ->
|
||||||
|
configurator.setApiNameSuffix(value)
|
||||||
|
}
|
||||||
|
|
||||||
invokerPackage.ifNotEmpty { value ->
|
invokerPackage.ifNotEmpty { value ->
|
||||||
configurator.setInvokerPackage(value)
|
configurator.setInvokerPackage(value)
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,55 @@ class GenerateTaskDslTest : TestBase() {
|
|||||||
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}")
|
"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
|
@Test
|
||||||
fun `openApiGenerate should used up-to-date instead of regenerate`() {
|
fun `openApiGenerate should used up-to-date instead of regenerate`() {
|
||||||
// Arrange
|
// Arrange
|
||||||
|
@ -67,6 +67,7 @@ mvn clean compile
|
|||||||
| `library` | `openapi.generator.maven.plugin.library` | library template (sub-template)
|
| `library` | `openapi.generator.maven.plugin.library` | library template (sub-template)
|
||||||
| `modelNamePrefix` | `openapi.generator.maven.plugin.modelNamePrefix` | Sets the prefix for model classes and enums
|
| `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
|
| `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
|
| `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
|
| `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)
|
| `removeOperationIdPrefix` | `openapi.generator.maven.plugin.removeOperationIdPrefix` | remove operationId prefix (e.g. user_getName => getName)
|
||||||
|
@ -220,6 +220,12 @@ public class CodeGenMojo extends AbstractMojo {
|
|||||||
@Parameter(name = "modelNameSuffix", property = "openapi.generator.maven.plugin.modelNameSuffix")
|
@Parameter(name = "modelNameSuffix", property = "openapi.generator.maven.plugin.modelNameSuffix")
|
||||||
private String 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
|
* Sets an optional ignoreFileOverride path
|
||||||
*/
|
*/
|
||||||
@ -596,6 +602,10 @@ public class CodeGenMojo extends AbstractMojo {
|
|||||||
configurator.setModelNameSuffix(modelNameSuffix);
|
configurator.setModelNameSuffix(modelNameSuffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isNotEmpty(apiNameSuffix)) {
|
||||||
|
configurator.setApiNameSuffix(apiNameSuffix);
|
||||||
|
}
|
||||||
|
|
||||||
if (null != templateDirectory) {
|
if (null != templateDirectory) {
|
||||||
configurator.setTemplateDir(templateDirectory.getAbsolutePath());
|
configurator.setTemplateDir(templateDirectory.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,7 @@ public class CodeGenMojoTest extends BaseTestCase {
|
|||||||
mojo.execute();
|
mojo.execute();
|
||||||
assertEquals("java", getVariableValueFromObject(mojo, "generatorName"));
|
assertEquals("java", getVariableValueFromObject(mojo, "generatorName"));
|
||||||
assertEquals("jersey2", getVariableValueFromObject(mojo, "library"));
|
assertEquals("jersey2", getVariableValueFromObject(mojo, "library"));
|
||||||
|
assertEquals("Suffix", getVariableValueFromObject(mojo, "apiNameSuffix"));
|
||||||
assertEquals("remote.org.openapitools.client.api", getVariableValueFromObject(mojo, "apiPackage"));
|
assertEquals("remote.org.openapitools.client.api", getVariableValueFromObject(mojo, "apiPackage"));
|
||||||
assertEquals("remote.org.openapitools.client.model", getVariableValueFromObject(mojo, "modelPackage"));
|
assertEquals("remote.org.openapitools.client.model", getVariableValueFromObject(mojo, "modelPackage"));
|
||||||
assertEquals("remote.org.openapitools.client", getVariableValueFromObject(mojo, "invokerPackage"));
|
assertEquals("remote.org.openapitools.client", getVariableValueFromObject(mojo, "invokerPackage"));
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
<configOptions>
|
<configOptions>
|
||||||
<dateLibrary>joda</dateLibrary>
|
<dateLibrary>joda</dateLibrary>
|
||||||
</configOptions>
|
</configOptions>
|
||||||
|
<apiNameSuffix>Suffix</apiNameSuffix>
|
||||||
<library>jersey2</library>
|
<library>jersey2</library>
|
||||||
<output>${basedir}/target/generated-sources/common-maven/remote-openapi</output>
|
<output>${basedir}/target/generated-sources/common-maven/remote-openapi</output>
|
||||||
<apiPackage>remote.org.openapitools.client.api</apiPackage>
|
<apiPackage>remote.org.openapitools.client.api</apiPackage>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user