googleapis / googleapis/google-cloud-swift

Improve serialization of optional fields set to `nil`

Open
#827 0 comments 0 reactions 0 assignees View on GitHub
priority: p2
Dominant language
Swift
Stars
26
Forks
10
Avg merge
12h 56m
Merged PRs (30d)
211

Description

Gemini says:

#### 2. Unset Optional Fields Emit `null` (Major Defect)

* **Spec Requirement**:

> *"Serializers should not emit null values... The field should remain unset, as though it was not present in the input at all... `google.protobuf.NullValue` is a special exception to this behavior: null is handled as a sentinel-present value for this type."*

* **Current Implementation**:

Generated message types with optional fields call `encode(self.field, forKey: .field)`:

```swift
try container.encode(self.option, forKey: .option)
```

When `self.option == nil`, Swift's `KeyedEncodingContainer.encode(_:forKey:)` invokes `encodeNil(forKey:)`, resulting in `"option": null`. In ProtoJSON, unset optional fields should be omitted from the output altogether. Only `NullValue` should emit `null`.

----

These are "SHOULD" requirements, we can ignore them, but would be nice to meet them.

Contributor guide

Open the contributing guide

Research direction

Start by locating the generated message-type encode methods that call encode(self.field, forKey: .field), then trace how KeyedEncodingContainer.encode(_:forKey:) handles nil values. Verify that unset optional fields are omitted from ProtoJSON output while google.protobuf.NullValue still emits null; the issue does not name specific files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.