mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 03:18:53 +00:00
[Swift] Force required attrs to be defined with unwrapRequired
Fix #2116 Removal of forced unwrapping, replaced by required attributes in constructor
This commit is contained in:
parent
0792ddc9b4
commit
dc4c8e5864
@ -137,9 +137,22 @@ class Decoders {
|
||||
// Decoder for {{{classname}}}
|
||||
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in
|
||||
let sourceDictionary = source as! [NSObject:AnyObject]
|
||||
{{#unwrapRequired}}
|
||||
let instance = {{classname}}({{#requiredVars}}{{^-first}}, {{/-first}}{{#isEnum}}{{name}}: {{classname}}.{{datatypeWithEnum}}(rawValue: (sourceDictionary["{{baseName}}"] as? String) ?? "")! {{/isEnum}}{{^isEnum}}{{name}}: Decoders.decode(clazz: {{{baseType}}}.self, source: sourceDictionary["{{baseName}}"]!){{/isEnum}}{{/requiredVars}})
|
||||
{{#optionalVars}}
|
||||
{{#isEnum}}
|
||||
instance.{{name}} = {{classname}}.{{datatypeWithEnum}}(rawValue: (sourceDictionary["{{baseName}}"] as? String) ?? "")
|
||||
{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
instance.{{name}} = Decoders.decodeOptional(clazz: {{{baseType}}}.self, source: sourceDictionary["{{baseName}}"])
|
||||
{{/isEnum}}
|
||||
{{/optionalVars}}
|
||||
{{/unwrapRequired}}
|
||||
{{^unwrapRequired}}
|
||||
let instance = {{classname}}(){{#vars}}{{#isEnum}}
|
||||
instance.{{name}} = {{classname}}.{{datatypeWithEnum}}(rawValue: (sourceDictionary["{{baseName}}"] as? String) ?? ""){{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}} {{/isEnum}}{{^isEnum}}
|
||||
instance.{{name}} = Decoders.decode{{^unwrapRequired}}Optional{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}Optional{{/required}}{{/unwrapRequired}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{baseName}}"]{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}}){{/isEnum}}{{/vars}}
|
||||
instance.{{name}} = {{classname}}.{{datatypeWithEnum}}(rawValue: (sourceDictionary["{{baseName}}"] as? String) ?? "") {{/isEnum}}{{^isEnum}}
|
||||
instance.{{name}} = Decoders.decodeOptional(clazz: {{{baseType}}}.self, source: sourceDictionary["{{baseName}}"]){{/isEnum}}{{/vars}}
|
||||
{{/unwrapRequired}}
|
||||
return instance
|
||||
}{{/model}}
|
||||
{{/models}}
|
||||
|
@ -21,15 +21,26 @@ public class {{classname}}: JSONEncodable {
|
||||
{{#vars}}
|
||||
{{#isEnum}}
|
||||
{{#description}}/** {{description}} */
|
||||
{{/description}}public var {{name}}: {{{datatypeWithEnum}}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{/unwrapRequired}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
|
||||
{{/description}}public var {{name}}: {{{datatypeWithEnum}}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
|
||||
{{/isEnum}}
|
||||
{{^isEnum}}
|
||||
{{#description}}/** {{description}} */
|
||||
{{/description}}public var {{name}}: {{{datatype}}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{/unwrapRequired}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
|
||||
{{/description}}public var {{name}}: {{{datatype}}}{{^unwrapRequired}}?{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}?{{/required}}{{/unwrapRequired}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
|
||||
{{/isEnum}}
|
||||
{{/vars}}
|
||||
|
||||
{{^unwrapRequired}}
|
||||
public init() {}
|
||||
{{/unwrapRequired}}
|
||||
{{#unwrapRequired}}
|
||||
public init({{#requiredVars}}{{^-first}}, {{/-first}}{{name}}: {{#isEnum}}{{datatypeWithEnum}}!{{/isEnum}}{{^isEnum}}{{datatype}}!{{/isEnum}}{{/requiredVars}}) {
|
||||
{{#vars}}
|
||||
{{#requiredVars}}
|
||||
self.{{name}} = {{name}}
|
||||
{{/requiredVars}}
|
||||
{{/vars}}
|
||||
}
|
||||
{{/unwrapRequired}}
|
||||
|
||||
// MARK: JSONEncodable
|
||||
func encodeToJSON() -> AnyObject {
|
||||
|
Loading…
Reference in New Issue
Block a user