Skip to content

Commit

Permalink
[Swift5] small improvements to Objc compatibility (OpenAPITools#5410)
Browse files Browse the repository at this point in the history
* [swift] make some small improvements

* [swift][client] revert model to use allVars

* PostProcessModelProperty with allVars

* PostProcessModelProperty with vars

* [swift] improve objc interoperability

* [swift] fix swift4 for CI to pass

* [swift] improve objc interoperability

* [swift] improve objc interoperability

* Swift - try to fix build

* [swift] remove pods from git
  • Loading branch information
4brunu authored and MikailBag committed Mar 23, 2020
1 parent c40c05f commit 8e8b240
Show file tree
Hide file tree
Showing 236 changed files with 679 additions and 17,596 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,11 @@ public CodegenModel fromModel(String name, Schema schema) {
postProcessModelProperty(m, prop);
}
}
if (m.allVars != null) {
for (CodegenProperty prop : m.allVars) {
postProcessModelProperty(m, prop);
}
}

if (sortModelPropertiesByRequiredFlag) {
Collections.sort(m.vars, new Comparator<CodegenProperty>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public Swift4Codegen() {
Arrays.asList(
// Added for Objective-C compatibility
"id", "description", "NSArray", "NSURL", "CGFloat", "NSSet", "NSString", "NSInteger", "NSUInteger",
"NSError", "NSDictionary"
"NSError", "NSDictionary",
// Cannot override with a stored property 'className'
"className"
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ public Swift5ClientCodegen() {
Arrays.asList(
// Added for Objective-C compatibility
"id", "description", "NSArray", "NSURL", "CGFloat", "NSSet", "NSString", "NSInteger", "NSUInteger",
"NSError", "NSDictionary"
"NSError", "NSDictionary",
// Cannot override with a stored property 'className'
"className"
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension {{projectName}}API {

{{#description}}
/** {{description}} */{{/description}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{classname}} {
{{#objcCompatible}}@objc {{/objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{classname}}{{#objcCompatible}} : NSObject{{/objcCompatible}} {
{{#operation}}
{{#allParams}}
{{#isEnum}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct {{classname}}: Codable {
{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct {{classname}}: Codable { {{/objcCompatible}}
{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable { {{/objcCompatible}}

{{#allVars}}
{{#isEnum}}
Expand All @@ -15,7 +16,7 @@
{{/description}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{name}}: {{{datatype}}}{{#unwrapRequired}}?{{/unwrapRequired}}{{^unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#objcCompatible}}{{#vendorExtensions.x-swift-optional-scalar}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{name}}Num: NSNumber? {
get {
return {{name}}.map({ return NSNumber(value: $0) })
return {{name}} as NSNumber?
}
}{{/vendorExtensions.x-swift-optional-scalar}}{{/objcCompatible}}
{{/isEnum}}
Expand Down Expand Up @@ -58,7 +59,7 @@

// Decodable protocol methods

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(from decoder: Decoder) throws {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}}{{#objcCompatible}} required{{/objcCompatible}} init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)
{{#allVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension {{projectName}}API {

{{#description}}
/** {{description}} */{{/description}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{classname}} {
{{#objcCompatible}}@objc {{/objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{classname}}{{#objcCompatible}} : NSObject{{/objcCompatible}} {
{{#operation}}
{{#allParams}}
{{#isEnum}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct {{classname}}: Codable {
{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct {{classname}}: Codable { {{/objcCompatible}}
{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable { {{/objcCompatible}}

{{#allVars}}
{{#isEnum}}
Expand All @@ -17,7 +18,7 @@
{{#vendorExtensions.x-swift-optional-scalar}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{name}}Num: NSNumber? {
get {
return {{name}}.map({ return NSNumber(value: $0) })
return {{name}} as NSNumber?
}
}
{{/vendorExtensions.x-swift-optional-scalar}}
Expand Down Expand Up @@ -62,7 +63,7 @@

// Decodable protocol methods

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(from decoder: Decoder) throws {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}}{{#objcCompatible}} required{{/objcCompatible}} init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)
{{#allVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
TargetAttributes = {
};
};
buildConfigurationList = ECAB17FF35111B5E14DAAC08 /* Build configuration list for PBXProject "PetstoreClient" */;
compatibilityVersion = "Xcode 10.0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct APIHelper {

public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
return source.reduce(into: [String: String]()) { (result, item) in
if let collection = item.value as? Array<Any?> {
if let collection = item.value as? [Any?] {
result[item.key] = collection.filter({ $0 != nil }).map { "\($0!)" }.joined(separator: ",")
} else if let value: Any = item.value {
result[item.key] = "\(value)"
Expand All @@ -46,15 +46,15 @@ public struct APIHelper {
}

public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? Array<Any?> {
if let collection = source as? [Any?] {
return collection.filter({ $0 != nil }).map({"\($0!)"}).joined(separator: ",")
}
return source
}

public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
if let collection = item.value as? Array<Any?> {
if let collection = item.value as? [Any?] {
let value = collection.filter({ $0 != nil }).map({"\($0!)"}).joined(separator: ",")
result.append(URLQueryItem(name: item.key, value: value))
} else if let value = item.value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ open class FakeAPI {
}

/**
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트

- parameter number: (form) None
- parameter double: (form) None
Expand Down Expand Up @@ -255,9 +255,9 @@ open class FakeAPI {
}

/**
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
- POST /fake
- Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
- Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
- BASIC:
- type: http
- name: http_basic_test
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/swift4/default/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Class | Method | HTTP request | Description
*FakeAPI* | [**testBodyWithFileSchema**](docs/FakeAPI.md#testbodywithfileschema) | **PUT** /fake/body-with-file-schema |
*FakeAPI* | [**testBodyWithQueryParams**](docs/FakeAPI.md#testbodywithqueryparams) | **PUT** /fake/body-with-query-params |
*FakeAPI* | [**testClientModel**](docs/FakeAPI.md#testclientmodel) | **PATCH** /fake | To test \&quot;client\&quot; model
*FakeAPI* | [**testEndpointParameters**](docs/FakeAPI.md#testendpointparameters) | **POST** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
*FakeAPI* | [**testEndpointParameters**](docs/FakeAPI.md#testendpointparameters) | **POST** /fake | Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트
*FakeAPI* | [**testEnumParameters**](docs/FakeAPI.md#testenumparameters) | **GET** /fake | To test enum parameters
*FakeAPI* | [**testGroupParameters**](docs/FakeAPI.md#testgroupparameters) | **DELETE** /fake | Fake endpoint to test group parameters (optional)
*FakeAPI* | [**testInlineAdditionalProperties**](docs/FakeAPI.md#testinlineadditionalproperties) | **POST** /fake/inline-additionalProperties | test inline additionalProperties
Expand Down
Loading

0 comments on commit 8e8b240

Please sign in to comment.