Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign to properties with explicit self in init(from decoder:) #696

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ extension FileTranslator {
let assignExprs: [Expression] = properties.map { property in
let typeUsage = property.typeUsage
return .assignment(
left: .identifierPattern(property.swiftSafeName),
left: .identifierPattern("self").dot(property.swiftSafeName),
right: .try(
.identifierPattern("container").dot("decode\(typeUsage.isOptional ? "IfPresent" : "")")
.call([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public enum Components {
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
foo = try container.decodeIfPresent(
self.foo = try container.decodeIfPresent(
Swift.String.self,
forKey: .foo
)
Expand Down Expand Up @@ -626,7 +626,7 @@ public enum Components {
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
foo = try container.decodeIfPresent(
self.foo = try container.decodeIfPresent(
Swift.String.self,
forKey: .foo
)
Expand Down Expand Up @@ -666,7 +666,7 @@ public enum Components {
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
foo = try container.decodeIfPresent(
self.foo = try container.decodeIfPresent(
Swift.String.self,
forKey: .foo
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,30 @@ final class SnippetBasedReferenceTests: XCTestCase {
schemas:
MyObject:
type: object
properties: {}
properties:
id:
type: string
additionalProperties: false
""",
"""
public enum Schemas {
public struct MyObject: Codable, Hashable, Sendable {
public init() {}
public var id: Swift.String?
public init(id: Swift.String? = nil) {
self.id = id
}
public enum CodingKeys: String, CodingKey {
case id
}
public init(from decoder: any Decoder) throws {
try decoder.ensureNoAdditionalProperties(knownKeys: [])
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decodeIfPresent(
Swift.String.self,
forKey: .id
)
try decoder.ensureNoAdditionalProperties(knownKeys: [
"id"
])
}
}
}
Expand Down
Loading