Skip to content

Commit 136cf1c

Browse files
authored
Merge pull request #8488 from JoelSpeed/discourage-nullable
Discourage usage of nullable on API types
2 parents 755891f + 33b481e commit 136cf1c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

contributors/devel/sig-architecture/api-conventions.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ An introduction to using resources with kubectl can be found in [the object mana
2727
- [Categories](#categories)
2828
- [Idempotency](#idempotency)
2929
- [Optional vs. Required](#optional-vs-required)
30+
- [Nullable](#nullable)
3031
- [Defaulting](#defaulting)
3132
- [Static Defaults](#static-defaults)
3233
- [Admission Controlled Defaults](#admission-controlled-defaults)
@@ -911,6 +912,26 @@ In these cases, pointers are not needed, as the zero value indicates to the stru
911912

912913
This can be beneficial to API authors, as it reduces the complexity of the API, and reduces the risk of nil pointer exceptions in controllers.
913914

915+
## Nullable
916+
917+
The `+nullable` comment tag allows the json `null` value to be a valid value
918+
for a field. The `null` value is serialized only when a field is a pointer
919+
in the Go definition, and does not have the `omitempty` json tag, or sometimes where a
920+
custom marshal function is implemented.
921+
922+
For example, a map field marked with `+nullable` would accept either `foo: null` or `foo: {}`.
923+
924+
Usage of `+nullable` is discouraged as it introduces several issues:
925+
- It is not compatible with json merge patching.
926+
- From the [JSON Merge Patch RFC](https://tools.ietf.org/html/rfc7386#section-1):
927+
> Null values in the merge patch are given special meaning to indicate the removal of existing values in the target.
928+
- Explicit `null` values are not persisted in proto serializations.
929+
- `null` values are not supported by Server-Side Apply applyconfiguration types.
930+
- A persisted `null` value would not round-trip through the applyconfiguration type
931+
encode/decode cycle.
932+
933+
Avoid designing APIs that require the distinction between unset and `null`.
934+
914935
## Defaulting
915936

916937
In general we want default values to be explicitly represented in our APIs,

0 commit comments

Comments
 (0)