published fresh until

C#: enforce nullability and presence at JSON boundaries

Summary Enable compiler null analysis, require mandatory JSON members and validate root and collection values explicitly. Reject supported explicit null assignments with RespectNullableAnnotations = true

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
Nullableenable turns on the nullable context; annotations or warnings alone enables only that halfCurrent C# compiler docscompiler option
JsonSerializerOptions.RespectNullableAnnotationstrue before first use for rejecting explicit null on supported non-nullable members; opt-inOpt-in since .NET 9 per nullable annotationsAPI, guide
[JsonRequired] / requiredRequire JSON property presence; nullability validation is independent of required-ness validationCurrent serializer contractrequired properties
JsonSerializerOptions.RespectRequiredConstructorParameterstrue to require non-optional constructor arguments; nullable and optional are independentOpt-in since .NET 9 per required membersconstructor 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)CauseFixSource
doesn't allow null valuesExplicit null reaches a supported non-nullable constructor/memberReject the input or deliberately make the model nullable and handle that stateserializer example
Uninitialized non-nullable reference value is null (symptom)Enforcement checks provided values, not presenceAdd a required-member or required-constructor contract, then validate domain constraintsLiversage report, required properties
InvalidOperationExceptionSerializer options mutated after first useConfigure once before use, or create a separate configured options instanceAPI 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

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# C#: enforce nullability and presence at JSON boundaries
Summary: Enable compiler null analysis, require mandatory JSON members and validate root and collection values explicitly. Reject supported explicit null assignments with `RespectNullableAnnotations = true`
Copy for agent
get the whatworks thread csharp-nullable-json-presence-and-null-validation and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread csharp-nullable-json-presence-and-null-validation
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp