[swift] add option for API prefix (#5567)

* [swift] add option for API prefix

* [swift] update docs
This commit is contained in:
Bruno Coelho
2020-03-13 14:08:51 +00:00
committed by GitHub
parent 86159cba49
commit fe02dfe196
5 changed files with 22 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ sidebar_label: swift5
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
|apiNamePrefix|Prefix that will be appended to all API names ('tags'). Default: empty string. e.g. Pet => Pet.| |null|
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|lenientTypeCast|Accept and cast values for simple types (string->bool, string->int, int->string)| |false|

View File

@@ -218,6 +218,9 @@ public class CodegenConstants {
// Codegen constants should define a description and provide proper input validation for the value of serializationLibrary
public static final String SERIALIZATION_LIBRARY = "serializationLibrary";
public static final String API_NAME_PREFIX = "apiNamePrefix";
public static final String API_NAME_PREFIX_DESC = "Prefix that will be appended to all API names ('tags'). Default: empty string. e.g. Pet => Pet.";
public static final String API_NAME_SUFFIX = "apiNameSuffix";
public static final String API_NAME_SUFFIX_DESC = "Suffix that will be appended to all API names ('tags'). Default: Api. e.g. Pet => PetApi. Note: Only ruby, python, jaxrs generators suppport this feature at the moment.";

View File

@@ -150,7 +150,7 @@ public class DefaultCodegen implements CodegenConfig {
protected Map<String, String> importMapping = new HashMap<String, String>();
protected String modelPackage = "", apiPackage = "", fileSuffix;
protected String modelNamePrefix = "", modelNameSuffix = "";
protected String apiNameSuffix = "Api";
protected String apiNamePrefix = "", apiNameSuffix = "Api";
protected String testPackage = "";
/*
apiTemplateFiles are for API outputs only (controllers/handlers).
@@ -268,6 +268,10 @@ public class DefaultCodegen implements CodegenConfig {
.get(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS).toString()));
}
if (additionalProperties.containsKey(CodegenConstants.API_NAME_PREFIX)) {
this.setApiNamePrefix((String) additionalProperties.get(CodegenConstants.API_NAME_PREFIX));
}
if (additionalProperties.containsKey(CodegenConstants.API_NAME_SUFFIX)) {
this.setApiNameSuffix((String) additionalProperties.get(CodegenConstants.API_NAME_SUFFIX));
}
@@ -1042,6 +1046,14 @@ public class DefaultCodegen implements CodegenConfig {
this.apiNameSuffix = apiNameSuffix;
}
public String getApiNamePrefix() {
return apiNamePrefix;
}
public void setApiNamePrefix(String apiNamePrefix) {
this.apiNamePrefix = apiNamePrefix;
}
public void setApiPackage(String apiPackage) {
this.apiPackage = apiPackage;
}
@@ -2018,7 +2030,7 @@ public class DefaultCodegen implements CodegenConfig {
if (name.length() == 0) {
return "DefaultApi";
}
return camelize(name + "_" + apiNameSuffix);
return camelize(apiNamePrefix + "_" + name + "_" + apiNameSuffix);
}
/**

View File

@@ -248,6 +248,8 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
+ "string->int, int->string)")
.defaultValue(Boolean.FALSE.toString()));
cliOptions.add(new CliOption(CodegenConstants.API_NAME_PREFIX, CodegenConstants.API_NAME_PREFIX_DESC));
supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession");
supportedLibraries.put(LIBRARY_ALAMOFIRE, "HTTP client: Alamofire");
@@ -636,7 +638,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
if (name.length() == 0) {
return "DefaultAPI";
}
return camelize(name) + "API";
return camelize(apiNamePrefix + "_" + name) + "API";
}
@Override

View File

@@ -78,6 +78,7 @@ public class Swift5OptionsProvider implements OptionsProvider {
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.put(CodegenConstants.API_NAME_PREFIX, "")
.put(CodegenConstants.LIBRARY, LIBRARY_VALUE)
.build();
}