firebase / firebase/firebase-ios-sdk

Firestore decode fails for an optional @ServerTimestamp field

Open
#14,883 10 comments 0 reactions 1 assignee Assigned to @cherylEnkidu View on GitHub
api: firestore
Dominant language
C++
Stars
6.7k
Forks
1.8k
Avg merge
2d 14h
Merged PRs (30d)
72

Description

### 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

```shell
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.