forked from loafle/openapi-generator-original
[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:
parent
f0346aeeb2
commit
7eb2be9c99
@ -9,6 +9,7 @@ sidebar_label: kotlin-server
|
|||||||
| ------ | ----------- | ------ | ------- |
|
| ------ | ----------- | ------ | ------- |
|
||||||
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
||||||
|packageName|Generated artifact package name.| |org.openapitools|
|
|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|
|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|
||||||
|artifactId|Generated artifact id (name of jar).| |null|
|
|artifactId|Generated artifact id (name of jar).| |null|
|
||||||
|artifactVersion|Generated artifact's package version.| |1.0.0|
|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|
||||||
|
@ -9,6 +9,7 @@ sidebar_label: kotlin-spring
|
|||||||
| ------ | ----------- | ------ | ------- |
|
| ------ | ----------- | ------ | ------- |
|
||||||
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
||||||
|packageName|Generated artifact package name.| |org.openapitools|
|
|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|
|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|
||||||
|artifactId|Generated artifact id (name of jar).| |null|
|
|artifactId|Generated artifact id (name of jar).| |null|
|
||||||
|artifactVersion|Generated artifact's package version.| |1.0.0|
|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|
||||||
|
@ -9,6 +9,7 @@ sidebar_label: kotlin
|
|||||||
| ------ | ----------- | ------ | ------- |
|
| ------ | ----------- | ------ | ------- |
|
||||||
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
|sourceFolder|source folder for generated code| |src/main/kotlin|
|
||||||
|packageName|Generated artifact package name.| |org.openapitools|
|
|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|
|
|groupId|Generated artifact package's organization (i.e. maven groupId).| |org.openapitools|
|
||||||
|artifactId|Generated artifact id (name of jar).| |null|
|
|artifactId|Generated artifact id (name of jar).| |null|
|
||||||
|artifactVersion|Generated artifact's package version.| |1.0.0|
|
|artifactVersion|Generated artifact's package version.| |1.0.0|
|
||||||
|
@ -37,6 +37,9 @@ public class CodegenConstants {
|
|||||||
public static final String API_PACKAGE = "apiPackage";
|
public static final String API_PACKAGE = "apiPackage";
|
||||||
public static final String API_PACKAGE_DESC = "package for generated api classes";
|
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 = "modelPackage";
|
||||||
public static final String MODEL_PACKAGE_DESC = "package for generated models";
|
public static final String MODEL_PACKAGE_DESC = "package for generated models";
|
||||||
|
|
||||||
|
@ -584,14 +584,6 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
return toModelName(name) + "Test";
|
return toModelName(name) + "Test";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toApiName(String name) {
|
|
||||||
if (name.length() == 0) {
|
|
||||||
return "DefaultApi";
|
|
||||||
}
|
|
||||||
return camelize(name) + "Api";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toApiFilename(String name) {
|
public String toApiFilename(String name) {
|
||||||
return toApiName(name);
|
return toApiName(name);
|
||||||
|
@ -41,6 +41,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
protected String artifactVersion = "1.0.0";
|
protected String artifactVersion = "1.0.0";
|
||||||
protected String groupId = "org.openapitools";
|
protected String groupId = "org.openapitools";
|
||||||
protected String packageName = "org.openapitools";
|
protected String packageName = "org.openapitools";
|
||||||
|
protected String apiSuffix = "Api";
|
||||||
|
|
||||||
protected String sourceFolder = "src/main/kotlin";
|
protected String sourceFolder = "src/main/kotlin";
|
||||||
|
|
||||||
@ -195,6 +196,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
cliOptions.clear();
|
cliOptions.clear();
|
||||||
addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder);
|
addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder);
|
||||||
addOption(CodegenConstants.PACKAGE_NAME, "Generated artifact package name.", packageName);
|
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.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_ID, "Generated artifact id (name of jar).", artifactId);
|
||||||
addOption(CodegenConstants.ARTIFACT_VERSION, "Generated artifact's package version.", artifactVersion);
|
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);
|
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)) {
|
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_ID)) {
|
||||||
this.setArtifactId((String) additionalProperties.get(CodegenConstants.ARTIFACT_ID));
|
this.setArtifactId((String) additionalProperties.get(CodegenConstants.ARTIFACT_ID));
|
||||||
} else {
|
} else {
|
||||||
@ -396,6 +402,10 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
this.packageName = packageName;
|
this.packageName = packageName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setApiSuffix(String apiSuffix) {
|
||||||
|
this.apiSuffix = apiSuffix;
|
||||||
|
}
|
||||||
|
|
||||||
public void setSourceFolder(String sourceFolder) {
|
public void setSourceFolder(String sourceFolder) {
|
||||||
this.sourceFolder = sourceFolder;
|
this.sourceFolder = sourceFolder;
|
||||||
}
|
}
|
||||||
@ -463,6 +473,14 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
|||||||
return super.toInstantiationType(p);
|
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
|
* Return the fully-qualified "Model" name for import
|
||||||
*
|
*
|
||||||
|
@ -123,4 +123,17 @@ public class AbstractKotlinCodegenTest {
|
|||||||
assertEquals(codegen.toVarName("USER123NAME"), "USER123NAME");
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user