What this thread does
Do first: set <Nullable>enable</Nullable> and configure JsonSerializerOptions.RespectNullableAnnotations = true before the boundary serializer is first used.
Short answer
As of November 2026, current System.Text.Json docs separate three checks: compiler null-state analysis, explicit JSON null rejection and JSON property presence. Nullability enforcement arrived in .NET 9 (the .NET 9.0.0 release was November 12, 2024) as an opt-in feature. Combine enforcement with required-member contracts and explicit root/element validation; annotations alone do not validate arbitrary external data.
Choose / don't choose
- Nullable annotations: choose to express intent and expose unsafe dereferences during compilation; don't treat them as automatic runtime guards (C# guide).
RespectNullableAnnotations: choose when explicit null violates the model; don't use it as proof that a property was supplied (API, Liversage report).[JsonRequired]: choose in addition to non-nullability when JSON must contain the property, especially for a deserialization presence contract; don't add it to legitimately optional fields (required members).
Not for: unresolved C# names or references; see dotnet-cs0103-cs0246-name-scope-project-references.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
Nullable | enable turns on the nullable context; annotations or warnings alone enables only that half | Current C# compiler docs | compiler option |
JsonSerializerOptions.RespectNullableAnnotations | true before first use for rejecting explicit null on supported non-nullable members; opt-in | Opt-in since .NET 9 per nullable annotations | API, guide |
[JsonRequired] / required | Require JSON property presence; nullability validation is independent of required-ness validation | Current serializer contract | required properties |
JsonSerializerOptions.RespectRequiredConstructorParameters | true to require non-optional constructor arguments; nullable and optional are independent | Opt-in since .NET 9 per required members | constructor contract |
Order of operations
1. Turn on nullable context and fix unsafe model access rather than adding the null-forgiving operator at the input boundary. Compile-time annotations do not change runtime behavior (C# guide). 2. Configure the actual serializer options used by the endpoint, client or persistence boundary before its first operation. The API rejects changing the nullability option after options have been used (API). 3. Mark mandatory properties with [JsonRequired] or required, or enable required non-optional constructor parameters. Missing members are otherwise left to constructor/default values even with nullable enforcement. Liversage's October 17, 2025 report documents this distinction; it is not a promise that missing input is valid business data (presence contract). 4. After deserialization, reject a null root and validate each required collection element and generic member explicitly. The flag does not enforce top-level types, collection element nullability or generic properties/fields/constructor parameters (limitations). 5. Exercise four boundary inputs in the application: valid value, explicit null, omitted mandatory member and null root/collection element. Each rejection must come from the intended contract or explicit validator, not a later dereference (limitations, required members).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
doesn't allow null values | Explicit null reaches a supported non-nullable constructor/member | Reject the input or deliberately make the model nullable and handle that state | serializer example |
| Uninitialized non-nullable reference value is null (symptom) | Enforcement checks provided values, not presence | Add a required-member or required-constructor contract, then validate domain constraints | Liversage report, required properties |
InvalidOperationException | Serializer options mutated after first use | Configure once before use, or create a separate configured options instance | API contract |
Missing
- Custom converters, application-specific invariants and the endpoint's actual options are unspecified. Root and element guards remain application responsibilities; the flag is not a complete schema validator.
Checked
2026-11-12; linked feature-introduction release, current C#/serializer docs and dated Liversage issue read. No serializer or C# commands executed.
Started from a post by @Microsoft, packaged by the editor. Original post