Skip to content

Commit a620853

Browse files
authored
[swift5] Refactor encodeToJSON (#10961)
* Add protocol extension method * execute sample script * Add Packing.cmake * Revert "Add Packing.cmake" This reverts commit cb52547. * Remove empty lines * Revert FILES * Add Packing.cmake
1 parent 8e30315 commit a620853

31 files changed

Lines changed: 219 additions & 585 deletions

File tree

modules/openapi-generator/src/main/resources/swift5/Extensions.mustache

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,15 @@ import AnyCodable
1111
import PromiseKit{{/usePromiseKit}}{{#useVapor}}
1212
import Vapor{{/useVapor}}{{^useVapor}}
1313

14-
extension Bool: JSONEncodable {
15-
func encodeToJSON() -> Any { return self }
16-
}
17-
18-
extension Float: JSONEncodable {
19-
func encodeToJSON() -> Any { return self }
20-
}
21-
22-
extension Int: JSONEncodable {
23-
func encodeToJSON() -> Any { return self }
24-
}
25-
26-
extension Int32: JSONEncodable {
27-
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
28-
}
29-
30-
extension Int64: JSONEncodable {
31-
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
32-
}
33-
34-
extension Double: JSONEncodable {
35-
func encodeToJSON() -> Any { return self }
36-
}
37-
38-
extension String: JSONEncodable {
39-
func encodeToJSON() -> Any { return self }
40-
}
14+
extension Bool: JSONEncodable {}
15+
extension Float: JSONEncodable {}
16+
extension Int: JSONEncodable {}
17+
extension Int32: JSONEncodable {}
18+
extension Int64: JSONEncodable {}
19+
extension Double: JSONEncodable {}
20+
extension String: JSONEncodable {}
21+
extension URL: JSONEncodable {}
22+
extension UUID: JSONEncodable {}
4123

4224
extension RawRepresentable where RawValue: JSONEncodable {
4325
func encodeToJSON() -> Any { return self.rawValue }
@@ -83,18 +65,6 @@ extension Date: JSONEncodable {
8365
func encodeToJSON() -> Any {
8466
return CodableHelper.dateFormatter.string(from: self)
8567
}
86-
}
87-
88-
extension URL: JSONEncodable {
89-
func encodeToJSON() -> Any {
90-
return self
91-
}
92-
}
93-
94-
extension UUID: JSONEncodable {
95-
func encodeToJSON() -> Any {
96-
return self.uuidString
97-
}
9868
}{{/useVapor}}{{#generateModelAdditionalProperties}}
9969

10070
extension String: CodingKey {

modules/openapi-generator/src/main/resources/swift5/Models.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ protocol JSONEncodable {
1010
func encodeToJSON() -> Any
1111
}
1212

13+
extension JSONEncodable {
14+
func encodeToJSON() -> Any { self }
15+
}
16+
1317
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum ErrorResponse: Error {
1418
case error(Int, Data?, URLResponse?, Error)
1519
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
set(CPACK_PACKAGE_NAME lib${pkgName})
2+
3+
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
4+
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
5+
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
6+
7+
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${PROJECT_PACKAGE_DESCRIPTION_SUMMARY})
8+
set(CPACK_PACKAGE_VENDOR ${PROJECT_PACKAGE_VENDOR})
9+
set(CPACK_PACKAGE_CONTACT ${PROJECT_PACKAGE_CONTACT})
10+
set(CPACK_DEBIAN_PACKAGE_MAINTAINER ${PROJECT_PACKAGE_MAINTAINER})
11+
12+
set(CPACK_VERBATIM_VARIABLES YES)
13+
14+
set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME})
15+
16+
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
17+
18+
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
19+
20+
set(CPACK_DEB_COMPONENT_INSTALL YES)
21+
22+
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS TRUE)
23+
24+
include(CPack)

samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/Extensions.swift

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,15 @@ import Foundation
99
import AnyCodable
1010
#endif
1111

12-
extension Bool: JSONEncodable {
13-
func encodeToJSON() -> Any { return self }
14-
}
15-
16-
extension Float: JSONEncodable {
17-
func encodeToJSON() -> Any { return self }
18-
}
19-
20-
extension Int: JSONEncodable {
21-
func encodeToJSON() -> Any { return self }
22-
}
23-
24-
extension Int32: JSONEncodable {
25-
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
26-
}
27-
28-
extension Int64: JSONEncodable {
29-
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
30-
}
31-
32-
extension Double: JSONEncodable {
33-
func encodeToJSON() -> Any { return self }
34-
}
35-
36-
extension String: JSONEncodable {
37-
func encodeToJSON() -> Any { return self }
38-
}
12+
extension Bool: JSONEncodable {}
13+
extension Float: JSONEncodable {}
14+
extension Int: JSONEncodable {}
15+
extension Int32: JSONEncodable {}
16+
extension Int64: JSONEncodable {}
17+
extension Double: JSONEncodable {}
18+
extension String: JSONEncodable {}
19+
extension URL: JSONEncodable {}
20+
extension UUID: JSONEncodable {}
3921

4022
extension RawRepresentable where RawValue: JSONEncodable {
4123
func encodeToJSON() -> Any { return self.rawValue }
@@ -83,18 +65,6 @@ extension Date: JSONEncodable {
8365
}
8466
}
8567

86-
extension URL: JSONEncodable {
87-
func encodeToJSON() -> Any {
88-
return self
89-
}
90-
}
91-
92-
extension UUID: JSONEncodable {
93-
func encodeToJSON() -> Any {
94-
return self.uuidString
95-
}
96-
}
97-
9868
extension String: CodingKey {
9969

10070
public var stringValue: String {

samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ protocol JSONEncodable {
1010
func encodeToJSON() -> Any
1111
}
1212

13+
extension JSONEncodable {
14+
func encodeToJSON() -> Any { self }
15+
}
16+
1317
public enum ErrorResponse: Error {
1418
case error(Int, Data?, URLResponse?, Error)
1519
}

samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Extensions.swift

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,15 @@ import Foundation
99
import AnyCodable
1010
#endif
1111

12-
extension Bool: JSONEncodable {
13-
func encodeToJSON() -> Any { return self }
14-
}
15-
16-
extension Float: JSONEncodable {
17-
func encodeToJSON() -> Any { return self }
18-
}
19-
20-
extension Int: JSONEncodable {
21-
func encodeToJSON() -> Any { return self }
22-
}
23-
24-
extension Int32: JSONEncodable {
25-
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
26-
}
27-
28-
extension Int64: JSONEncodable {
29-
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
30-
}
31-
32-
extension Double: JSONEncodable {
33-
func encodeToJSON() -> Any { return self }
34-
}
35-
36-
extension String: JSONEncodable {
37-
func encodeToJSON() -> Any { return self }
38-
}
12+
extension Bool: JSONEncodable {}
13+
extension Float: JSONEncodable {}
14+
extension Int: JSONEncodable {}
15+
extension Int32: JSONEncodable {}
16+
extension Int64: JSONEncodable {}
17+
extension Double: JSONEncodable {}
18+
extension String: JSONEncodable {}
19+
extension URL: JSONEncodable {}
20+
extension UUID: JSONEncodable {}
3921

4022
extension RawRepresentable where RawValue: JSONEncodable {
4123
func encodeToJSON() -> Any { return self.rawValue }
@@ -83,18 +65,6 @@ extension Date: JSONEncodable {
8365
}
8466
}
8567

86-
extension URL: JSONEncodable {
87-
func encodeToJSON() -> Any {
88-
return self
89-
}
90-
}
91-
92-
extension UUID: JSONEncodable {
93-
func encodeToJSON() -> Any {
94-
return self.uuidString
95-
}
96-
}
97-
9868
extension String: CodingKey {
9969

10070
public var stringValue: String {

samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ protocol JSONEncodable {
1010
func encodeToJSON() -> Any
1111
}
1212

13+
extension JSONEncodable {
14+
func encodeToJSON() -> Any { self }
15+
}
16+
1317
public enum ErrorResponse: Error {
1418
case error(Int, Data?, URLResponse?, Error)
1519
}

samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Extensions.swift

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,15 @@ import Foundation
99
import AnyCodable
1010
#endif
1111

12-
extension Bool: JSONEncodable {
13-
func encodeToJSON() -> Any { return self }
14-
}
15-
16-
extension Float: JSONEncodable {
17-
func encodeToJSON() -> Any { return self }
18-
}
19-
20-
extension Int: JSONEncodable {
21-
func encodeToJSON() -> Any { return self }
22-
}
23-
24-
extension Int32: JSONEncodable {
25-
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
26-
}
27-
28-
extension Int64: JSONEncodable {
29-
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
30-
}
31-
32-
extension Double: JSONEncodable {
33-
func encodeToJSON() -> Any { return self }
34-
}
35-
36-
extension String: JSONEncodable {
37-
func encodeToJSON() -> Any { return self }
38-
}
12+
extension Bool: JSONEncodable {}
13+
extension Float: JSONEncodable {}
14+
extension Int: JSONEncodable {}
15+
extension Int32: JSONEncodable {}
16+
extension Int64: JSONEncodable {}
17+
extension Double: JSONEncodable {}
18+
extension String: JSONEncodable {}
19+
extension URL: JSONEncodable {}
20+
extension UUID: JSONEncodable {}
3921

4022
extension RawRepresentable where RawValue: JSONEncodable {
4123
func encodeToJSON() -> Any { return self.rawValue }
@@ -83,18 +65,6 @@ extension Date: JSONEncodable {
8365
}
8466
}
8567

86-
extension URL: JSONEncodable {
87-
func encodeToJSON() -> Any {
88-
return self
89-
}
90-
}
91-
92-
extension UUID: JSONEncodable {
93-
func encodeToJSON() -> Any {
94-
return self.uuidString
95-
}
96-
}
97-
9868
extension String: CodingKey {
9969

10070
public var stringValue: String {

samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Models.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ protocol JSONEncodable {
1010
func encodeToJSON() -> Any
1111
}
1212

13+
extension JSONEncodable {
14+
func encodeToJSON() -> Any { self }
15+
}
16+
1317
public enum ErrorResponse: Error {
1418
case error(Int, Data?, URLResponse?, Error)
1519
}

samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Extensions.swift

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,15 @@ import Foundation
99
import AnyCodable
1010
#endif
1111

12-
extension Bool: JSONEncodable {
13-
func encodeToJSON() -> Any { return self }
14-
}
15-
16-
extension Float: JSONEncodable {
17-
func encodeToJSON() -> Any { return self }
18-
}
19-
20-
extension Int: JSONEncodable {
21-
func encodeToJSON() -> Any { return self }
22-
}
23-
24-
extension Int32: JSONEncodable {
25-
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
26-
}
27-
28-
extension Int64: JSONEncodable {
29-
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
30-
}
31-
32-
extension Double: JSONEncodable {
33-
func encodeToJSON() -> Any { return self }
34-
}
35-
36-
extension String: JSONEncodable {
37-
func encodeToJSON() -> Any { return self }
38-
}
12+
extension Bool: JSONEncodable {}
13+
extension Float: JSONEncodable {}
14+
extension Int: JSONEncodable {}
15+
extension Int32: JSONEncodable {}
16+
extension Int64: JSONEncodable {}
17+
extension Double: JSONEncodable {}
18+
extension String: JSONEncodable {}
19+
extension URL: JSONEncodable {}
20+
extension UUID: JSONEncodable {}
3921

4022
extension RawRepresentable where RawValue: JSONEncodable {
4123
func encodeToJSON() -> Any { return self.rawValue }
@@ -83,18 +65,6 @@ extension Date: JSONEncodable {
8365
}
8466
}
8567

86-
extension URL: JSONEncodable {
87-
func encodeToJSON() -> Any {
88-
return self
89-
}
90-
}
91-
92-
extension UUID: JSONEncodable {
93-
func encodeToJSON() -> Any {
94-
return self.uuidString
95-
}
96-
}
97-
9868
extension String: CodingKey {
9969

10070
public var stringValue: String {

0 commit comments

Comments
 (0)