How to Clear API Fields with PATCH When Null Values Are Dropped from JSON
A common conflict arises in API design when following Google's JSON Style Guide recommendation to omit null or empty properties, while also supporting PATCH requests for partial resource updates. Because both an intentional field-clearing action and a 'leave this field alone' instruction look identical when null values are stripped from the request body, the server cannot distinguish between the two. Two approaches from Google's API Improvement Proposals can resolve this ambiguity: using an enum zero value to represent an unset state (AIP-126), or explicitly listing fields to be updated via an update mask (AIP-134). The update mask approach is more versatile and works across all field types, while the enum zero value method is best suited to enum fields specifically. Both solutions preserve idempotency, meaning the requests can be safely retried without unintended side effects.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in