Skip to content

Commit 78c7611

Browse files
committed
fix: null checks and add public to init
1 parent 9f38234 commit 78c7611

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

templates/cli/lib/type-generation/languages/swift.js.twig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@ public class <%- toPascalCase(collection.name) %>: Codable {
7171
<% } -%>
7272
}
7373

74-
init(
74+
public init(
7575
<% for (const [index, attribute] of Object.entries(collection.attributes)) { -%>
76-
<%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
76+
<%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute) %><% if (index < collection.attributes.length - 1) { %>,<% } %>
7777
<% } -%>
7878
) {
7979
<% for (const attribute of collection.attributes) { -%>
80-
self.<%- strict ? toCamelCase(attribute.key) : attribute.key %> = <%- strict ? toCamelCase(attribute.key) : attribute.key %>
80+
self.<%- strict ? toCamelCase(attribute.key) : attribute.key %> = <%- strict ? toCamelCase(attribute.key) : attribute.key %>
8181
<% } -%>
8282
}
8383

8484
public required init(from decoder: Decoder) throws {
8585
let container = try decoder.container(keyedBy: CodingKeys.self)
8686

8787
<% for (const attribute of collection.attributes) { -%>
88-
<% if (attribute.required) { -%>
88+
<% if (!(!attribute.required && attribute.default === null)) { -%>
8989
self.<%- strict ? toCamelCase(attribute.key) : attribute.key %> = try container.decode(<%- getType(attribute).replace('?', '') %>.self, forKey: .<%- strict ? toCamelCase(attribute.key) : attribute.key %>)
9090
<% } else { -%>
9191
self.<%- strict ? toCamelCase(attribute.key) : attribute.key %> = try container.decodeIfPresent(<%- getType(attribute).replace('?', '') %>.self, forKey: .<%- strict ? toCamelCase(attribute.key) : attribute.key %>)
@@ -97,7 +97,7 @@ public class <%- toPascalCase(collection.name) %>: Codable {
9797
var container = encoder.container(keyedBy: CodingKeys.self)
9898

9999
<% for (const attribute of collection.attributes) { -%>
100-
<% if (attribute.required) { -%>
100+
<% if (!(!attribute.required && attribute.default === null)) { -%>
101101
try container.encode(<%- strict ? toCamelCase(attribute.key) : attribute.key %>, forKey: .<%- strict ? toCamelCase(attribute.key) : attribute.key %>)
102102
<% } else { -%>
103103
try container.encodeIfPresent(<%- strict ? toCamelCase(attribute.key) : attribute.key %>, forKey: .<%- strict ? toCamelCase(attribute.key) : attribute.key %>)

0 commit comments

Comments
 (0)