Skip to content

Commit 4fd3eac

Browse files
Assign to properties with explicit self in init(from decoder:)
1 parent f1d7d8b commit 4fd3eac

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateCodable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ extension FileTranslator {
118118
let assignExprs: [Expression] = properties.map { property in
119119
let typeUsage = property.typeUsage
120120
return .assignment(
121-
left: .identifierPattern(property.swiftSafeName),
121+
left: .identifierPattern("self").dot(property.swiftSafeName),
122122
right: .try(
123123
.identifierPattern("container").dot("decode\(typeUsage.isOptional ? "IfPresent" : "")")
124124
.call([

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Types.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ public enum Components {
530530
}
531531
public init(from decoder: any Decoder) throws {
532532
let container = try decoder.container(keyedBy: CodingKeys.self)
533-
foo = try container.decodeIfPresent(
533+
self.foo = try container.decodeIfPresent(
534534
Swift.String.self,
535535
forKey: .foo
536536
)
@@ -562,7 +562,7 @@ public enum Components {
562562
}
563563
public init(from decoder: any Decoder) throws {
564564
let container = try decoder.container(keyedBy: CodingKeys.self)
565-
foo = try container.decodeIfPresent(
565+
self.foo = try container.decodeIfPresent(
566566
Swift.String.self,
567567
forKey: .foo
568568
)
@@ -602,7 +602,7 @@ public enum Components {
602602
}
603603
public init(from decoder: any Decoder) throws {
604604
let container = try decoder.container(keyedBy: CodingKeys.self)
605-
foo = try container.decodeIfPresent(
605+
self.foo = try container.decodeIfPresent(
606606
Swift.String.self,
607607
forKey: .foo
608608
)

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,30 @@ final class SnippetBasedReferenceTests: XCTestCase {
510510
schemas:
511511
MyObject:
512512
type: object
513-
properties: {}
513+
properties:
514+
id:
515+
type: string
514516
additionalProperties: false
515517
""",
516518
"""
517519
public enum Schemas {
518520
public struct MyObject: Codable, Hashable, Sendable {
519-
public init() {}
521+
public var id: Swift.String?
522+
public init(id: Swift.String? = nil) {
523+
self.id = id
524+
}
525+
public enum CodingKeys: String, CodingKey {
526+
case id
527+
}
520528
public init(from decoder: any Decoder) throws {
521-
try decoder.ensureNoAdditionalProperties(knownKeys: [])
529+
let container = try decoder.container(keyedBy: CodingKeys.self)
530+
self.id = try container.decodeIfPresent(
531+
Swift.String.self,
532+
forKey: .id
533+
)
534+
try decoder.ensureNoAdditionalProperties(knownKeys: [
535+
"id"
536+
])
522537
}
523538
}
524539
}

0 commit comments

Comments
 (0)