[Kotlin] add apiSuffix configuration (#2690)

* add apiSuffix configuration

* Add default value for Api suffix

* remove overriding method toApiName

* refactor the global option apiSuffix to kotlin specific feature

* add missing Option for apiSuffix

* extend readme.md for apiSuffix configuration

* update doc

* add testcase
This commit is contained in:
fj-roman 2019-05-07 15:37:32 +02:00 committed by William Cheng
parent f0346aeeb2
commit 7eb2be9c99
7 changed files with 37 additions and 8 deletions

View File

@ -9,6 +9,7 @@ sidebar_label: kotlin-server
| ------ | ----------- | ------ | ------- |
|sourceFolder|source folder for generated code| |src/main/kotlin|
|packageName|Generated artifact package name.| |org.openapitools|
|apiSuffix|suffix for api classes| |Api|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|artifactId|Generated artifact id (name of jar).| |null|
|artifactVersion|Generated artifact's package version.| |1.0.0|

View File

@ -9,6 +9,7 @@ sidebar_label: kotlin-spring
| ------ | ----------- | ------ | ------- |
|sourceFolder|source folder for generated code| |src/main/kotlin|
|packageName|Generated artifact package name.| |org.openapitools|
|apiSuffix|suffix for api classes| |Api|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|artifactId|Generated artifact id (name of jar).| |null|
|artifactVersion|Generated artifact's package version.| |1.0.0|

View File

@ -9,6 +9,7 @@ sidebar_label: kotlin
| ------ | ----------- | ------ | ------- |
|sourceFolder|source folder for generated code| |src/main/kotlin|
|packageName|Generated artifact package name.| |org.openapitools|
|apiSuffix|suffix for api classes| |Api|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|artifactId|Generated artifact id (name of jar).| |null|
|artifactVersion|Generated artifact's package version.| |1.0.0|

View File

@ -37,6 +37,9 @@ public class CodegenConstants {
public static final String API_PACKAGE = "apiPackage";
public static final String API_PACKAGE_DESC = "package for generated api classes";
public static final String API_SUFFIX = "apiSuffix";
public static final String API_SUFFIX_DESC = "suffix for api classes";
public static final String MODEL_PACKAGE = "modelPackage";
public static final String MODEL_PACKAGE_DESC = "package for generated models";

View File

@ -584,14 +584,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
return toModelName(name) + "Test";
}
@Override
public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultApi";
}
return camelize(name) + "Api";
}
@Override
public String toApiFilename(String name) {
return toApiName(name);

View File

@ -41,6 +41,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
protected String artifactVersion = "1.0.0";
protected String groupId = "org.openapitools";
protected String packageName = "org.openapitools";
protected String apiSuffix = "Api";
protected String sourceFolder = "src/main/kotlin";
@ -195,6 +196,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
cliOptions.clear();
addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder);
addOption(CodegenConstants.PACKAGE_NAME, "Generated artifact package name.", packageName);
addOption(CodegenConstants.API_SUFFIX, CodegenConstants.API_SUFFIX_DESC, apiSuffix);
addOption(CodegenConstants.GROUP_ID, "Generated artifact package's organization (i.e. maven groupId).", groupId);
addOption(CodegenConstants.ARTIFACT_ID, "Generated artifact id (name of jar).", artifactId);
addOption(CodegenConstants.ARTIFACT_VERSION, "Generated artifact's package version.", artifactVersion);
@ -337,6 +339,10 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
}
if (additionalProperties.containsKey(CodegenConstants.API_SUFFIX)) {
this.setApiSuffix((String) additionalProperties.get(CodegenConstants.API_SUFFIX));
}
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_ID)) {
this.setArtifactId((String) additionalProperties.get(CodegenConstants.ARTIFACT_ID));
} else {
@ -396,6 +402,10 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
this.packageName = packageName;
}
public void setApiSuffix(String apiSuffix) {
this.apiSuffix = apiSuffix;
}
public void setSourceFolder(String sourceFolder) {
this.sourceFolder = sourceFolder;
}
@ -463,6 +473,14 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
return super.toInstantiationType(p);
}
@Override
public String toApiName(String name) {
if (name.length() == 0) {
return "DefaultApi";
}
return (this.apiSuffix.isEmpty() ? camelize(name) : camelize(name) + this.apiSuffix);
}
/**
* Return the fully-qualified "Model" name for import
*

View File

@ -123,4 +123,17 @@ public class AbstractKotlinCodegenTest {
assertEquals(codegen.toVarName("USER123NAME"), "USER123NAME");
}
@Test
public void convertApiNameWithEmptySuffix() {
assertEquals(codegen.toApiName("Fake"), "FakeApi");
assertEquals(codegen.toApiName(""), "DefaultApi");
}
@Test
public void convertApiNameWithSuffix() {
codegen.setApiSuffix("Test");
assertEquals(codegen.toApiName("Fake"), "FakeTest");
assertEquals(codegen.toApiName(""), "DefaultApi");
}
}