@@ -3,9 +3,18 @@ import JSONCodable
3
3
4
4
/// {{ definition .description }}
5
5
{% if definition .properties | length == 0 and not definition .additionalProperties %}
6
- public class {{ definition | modelType(spec ) | raw }} {}
6
+ open class {{ definition | modelType(spec ) | raw }}: Codable {}
7
7
{% else %}
8
- public class {{ definition | modelType(spec ) | raw }} {
8
+ open class {{ definition | modelType(spec ) | raw }}: Codable {
9
+
10
+ enum CodingKeys: String, CodingKey {
11
+ {%~ for property in definition .properties %}
12
+ case {{ property .name | escapeSwiftKeyword | removeDollarSign }} = "{{ property .name }}"
13
+ {%~ endfor %}
14
+ {%~ if definition .additionalProperties %}
15
+ case data
16
+ {%~ endif %}
17
+ }
9
18
10
19
{%~ for property in definition .properties %}
11
20
/// {{ property .description }}
@@ -35,6 +44,28 @@ public class {{ definition | modelType(spec) | raw }} {
35
44
{%~ endif %}
36
45
}
37
46
47
+ public required init(from decoder: Decoder) throws {
48
+ let container = try decoder.container(keyedBy: CodingKeys.self)
49
+
50
+ {%~ for property in definition .properties %}
51
+ self.{{ property .name | escapeSwiftKeyword | removeDollarSign }} = try container.decode{% if not property .required %}IfPresent{% endif %}({{ property | propertyType(spec ) | raw }}.self, forKey: .{{ property .name | escapeSwiftKeyword | removeDollarSign }})
52
+ {%~ endfor %}
53
+ {%~ if definition .additionalProperties %}
54
+ self.data = try container.decode(T.self, forKey: .data)
55
+ {%~ endif %}
56
+ }
57
+
58
+ public func encode(to encoder: Encoder) throws {
59
+ var container = encoder.container(keyedBy: CodingKeys.self)
60
+
61
+ {%~ for property in definition .properties %}
62
+ try container.encode{% if not property .required %}IfPresent{% endif %}({{ property .name | escapeSwiftKeyword | removeDollarSign }}, forKey: .{{ property .name | escapeSwiftKeyword | removeDollarSign }})
63
+ {%~ endfor %}
64
+ {%~ if definition .additionalProperties %}
65
+ try container.encode(data, forKey: .data)
66
+ {%~ endif %}
67
+ }
68
+
38
69
public func toMap() -> [String: Any] {
39
70
return [
40
71
{%~ for property in definition .properties %}
0 commit comments