Skip to content

Commit ff11895

Browse files
authored
Merge branch 'master' into master
2 parents 2717116 + 0ceba0f commit ff11895

File tree

6 files changed

+206
-5
lines changed

6 files changed

+206
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public Swift5Codegen() {
192192
typeMapping.put("float", "Float");
193193
typeMapping.put("number", "Double");
194194
typeMapping.put("double", "Double");
195-
typeMapping.put("object", "Any");
195+
typeMapping.put("object", "JSONValue");
196196
typeMapping.put("file", "URL");
197197
typeMapping.put("binary", "Data");
198198
typeMapping.put("ByteArray", "Data");
@@ -324,6 +324,9 @@ public void processOpts() {
324324
supportingFiles.add(new SupportingFile("JSONEncodingHelper.mustache",
325325
sourceFolder,
326326
"JSONEncodingHelper.swift"));
327+
supportingFiles.add(new SupportingFile("JSONValue.mustache",
328+
sourceFolder,
329+
"JSONValue.swift"));
327330
supportingFiles.add(new SupportingFile("git_push.sh.mustache",
328331
"",
329332
"git_push.sh"));
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// JSONEncodingHelper.swift
3+
//
4+
// Generated by swagger-codegen
5+
// https://github.com/swagger-api/swagger-codegen
6+
//
7+
8+
import Foundation
9+
10+
11+
public enum JSONValue: Codable, Equatable {
12+
case string(String)
13+
case int(Int)
14+
case double(Double)
15+
case bool(Bool)
16+
case object([String: JSONValue])
17+
case array([JSONValue])
18+
case null
19+
20+
public func encode(to encoder: Encoder) throws {
21+
var container = encoder.singleValueContainer()
22+
switch self {
23+
case .string(let string): try container.encode(string)
24+
case .int(let int): try container.encode(int)
25+
case .double(let double): try container.encode(double)
26+
case .bool(let bool): try container.encode(bool)
27+
case .object(let object): try container.encode(object)
28+
case .array(let array): try container.encode(array)
29+
case .null: try container.encode(Optional<String>.none)
30+
}
31+
}
32+
33+
public init(from decoder: Decoder) throws {
34+
let container = try decoder.singleValueContainer()
35+
self = try ((try? container.decode(String.self)).map(JSONValue.string))
36+
.or((try? container.decode(Int.self)).map(JSONValue.int))
37+
.or((try? container.decode(Double.self)).map(JSONValue.double))
38+
.or((try? container.decode(Bool.self)).map(JSONValue.bool))
39+
.or((try? container.decode([String: JSONValue].self)).map(JSONValue.object))
40+
.or((try? container.decode([JSONValue].self)).map(JSONValue.array))
41+
.or((container.decodeNil() ? .some(JSONValue.null) : .none))
42+
.resolve(
43+
with: DecodingError.typeMismatch(
44+
JSONValue.self,
45+
DecodingError.Context(
46+
codingPath: container.codingPath,
47+
debugDescription: "Not a JSON value"
48+
)
49+
)
50+
)
51+
}
52+
53+
}
54+
55+
extension JSONValue: ExpressibleByStringLiteral {
56+
public init(stringLiteral value: String) {
57+
self = .string(value)
58+
}
59+
}
60+
extension JSONValue: ExpressibleByIntegerLiteral {
61+
public init(integerLiteral value: Int) {
62+
self = .int(value)
63+
}
64+
}
65+
extension JSONValue: ExpressibleByFloatLiteral {
66+
public init(floatLiteral value: Double) {
67+
self = .double(value)
68+
}
69+
}
70+
extension JSONValue: ExpressibleByBooleanLiteral {
71+
public init(booleanLiteral value: Bool) {
72+
self = .bool(value)
73+
}
74+
}
75+
extension JSONValue: ExpressibleByDictionaryLiteral {
76+
public init(dictionaryLiteral elements: (String, JSONValue)...) {
77+
self = .object([String: JSONValue](uniqueKeysWithValues: elements))
78+
}
79+
}
80+
extension JSONValue: ExpressibleByArrayLiteral {
81+
public init(arrayLiteral elements: JSONValue...) {
82+
self = .array(elements)
83+
}
84+
}
85+
86+
fileprivate extension Optional {
87+
func or(_ other: Optional) -> Optional {
88+
switch self {
89+
case .none: return other
90+
case .some: return self
91+
}
92+
}
93+
func resolve(with error: @autoclosure () -> Error) throws -> Wrapped {
94+
switch self {
95+
case .none: throw error()
96+
case .some(let wrapped): return wrapped
97+
}
98+
}
99+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.9-SNAPSHOT
1+
2.4.11-SNAPSHOT
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.9-SNAPSHOT
1+
2.4.11-SNAPSHOT

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ open class FakeAPI {
451451
- parameter param: (body) request body
452452
- parameter completion: completion handler to receive the data and the error objects
453453
*/
454-
open class func testInlineAdditionalProperties(param: Any, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
454+
open class func testInlineAdditionalProperties(param: JSONValue, completion: @escaping ((_ data: Void?,_ error: Error?) -> Void)) {
455455
testInlineAdditionalPropertiesWithRequestBuilder(param: param).execute { (response, error) -> Void in
456456
if error == nil {
457457
completion((), error)
@@ -471,7 +471,7 @@ open class FakeAPI {
471471

472472
- returns: RequestBuilder<Void>
473473
*/
474-
open class func testInlineAdditionalPropertiesWithRequestBuilder(param: Any) -> RequestBuilder<Void> {
474+
open class func testInlineAdditionalPropertiesWithRequestBuilder(param: JSONValue) -> RequestBuilder<Void> {
475475
let path = "/fake/inline-additionalProperties"
476476
let URLString = PetstoreClientAPI.basePath + path
477477
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: param)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// JSONEncodingHelper.swift
3+
//
4+
// Generated by swagger-codegen
5+
// https://github.com/swagger-api/swagger-codegen
6+
//
7+
8+
import Foundation
9+
10+
11+
public enum JSONValue: Codable, Equatable {
12+
case string(String)
13+
case int(Int)
14+
case double(Double)
15+
case bool(Bool)
16+
case object([String: JSONValue])
17+
case array([JSONValue])
18+
case null
19+
20+
public func encode(to encoder: Encoder) throws {
21+
var container = encoder.singleValueContainer()
22+
switch self {
23+
case .string(let string): try container.encode(string)
24+
case .int(let int): try container.encode(int)
25+
case .double(let double): try container.encode(double)
26+
case .bool(let bool): try container.encode(bool)
27+
case .object(let object): try container.encode(object)
28+
case .array(let array): try container.encode(array)
29+
case .null: try container.encode(Optional<String>.none)
30+
}
31+
}
32+
33+
public init(from decoder: Decoder) throws {
34+
let container = try decoder.singleValueContainer()
35+
self = try ((try? container.decode(String.self)).map(JSONValue.string))
36+
.or((try? container.decode(Int.self)).map(JSONValue.int))
37+
.or((try? container.decode(Double.self)).map(JSONValue.double))
38+
.or((try? container.decode(Bool.self)).map(JSONValue.bool))
39+
.or((try? container.decode([String: JSONValue].self)).map(JSONValue.object))
40+
.or((try? container.decode([JSONValue].self)).map(JSONValue.array))
41+
.or((container.decodeNil() ? .some(JSONValue.null) : .none))
42+
.resolve(
43+
with: DecodingError.typeMismatch(
44+
JSONValue.self,
45+
DecodingError.Context(
46+
codingPath: container.codingPath,
47+
debugDescription: "Not a JSON value"
48+
)
49+
)
50+
)
51+
}
52+
53+
}
54+
55+
extension JSONValue: ExpressibleByStringLiteral {
56+
public init(stringLiteral value: String) {
57+
self = .string(value)
58+
}
59+
}
60+
extension JSONValue: ExpressibleByIntegerLiteral {
61+
public init(integerLiteral value: Int) {
62+
self = .int(value)
63+
}
64+
}
65+
extension JSONValue: ExpressibleByFloatLiteral {
66+
public init(floatLiteral value: Double) {
67+
self = .double(value)
68+
}
69+
}
70+
extension JSONValue: ExpressibleByBooleanLiteral {
71+
public init(booleanLiteral value: Bool) {
72+
self = .bool(value)
73+
}
74+
}
75+
extension JSONValue: ExpressibleByDictionaryLiteral {
76+
public init(dictionaryLiteral elements: (String, JSONValue)...) {
77+
self = .object([String: JSONValue](uniqueKeysWithValues: elements))
78+
}
79+
}
80+
extension JSONValue: ExpressibleByArrayLiteral {
81+
public init(arrayLiteral elements: JSONValue...) {
82+
self = .array(elements)
83+
}
84+
}
85+
86+
fileprivate extension Optional {
87+
func or(_ other: Optional) -> Optional {
88+
switch self {
89+
case .none: return other
90+
case .some: return self
91+
}
92+
}
93+
func resolve(with error: @autoclosure () -> Error) throws -> Wrapped {
94+
switch self {
95+
case .none: throw error()
96+
case .some(let wrapped): return wrapped
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)