Skip to content

Commit c2ababb

Browse files
wuf810wing328
authored andcommitted
[swift3] empty model with only additional properties (#6273)
1 parent ab28c7c commit c2ababb

40 files changed

+188
-17
lines changed

modules/swagger-codegen/src/main/resources/swift3/Models.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class Decoders {
277277
{{/isEnum}}
278278
{{^isEnum}}
279279
{{#allVars.isEmpty}}
280-
if let source = source as? {{dataType}} {
280+
if let source = source as? {{classname}} {
281281
return .success(source)
282282
} else {
283283
return .failure(.typeMismatch(expected: "Typealias {{classname}}", actual: "\(source)"))
@@ -335,8 +335,11 @@ class Decoders {
335335
}
336336

337337
for key in propsDictionary.keys {
338-
if let decodedValue = Decoders.decodeOptional(clazz: String.self, source: propsDictionary[key] as AnyObject?) {
339-
result[key] = decodedValue
338+
switch Decoders.decodeOptional(clazz: String.self, source: propsDictionary[key] as AnyObject?) {
339+
340+
case let .success(value): result[key] = value
341+
default: continue
342+
340343
}
341344
}
342345
{{/additionalPropertiesType}}

modules/swagger-codegen/src/main/resources/swift3/model.mustache

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ public enum {{classname}}: {{dataType}} {
2424
}
2525
{{/isEnum}}
2626
{{^isEnum}}
27-
{{#vars.isEmpty}}
28-
public typealias {{classname}} = {{dataType}}
29-
{{/vars.isEmpty}}
30-
{{^vars.isEmpty}}
3127
open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}JSONEncodable{{/parent}} {
3228
3329
{{#vars}}
@@ -102,7 +98,7 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}JSONEncod
10298
return dictionary
10399
}
104100
}
105-
{{/vars.isEmpty}}
101+
106102
{{/isEnum}}
107103
{{/isArrayModel}}
108104
{{/model}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.3.0-SNAPSHOT

samples/client/petstore-security-test/swift/SwaggerClient.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Pod::Spec.new do |s|
88
s.license = 'Proprietary'
99
s.homepage = 'https://github.com/swagger-api/swagger-codegen'
1010
s.summary = 'SwaggerClient Swift SDK'
11-
s.source_files = 'SwaggerClient/Classes/Swaggers/**/*.swift'
12-
s.dependency 'Alamofire', '~> 3.4.1'
11+
s.source_files = 'SwaggerClient/Classes/**/*.swift'
12+
s.dependency 'Alamofire', '~> 3.5.1'
1313
end

samples/client/petstore-security-test/swift/SwaggerClient/Classes/Swaggers/Extensions.swift

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,97 @@ extension NSUUID: JSONEncodable {
8383
}
8484
}
8585

86+
/// Represents an ISO-8601 full-date (RFC-3339).
87+
/// ex: 12-31-1999
88+
/// https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14
89+
public final class ISOFullDate: CustomStringConvertible {
90+
91+
public let year: Int
92+
public let month: Int
93+
public let day: Int
94+
95+
public init(year year: Int, month: Int, day: Int) {
96+
self.year = year
97+
self.month = month
98+
self.day = day
99+
}
100+
101+
/**
102+
Converts an NSDate to an ISOFullDate. Only interested in the year, month, day components.
103+
104+
- parameter date: The date to convert.
105+
106+
- returns: An ISOFullDate constructed from the year, month, day of the date.
107+
*/
108+
public static func from(date date: NSDate) -> ISOFullDate? {
109+
guard let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian) else {
110+
return nil
111+
}
112+
113+
let components = calendar.components(
114+
[
115+
.Year,
116+
.Month,
117+
.Day,
118+
],
119+
fromDate: date
120+
)
121+
return ISOFullDate(
122+
year: components.year,
123+
month: components.month,
124+
day: components.day
125+
)
126+
}
127+
128+
/**
129+
Converts a ISO-8601 full-date string to an ISOFullDate.
130+
131+
- parameter string: The ISO-8601 full-date format string to convert.
132+
133+
- returns: An ISOFullDate constructed from the string.
134+
*/
135+
public static func from(string string: String) -> ISOFullDate? {
136+
let components = string
137+
.characters
138+
.split("-")
139+
.map(String.init)
140+
.flatMap { Int($0) }
141+
guard components.count == 3 else { return nil }
142+
143+
return ISOFullDate(
144+
year: components[0],
145+
month: components[1],
146+
day: components[2]
147+
)
148+
}
149+
150+
/**
151+
Converts the receiver to an NSDate, in the default time zone.
152+
153+
- returns: An NSDate from the components of the receiver, in the default time zone.
154+
*/
155+
public func toDate() -> NSDate? {
156+
let components = NSDateComponents()
157+
components.year = year
158+
components.month = month
159+
components.day = day
160+
components.timeZone = NSTimeZone.defaultTimeZone()
161+
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
162+
return calendar?.dateFromComponents(components)
163+
}
164+
165+
// MARK: CustomStringConvertible
166+
167+
public var description: String {
168+
return "\(year)-\(month)-\(day)"
169+
}
170+
171+
}
172+
173+
extension ISOFullDate: JSONEncodable {
174+
public func encodeToJSON() -> AnyObject {
175+
return "\(year)-\(month)-\(day)"
176+
}
177+
}
178+
86179

samples/client/petstore-security-test/swift/SwaggerClient/Classes/Swaggers/Models.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,16 @@ class Decoders {
140140
return NSDate(timeIntervalSince1970: Double(sourceInt / 1000) )
141141
}
142142
fatalError("formatter failed to parse \(source)")
143-
}
143+
}
144+
145+
// Decoder for ISOFullDate
146+
Decoders.addDecoder(clazz: ISOFullDate.self, decoder: { (source: AnyObject) -> ISOFullDate in
147+
if let string = source as? String,
148+
let isoDate = ISOFullDate.from(string: string) {
149+
return isoDate
150+
}
151+
fatalError("formatter failed to parse \(source)")
152+
})
144153

145154
// Decoder for [Return]
146155
Decoders.addDecoder(clazz: [Return].self) { (source: AnyObject) -> [Return] in

samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/Models.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ class Decoders {
824824
}
825825
// Decoder for OuterBoolean
826826
Decoders.addDecoder(clazz: OuterBoolean.self) { (source: AnyObject, instance: AnyObject?) -> Decoded<OuterBoolean> in
827-
if let source = source as? Bool {
827+
if let source = source as? OuterBoolean {
828828
return .success(source)
829829
} else {
830830
return .failure(.typeMismatch(expected: "Typealias OuterBoolean", actual: "\(source)"))
@@ -864,15 +864,15 @@ class Decoders {
864864
}
865865
// Decoder for OuterNumber
866866
Decoders.addDecoder(clazz: OuterNumber.self) { (source: AnyObject, instance: AnyObject?) -> Decoded<OuterNumber> in
867-
if let source = source as? Double {
867+
if let source = source as? OuterNumber {
868868
return .success(source)
869869
} else {
870870
return .failure(.typeMismatch(expected: "Typealias OuterNumber", actual: "\(source)"))
871871
}
872872
}
873873
// Decoder for OuterString
874874
Decoders.addDecoder(clazz: OuterString.self) { (source: AnyObject, instance: AnyObject?) -> Decoded<OuterString> in
875-
if let source = source as? String {
875+
if let source = source as? OuterString {
876876
return .success(source)
877877
} else {
878878
return .failure(.typeMismatch(expected: "Typealias OuterString", actual: "\(source)"))

samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/Models/AdditionalPropertiesClass.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ open class AdditionalPropertiesClass: JSONEncodable {
2525
return dictionary
2626
}
2727
}
28+

samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/Models/Animal.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ open class Animal: JSONEncodable {
2525
return dictionary
2626
}
2727
}
28+

samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/Models/ApiResponse.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ open class ApiResponse: JSONEncodable {
2727
return dictionary
2828
}
2929
}
30+

0 commit comments

Comments
 (0)