Skip to content

Commit 72843ad

Browse files
committed
Merge pull request #2244 from hideya/feature/2120
Fix #2120, #2157 - Update Swift API method generation for ease of use
2 parents d619b5e + 9ee154c commit 72843ad

File tree

9 files changed

+1391
-620
lines changed

9 files changed

+1391
-620
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwiftCodegen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
3737
public static final String POD_DESCRIPTION = "podDescription";
3838
public static final String POD_SCREENSHOTS = "podScreenshots";
3939
public static final String POD_DOCUMENTATION_URL = "podDocumentationURL";
40+
public static final String SWIFT_USE_API_NAMESPACE = "swiftUseApiNamespace";
4041
protected static final String LIBRARY_PROMISE_KIT = "PromiseKit";
4142
protected static final String[] RESPONSE_LIBRARIES = { LIBRARY_PROMISE_KIT };
4243
protected String projectName = "SwaggerClient";
4344
protected boolean unwrapRequired;
45+
protected boolean swiftUseApiNamespace;
4446
protected String[] responseAs = new String[0];
4547
protected String sourceFolder = "Classes" + File.separator + "Swaggers";
4648
private static final Pattern PATH_PARAM_PATTERN = Pattern.compile("\\{[a-zA-Z_]+\\}");
@@ -143,6 +145,7 @@ public SwiftCodegen() {
143145
cliOptions.add(new CliOption(POD_DESCRIPTION, "Description used for Podspec"));
144146
cliOptions.add(new CliOption(POD_SCREENSHOTS, "Screenshots used for Podspec"));
145147
cliOptions.add(new CliOption(POD_DOCUMENTATION_URL, "Documentation URL used for Podspec"));
148+
cliOptions.add(new CliOption(SWIFT_USE_API_NAMESPACE, "Flag to make all the API classes inner-class of {{projectName}}API"));
146149
}
147150

148151
@Override
@@ -177,6 +180,12 @@ public void processOpts() {
177180
additionalProperties.put("usePromiseKit", true);
178181
}
179182

183+
// Setup swiftUseApiNamespace option, which makes all the API classes inner-class of {{projectName}}API
184+
if (additionalProperties.containsKey(SWIFT_USE_API_NAMESPACE)) {
185+
swiftUseApiNamespace = Boolean.parseBoolean(String.valueOf(additionalProperties.get(SWIFT_USE_API_NAMESPACE)));
186+
}
187+
additionalProperties.put(SWIFT_USE_API_NAMESPACE, swiftUseApiNamespace);
188+
180189
supportingFiles.add(new SupportingFile("Podspec.mustache", "", projectName + ".podspec"));
181190
supportingFiles.add(new SupportingFile("Cartfile.mustache", "", "Cartfile"));
182191
supportingFiles.add(new SupportingFile("APIHelper.mustache", sourceFolder, "APIHelper.swift"));

modules/swagger-codegen/src/main/resources/swift/api.mustache

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,88 @@
88
import Alamofire{{#usePromiseKit}}
99
import PromiseKit{{/usePromiseKit}}
1010

11+
{{#swiftUseApiNamespace}}
1112
extension {{projectName}}API {
12-
{{#description}}
13-
/** {{description}} */{{/description}}
14-
public class {{classname}}: APIBase {
15-
{{#operation}}
16-
/**
17-
{{#summary}}
18-
{{{summary}}}
19-
{{/summary}}
20-
- {{httpMethod}} {{path}}{{#notes}}
21-
- {{{notes}}}{{/notes}}{{#subresourceOperation}}
22-
- subresourceOperation: {{subresourceOperation}}{{/subresourceOperation}}{{#defaultResponse}}
23-
- defaultResponse: {{defaultResponse}}{{/defaultResponse}}{{#authMethods}}
24-
- {{#isBasic}}BASIC{{/isBasic}}{{#isOAuth}}OAuth{{/isOAuth}}{{#isApiKey}}API Key{{/isApiKey}}:
25-
- type: {{type}}{{#keyParamName}} {{keyParamName}} {{#isKeyInQuery}}(QUERY){{/isKeyInQuery}}{{#isKeyInHeaer}}(HEADER){{/isKeyInHeaer}}{{/keyParamName}}
26-
- name: {{name}}{{/authMethods}}{{#responseHeaders}}
27-
- responseHeaders: {{responseHeaders}}{{/responseHeaders}}{{#examples}}
28-
- examples: {{{examples}}}{{/examples}}{{#externalDocs}}
29-
- externalDocs: {{externalDocs}}{{/externalDocs}}{{#hasParams}}
30-
{{/hasParams}}{{#allParams}}
31-
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
32-
33-
- returns: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{description}}
34-
*/
35-
public class func {{operationId}}({{#allParams}}{{^secondaryParam}}{{paramName}} {{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
36-
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
37-
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
38-
let URLString = {{projectName}}API.basePath + path
39-
{{#bodyParam}}
40-
let parameters = {{paramName}}{{^required}}?{{/required}}.encodeToJSON() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}}
41-
let nillableParameters: [String:AnyObject?] = {{^queryParams}}{{^formParams}}[:]{{/formParams}}{{#formParams}}{{^secondaryParam}}[{{/secondaryParam}}
42-
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
43-
]{{/hasMore}}{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}}
44-
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
45-
]{{/hasMore}}{{/queryParams}}
46-
let parameters = APIHelper.rejectNil(nillableParameters){{/bodyParam}}
47-
48-
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder()
49-
50-
return requestBuilder.init(method: "{{httpMethod}}", URLString: URLString, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}})
13+
{{/swiftUseApiNamespace}}
14+
15+
{{#description}}
16+
/** {{description}} */{{/description}}
17+
public class {{classname}}: APIBase {
18+
{{#operation}}
19+
/**
20+
{{#summary}}
21+
{{{summary}}}
22+
{{/summary}}{{#allParams}}
23+
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
24+
- parameter completion: completion handler to receive the data and the error objects
25+
*/
26+
public class func {{operationId}}({{#allParams}}{{^secondaryParam}}{{paramName}} {{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}completion: (({{#returnType}}data: {{{returnType}}}?, {{/returnType}}error: ErrorType?) -> Void)) {
27+
{{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).execute { (response, error) -> Void in
28+
completion({{#returnType}}data: response?.body, {{/returnType}}error: error);
29+
}
30+
}
31+
32+
{{#usePromiseKit}}
33+
/**
34+
{{#summary}}
35+
{{{summary}}}
36+
{{/summary}}{{#allParams}}
37+
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
38+
- returns: Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>
39+
*/
40+
public class func {{operationId}}({{#allParams}}{{^secondaryParam}}{{paramName}} {{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
41+
let deferred = Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.pendingPromise()
42+
{{operationId}}({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { {{#returnType}}data, {{/returnType}}error in
43+
if let error = error {
44+
deferred.reject(error)
45+
} else {
46+
deferred.fulfill({{#returnType}}data!{{/returnType}})
47+
}
5148
}
52-
{{/operation}}
49+
return deferred.promise
5350
}
51+
{{/usePromiseKit}}
52+
53+
/**
54+
{{#summary}}
55+
{{{summary}}}
56+
{{/summary}}
57+
- {{httpMethod}} {{path}}{{#notes}}
58+
- {{{notes}}}{{/notes}}{{#subresourceOperation}}
59+
- subresourceOperation: {{subresourceOperation}}{{/subresourceOperation}}{{#defaultResponse}}
60+
- defaultResponse: {{defaultResponse}}{{/defaultResponse}}{{#authMethods}}
61+
- {{#isBasic}}BASIC{{/isBasic}}{{#isOAuth}}OAuth{{/isOAuth}}{{#isApiKey}}API Key{{/isApiKey}}:
62+
- type: {{type}}{{#keyParamName}} {{keyParamName}} {{#isKeyInQuery}}(QUERY){{/isKeyInQuery}}{{#isKeyInHeaer}}(HEADER){{/isKeyInHeaer}}{{/keyParamName}}
63+
- name: {{name}}{{/authMethods}}{{#responseHeaders}}
64+
- responseHeaders: {{responseHeaders}}{{/responseHeaders}}{{#examples}}
65+
- examples: {{{examples}}}{{/examples}}{{#externalDocs}}
66+
- externalDocs: {{externalDocs}}{{/externalDocs}}{{#hasParams}}
67+
{{/hasParams}}{{#allParams}}
68+
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
69+
70+
- returns: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{description}}
71+
*/
72+
public class func {{operationId}}WithRequestBuilder({{#allParams}}{{^secondaryParam}}{{paramName}} {{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
73+
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
74+
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
75+
let URLString = {{projectName}}API.basePath + path
76+
{{#bodyParam}}
77+
let parameters = {{paramName}}{{^required}}?{{/required}}.encodeToJSON() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}}
78+
let nillableParameters: [String:AnyObject?] = {{^queryParams}}{{^formParams}}[:]{{/formParams}}{{#formParams}}{{^secondaryParam}}[{{/secondaryParam}}
79+
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
80+
]{{/hasMore}}{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}}
81+
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
82+
]{{/hasMore}}{{/queryParams}}
83+
let parameters = APIHelper.rejectNil(nillableParameters){{/bodyParam}}
84+
85+
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder()
86+
87+
return requestBuilder.init(method: "{{httpMethod}}", URLString: URLString, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}})
88+
}
89+
90+
{{/operation}}
91+
}
92+
{{#swiftUseApiNamespace}}
5493
}
94+
{{/swiftUseApiNamespace}}
5595
{{/operations}}

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SwiftOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class SwiftOptionsProvider implements OptionsProvider {
2525
public static final String POD_DESCRIPTION_VALUE = "podDescription";
2626
public static final String POD_SCREENSHOTS_VALUE = "podScreenshots";
2727
public static final String POD_DOCUMENTATION_URL_VALUE = "podDocumentationURL";
28+
public static final String SWIFT_USE_API_NAMESPACE_VALUE = "swiftUseApiNamespace";
2829

2930
@Override
3031
public String getLanguage() {
@@ -50,6 +51,7 @@ public Map<String, String> createOptions() {
5051
.put(SwiftCodegen.POD_DESCRIPTION, POD_DESCRIPTION_VALUE)
5152
.put(SwiftCodegen.POD_SCREENSHOTS, POD_SCREENSHOTS_VALUE)
5253
.put(SwiftCodegen.POD_DOCUMENTATION_URL, POD_DOCUMENTATION_URL_VALUE)
54+
.put(SwiftCodegen.SWIFT_USE_API_NAMESPACE, SWIFT_USE_API_NAMESPACE_VALUE)
5355
.build();
5456
}
5557

0 commit comments

Comments
 (0)