Skip to content

Firestore decode fails for an optional @ServerTimestamp field #14883

Open
@fanwgwg

Description

@fanwgwg

Description

Decoding a Firestore document into a Codable model with an optional field marked @ServerTimestamp (e.g., var myTimestamp: Timestamp?) fails if the field is absent in the document. This is commonly needed let's say we're adding a new server write timestamp to an existing data model.

Reproducing the issue

struct TestModel: Codable {
    @ServerTimestamp var optionalServerTs: Date?
    var regularOptional: String?
    var name: String
}

let docRef = db.collection("tests").document("missingOptionalTsDoc")
docRef.getDocument(as: TestModel.self) { result in
    // Decoding fails here if 'optionalServerTs' is missing
    // but 'regularOptional' (if missing) would correctly be nil.
}

Firebase SDK Version

11.12.0

Xcode Version

16.4

Installation Method

Swift Package Manager

Firebase Product(s)

Firestore

Targeted Platforms

iOS

Relevant Log Output

keyNotFound(CodingKeys(stringValue: "lastServerWriteTimestamp", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"lastServerWriteTimestamp\", intValue: nil) (\"lastServerWriteTimestamp\").", underlyingError: nil))

If using Swift Package Manager, the project's Package.resolved

No response

If using CocoaPods, the project's Podfile.lock

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions