Skip to content

Commit fe02dfe

Browse files
authored
[swift] add option for API prefix (#5567)
* [swift] add option for API prefix * [swift] update docs
1 parent 86159cb commit fe02dfe

5 files changed

Lines changed: 22 additions & 3 deletions

File tree

docs/generators/swift5.md

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

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
218218
// Codegen constants should define a description and provide proper input validation for the value of serializationLibrary
219219
public static final String SERIALIZATION_LIBRARY = "serializationLibrary";
220220

221+
public static final String API_NAME_PREFIX = "apiNamePrefix";
222+
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.";
223+
221224
public static final String API_NAME_SUFFIX = "apiNameSuffix";
222225
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.";
223226

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public class DefaultCodegen implements CodegenConfig {
150150
protected Map<String, String> importMapping = new HashMap<String, String>();
151151
protected String modelPackage = "", apiPackage = "", fileSuffix;
152152
protected String modelNamePrefix = "", modelNameSuffix = "";
153-
protected String apiNameSuffix = "Api";
153+
protected String apiNamePrefix = "", apiNameSuffix = "Api";
154154
protected String testPackage = "";
155155
/*
156156
apiTemplateFiles are for API outputs only (controllers/handlers).
@@ -268,6 +268,10 @@ public void processOpts() {
268268
.get(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS).toString()));
269269
}
270270

271+
if (additionalProperties.containsKey(CodegenConstants.API_NAME_PREFIX)) {
272+
this.setApiNamePrefix((String) additionalProperties.get(CodegenConstants.API_NAME_PREFIX));
273+
}
274+
271275
if (additionalProperties.containsKey(CodegenConstants.API_NAME_SUFFIX)) {
272276
this.setApiNameSuffix((String) additionalProperties.get(CodegenConstants.API_NAME_SUFFIX));
273277
}
@@ -1042,6 +1046,14 @@ public void setApiNameSuffix(String apiNameSuffix) {
10421046
this.apiNameSuffix = apiNameSuffix;
10431047
}
10441048

1049+
public String getApiNamePrefix() {
1050+
return apiNamePrefix;
1051+
}
1052+
1053+
public void setApiNamePrefix(String apiNamePrefix) {
1054+
this.apiNamePrefix = apiNamePrefix;
1055+
}
1056+
10451057
public void setApiPackage(String apiPackage) {
10461058
this.apiPackage = apiPackage;
10471059
}
@@ -2018,7 +2030,7 @@ public String toApiName(String name) {
20182030
if (name.length() == 0) {
20192031
return "DefaultApi";
20202032
}
2021-
return camelize(name + "_" + apiNameSuffix);
2033+
return camelize(apiNamePrefix + "_" + name + "_" + apiNameSuffix);
20222034
}
20232035

20242036
/**

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ public Swift5ClientCodegen() {
248248
+ "string->int, int->string)")
249249
.defaultValue(Boolean.FALSE.toString()));
250250

251+
cliOptions.add(new CliOption(CodegenConstants.API_NAME_PREFIX, CodegenConstants.API_NAME_PREFIX_DESC));
252+
251253
supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession");
252254
supportedLibraries.put(LIBRARY_ALAMOFIRE, "HTTP client: Alamofire");
253255

@@ -636,7 +638,7 @@ public String toApiName(String name) {
636638
if (name.length() == 0) {
637639
return "DefaultAPI";
638640
}
639-
return camelize(name) + "API";
641+
return camelize(apiNamePrefix + "_" + name) + "API";
640642
}
641643

642644
@Override

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public Map<String, String> createOptions() {
7878
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
7979
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
8080
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
81+
.put(CodegenConstants.API_NAME_PREFIX, "")
8182
.put(CodegenConstants.LIBRARY, LIBRARY_VALUE)
8283
.build();
8384
}

0 commit comments

Comments
 (0)