Skip to content

Commit 40d5d09

Browse files
Daiki Matsudatewing328
authored andcommitted
[Swift4] accept empty content with default client (#7921)
* [swift4] update mustache to accept empty body * [swift4] update client * [swift4] update client tests
1 parent 35d681b commit 40d5d09

File tree

57 files changed

+2029
-1637
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2029
-1637
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ open class {{classname}} {
3636
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}{{/allParams}}
3737
- parameter completion: completion handler to receive the data and the error objects
3838
*/
39-
open class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}completion: @escaping ((_ {{#returnType}}data: {{{returnType}}}?,_ {{/returnType}}error: Error?) -> Void)) {
39+
open class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}completion: @escaping ((_ data: {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}?,_ error: Error?) -> Void)) {
4040
{{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).execute { (response, error) -> Void in
41-
completion({{#returnType}}response?.body, {{/returnType}}error);
41+
{{#returnType}}
42+
completion(response?.body, error)
43+
{{/returnType}}
44+
{{^returnType}}
45+
if error == nil {
46+
completion((), error)
47+
} else {
48+
completion(nil, error)
49+
}
50+
{{/returnType}}
4251
}
4352
}
4453

@@ -52,11 +61,11 @@ open class {{classname}} {
5261
*/
5362
open class func {{operationId}}({{#allParams}} {{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
5463
let deferred = Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.pending()
55-
{{operationId}}({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { {{#returnType}}data, {{/returnType}}error in
64+
{{operationId}}({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { data, error in
5665
if let error = error {
5766
deferred.reject(error)
5867
} else {
59-
deferred.fulfill({{#returnType}}data!{{/returnType}})
68+
deferred.fulfill(data!)
6069
}
6170
}
6271
return deferred.promise
@@ -72,11 +81,11 @@ open class {{classname}} {
7281
*/
7382
open class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
7483
return Observable.create { observer -> Disposable in
75-
{{operationId}}({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { {{#returnType}}data, {{/returnType}}error in
84+
{{operationId}}({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { data, error in
7685
if let error = error {
7786
observer.on(.error(error))
7887
} else {
79-
observer.on(.next({{#returnType}}data!{{/returnType}}{{^returnType}}(){{/returnType}}))
88+
observer.on(.next(data!))
8089
}
8190
observer.on(.completed)
8291
}

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/AnotherfakeAPI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ open class AnotherFakeAPI {
1919
*/
2020
open class func testSpecialTags(body: Client, completion: @escaping ((_ data: Client?,_ error: Error?) -> Void)) {
2121
testSpecialTagsWithRequestBuilder(body: body).execute { (response, error) -> Void in
22-
completion(response?.body, error);
22+
completion(response?.body, error)
2323
}
2424
}
2525

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/FakeAPI.swift

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ open class FakeAPI {
1818
*/
1919
open class func fakeOuterBooleanSerialize(body: OuterBoolean? = nil, completion: @escaping ((_ data: OuterBoolean?,_ error: Error?) -> Void)) {
2020
fakeOuterBooleanSerializeWithRequestBuilder(body: body).execute { (response, error) -> Void in
21-
completion(response?.body, error);
21+
completion(response?.body, error)
2222
}
2323
}
2424

@@ -51,7 +51,7 @@ open class FakeAPI {
5151
*/
5252
open class func fakeOuterCompositeSerialize(body: OuterComposite? = nil, completion: @escaping ((_ data: OuterComposite?,_ error: Error?) -> Void)) {
5353
fakeOuterCompositeSerializeWithRequestBuilder(body: body).execute { (response, error) -> Void in
54-
completion(response?.body, error);
54+
completion(response?.body, error)
5555
}
5656
}
5757

@@ -88,7 +88,7 @@ open class FakeAPI {
8888
*/
8989
open class func fakeOuterNumberSerialize(body: OuterNumber? = nil, completion: @escaping ((_ data: OuterNumber?,_ error: Error?) -> Void)) {
9090
fakeOuterNumberSerializeWithRequestBuilder(body: body).execute { (response, error) -> Void in
91-
completion(response?.body, error);
91+
completion(response?.body, error)
9292
}
9393
}
9494

@@ -121,7 +121,7 @@ open class FakeAPI {
121121
*/
122122
open class func fakeOuterStringSerialize(body: OuterString? = nil, completion: @escaping ((_ data: OuterString?,_ error: Error?) -> Void)) {
123123
fakeOuterStringSerializeWithRequestBuilder(body: body).execute { (response, error) -> Void in
124-
completion(response?.body, error);
124+
completion(response?.body, error)
125125
}
126126
}
127127

@@ -155,7 +155,7 @@ open class FakeAPI {
155155
*/
156156
open class func testClientModel(body: Client, completion: @escaping ((_ data: Client?,_ error: Error?) -> Void)) {
157157
testClientModelWithRequestBuilder(body: body).execute { (response, error) -> Void in
158-
completion(response?.body, error);
158+
completion(response?.body, error)
159159
}
160160
}
161161

@@ -203,9 +203,13 @@ open class FakeAPI {
203203
- parameter callback: (form) None (optional)
204204
- parameter completion: completion handler to receive the data and the error objects
205205
*/
206-
open class func testEndpointParameters(number: Double, double: Double, patternWithoutDelimiter: String, byte: Data, integer: Int? = nil, int32: Int? = nil, int64: Int64? = nil, float: Float? = nil, string: String? = nil, binary: Data? = nil, date: Date? = nil, dateTime: Date? = nil, password: String? = nil, callback: String? = nil, completion: @escaping ((_ error: Error?) -> Void)) {
206+
open class func testEndpointParameters(number: Double, double: Double, patternWithoutDelimiter: String, byte: Data, integer: Int? = nil, int32: Int? = nil, int64: Int64? = nil, float: Float? = nil, string: String? = nil, binary: Data? = nil, date: Date? = nil, dateTime: Date? = nil, password: String? = nil, callback: String? = nil, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
207207
testEndpointParametersWithRequestBuilder(number: number, double: double, patternWithoutDelimiter: patternWithoutDelimiter, byte: byte, integer: integer, int32: int32, int64: int64, float: float, string: string, binary: binary, date: date, dateTime: dateTime, password: password, callback: callback).execute { (response, error) -> Void in
208-
completion(error);
208+
if error == nil {
209+
completion((), error)
210+
} else {
211+
completion(nil, error)
212+
}
209213
}
210214
}
211215

@@ -345,9 +349,13 @@ open class FakeAPI {
345349
- parameter enumQueryDouble: (form) Query parameter enum test (double) (optional)
346350
- parameter completion: completion handler to receive the data and the error objects
347351
*/
348-
open class func testEnumParameters(enumFormStringArray: [String]? = nil, enumFormString: EnumFormString_testEnumParameters? = nil, enumHeaderStringArray: [String]? = nil, enumHeaderString: EnumHeaderString_testEnumParameters? = nil, enumQueryStringArray: [String]? = nil, enumQueryString: EnumQueryString_testEnumParameters? = nil, enumQueryInteger: EnumQueryInteger_testEnumParameters? = nil, enumQueryDouble: EnumQueryDouble_testEnumParameters? = nil, completion: @escaping ((_ error: Error?) -> Void)) {
352+
open class func testEnumParameters(enumFormStringArray: [String]? = nil, enumFormString: EnumFormString_testEnumParameters? = nil, enumHeaderStringArray: [String]? = nil, enumHeaderString: EnumHeaderString_testEnumParameters? = nil, enumQueryStringArray: [String]? = nil, enumQueryString: EnumQueryString_testEnumParameters? = nil, enumQueryInteger: EnumQueryInteger_testEnumParameters? = nil, enumQueryDouble: EnumQueryDouble_testEnumParameters? = nil, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
349353
testEnumParametersWithRequestBuilder(enumFormStringArray: enumFormStringArray, enumFormString: enumFormString, enumHeaderStringArray: enumHeaderStringArray, enumHeaderString: enumHeaderString, enumQueryStringArray: enumQueryStringArray, enumQueryString: enumQueryString, enumQueryInteger: enumQueryInteger, enumQueryDouble: enumQueryDouble).execute { (response, error) -> Void in
350-
completion(error);
354+
if error == nil {
355+
completion((), error)
356+
} else {
357+
completion(nil, error)
358+
}
351359
}
352360
}
353361

@@ -403,9 +411,13 @@ open class FakeAPI {
403411
- parameter param: (body) request body
404412
- parameter completion: completion handler to receive the data and the error objects
405413
*/
406-
open class func testInlineAdditionalProperties(param: Any, completion: @escaping ((_ error: Error?) -> Void)) {
414+
open class func testInlineAdditionalProperties(param: Any, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
407415
testInlineAdditionalPropertiesWithRequestBuilder(param: param).execute { (response, error) -> Void in
408-
completion(error);
416+
if error == nil {
417+
completion((), error)
418+
} else {
419+
completion(nil, error)
420+
}
409421
}
410422
}
411423

@@ -438,9 +450,13 @@ open class FakeAPI {
438450
- parameter param2: (form) field2
439451
- parameter completion: completion handler to receive the data and the error objects
440452
*/
441-
open class func testJsonFormData(param: String, param2: String, completion: @escaping ((_ error: Error?) -> Void)) {
453+
open class func testJsonFormData(param: String, param2: String, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
442454
testJsonFormDataWithRequestBuilder(param: param, param2: param2).execute { (response, error) -> Void in
443-
completion(error);
455+
if error == nil {
456+
completion((), error)
457+
} else {
458+
completion(nil, error)
459+
}
444460
}
445461
}
446462

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/FakeClassnameTags123API.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ open class FakeClassnameTags123API {
1919
*/
2020
open class func testClassname(body: Client, completion: @escaping ((_ data: Client?,_ error: Error?) -> Void)) {
2121
testClassnameWithRequestBuilder(body: body).execute { (response, error) -> Void in
22-
completion(response?.body, error);
22+
completion(response?.body, error)
2323
}
2424
}
2525

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/PetAPI.swift

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ open class PetAPI {
1717
- parameter body: (body) Pet object that needs to be added to the store
1818
- parameter completion: completion handler to receive the data and the error objects
1919
*/
20-
open class func addPet(body: Pet, completion: @escaping ((_ error: Error?) -> Void)) {
20+
open class func addPet(body: Pet, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
2121
addPetWithRequestBuilder(body: body).execute { (response, error) -> Void in
22-
completion(error);
22+
if error == nil {
23+
completion((), error)
24+
} else {
25+
completion(nil, error)
26+
}
2327
}
2428
}
2529

@@ -55,9 +59,13 @@ open class PetAPI {
5559
- parameter apiKey: (header) (optional)
5660
- parameter completion: completion handler to receive the data and the error objects
5761
*/
58-
open class func deletePet(petId: Int64, apiKey: String? = nil, completion: @escaping ((_ error: Error?) -> Void)) {
62+
open class func deletePet(petId: Int64, apiKey: String? = nil, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
5963
deletePetWithRequestBuilder(petId: petId, apiKey: apiKey).execute { (response, error) -> Void in
60-
completion(error);
64+
if error == nil {
65+
completion((), error)
66+
} else {
67+
completion(nil, error)
68+
}
6169
}
6270
}
6371

@@ -111,7 +119,7 @@ open class PetAPI {
111119
*/
112120
open class func findPetsByStatus(status: [String], completion: @escaping ((_ data: [Pet]?,_ error: Error?) -> Void)) {
113121
findPetsByStatusWithRequestBuilder(status: status).execute { (response, error) -> Void in
114-
completion(response?.body, error);
122+
completion(response?.body, error)
115123
}
116124
}
117125

@@ -235,7 +243,7 @@ open class PetAPI {
235243
*/
236244
open class func findPetsByTags(tags: [String], completion: @escaping ((_ data: [Pet]?,_ error: Error?) -> Void)) {
237245
findPetsByTagsWithRequestBuilder(tags: tags).execute { (response, error) -> Void in
238-
completion(response?.body, error);
246+
completion(response?.body, error)
239247
}
240248
}
241249

@@ -359,7 +367,7 @@ open class PetAPI {
359367
*/
360368
open class func getPetById(petId: Int64, completion: @escaping ((_ data: Pet?,_ error: Error?) -> Void)) {
361369
getPetByIdWithRequestBuilder(petId: petId).execute { (response, error) -> Void in
362-
completion(response?.body, error);
370+
completion(response?.body, error)
363371
}
364372
}
365373

@@ -449,9 +457,13 @@ open class PetAPI {
449457
- parameter body: (body) Pet object that needs to be added to the store
450458
- parameter completion: completion handler to receive the data and the error objects
451459
*/
452-
open class func updatePet(body: Pet, completion: @escaping ((_ error: Error?) -> Void)) {
460+
open class func updatePet(body: Pet, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
453461
updatePetWithRequestBuilder(body: body).execute { (response, error) -> Void in
454-
completion(error);
462+
if error == nil {
463+
completion((), error)
464+
} else {
465+
completion(nil, error)
466+
}
455467
}
456468
}
457469

@@ -488,9 +500,13 @@ open class PetAPI {
488500
- parameter status: (form) Updated status of the pet (optional)
489501
- parameter completion: completion handler to receive the data and the error objects
490502
*/
491-
open class func updatePetWithForm(petId: Int64, name: String? = nil, status: String? = nil, completion: @escaping ((_ error: Error?) -> Void)) {
503+
open class func updatePetWithForm(petId: Int64, name: String? = nil, status: String? = nil, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
492504
updatePetWithFormWithRequestBuilder(petId: petId, name: name, status: status).execute { (response, error) -> Void in
493-
completion(error);
505+
if error == nil {
506+
completion((), error)
507+
} else {
508+
completion(nil, error)
509+
}
494510
}
495511
}
496512

@@ -540,7 +556,7 @@ open class PetAPI {
540556
*/
541557
open class func uploadFile(petId: Int64, additionalMetadata: String? = nil, file: URL? = nil, completion: @escaping ((_ data: ApiResponse?,_ error: Error?) -> Void)) {
542558
uploadFileWithRequestBuilder(petId: petId, additionalMetadata: additionalMetadata, file: file).execute { (response, error) -> Void in
543-
completion(response?.body, error);
559+
completion(response?.body, error)
544560
}
545561
}
546562

samples/client/petstore/swift4/default/PetstoreClient/Classes/Swaggers/APIs/StoreAPI.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ open class StoreAPI {
1717
- parameter orderId: (path) ID of the order that needs to be deleted
1818
- parameter completion: completion handler to receive the data and the error objects
1919
*/
20-
open class func deleteOrder(orderId: String, completion: @escaping ((_ error: Error?) -> Void)) {
20+
open class func deleteOrder(orderId: String, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
2121
deleteOrderWithRequestBuilder(orderId: orderId).execute { (response, error) -> Void in
22-
completion(error);
22+
if error == nil {
23+
completion((), error)
24+
} else {
25+
completion(nil, error)
26+
}
2327
}
2428
}
2529

@@ -55,7 +59,7 @@ open class StoreAPI {
5559
*/
5660
open class func getInventory(completion: @escaping ((_ data: [String:Int]?,_ error: Error?) -> Void)) {
5761
getInventoryWithRequestBuilder().execute { (response, error) -> Void in
58-
completion(response?.body, error);
62+
completion(response?.body, error)
5963
}
6064
}
6165

@@ -93,7 +97,7 @@ open class StoreAPI {
9397
*/
9498
open class func getOrderById(orderId: Int64, completion: @escaping ((_ data: Order?,_ error: Error?) -> Void)) {
9599
getOrderByIdWithRequestBuilder(orderId: orderId).execute { (response, error) -> Void in
96-
completion(response?.body, error);
100+
completion(response?.body, error)
97101
}
98102
}
99103

@@ -160,7 +164,7 @@ open class StoreAPI {
160164
*/
161165
open class func placeOrder(body: Order, completion: @escaping ((_ data: Order?,_ error: Error?) -> Void)) {
162166
placeOrderWithRequestBuilder(body: body).execute { (response, error) -> Void in
163-
completion(response?.body, error);
167+
completion(response?.body, error)
164168
}
165169
}
166170

0 commit comments

Comments
 (0)