-
Notifications
You must be signed in to change notification settings - Fork 6k
Document missing vs. null value behavior in RespectNullableAnnotations #49378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…ations Co-authored-by: gewarren <[email protected]>
Co-authored-by: gewarren <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I verified the code works as advertised.
Removed example demonstrating exception on explicit null value during deserialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses a documentation gap by clarifying how RespectNullableAnnotations
behaves differently when JSON properties are missing versus explicitly set to null
. The documentation update explains that JavaScript's undefined
(missing property) and null
(explicit null value) both deserialize to .NET null
, but only explicit null
values trigger exceptions for non-nullable properties.
Key Changes:
- Added a new "Missing values versus null values" section explaining the behavioral distinction
- Created a code example demonstrating that missing properties don't throw exceptions
- Updated metadata (date and added ai-usage frontmatter)
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
nullable-annotations.md | Added new section documenting missing vs. null value behavior and enforcement guidance |
Nullable.cs | Added Nullable4 class with MissingVsNull example demonstrating missing property behavior |
Program.cs | Updated to run the new Nullable4 example |
|
||
## Missing values versus null values | ||
|
||
It's important to understand the distinction between *missing JSON properties* and *properties with explicit `null` values* when you set <xref:System.Text.Json.JsonSerializerOptions.RespectNullableAnnotations>. JavaScript distinguishes between `undefined` (missing property) and `null` (explicit null value). However, .NET doesn't have an `undefined` concept, so both cases deserialize to `null` in .NET. |
Copilot
AI
Oct 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cross-reference tag should use the property name format RespectNullableAnnotations
instead of the full type path. Change <xref:System.Text.Json.JsonSerializerOptions.RespectNullableAnnotations>
to just reference the property consistently with how it's used elsewhere in the document (as RespectNullableAnnotations
).
It's important to understand the distinction between *missing JSON properties* and *properties with explicit `null` values* when you set <xref:System.Text.Json.JsonSerializerOptions.RespectNullableAnnotations>. JavaScript distinguishes between `undefined` (missing property) and `null` (explicit null value). However, .NET doesn't have an `undefined` concept, so both cases deserialize to `null` in .NET. | |
It's important to understand the distinction between *missing JSON properties* and *properties with explicit `null` values* when you set <xref:RespectNullableAnnotations>. JavaScript distinguishes between `undefined` (missing property) and `null` (explicit null value). However, .NET doesn't have an `undefined` concept, so both cases deserialize to `null` in .NET. |
Copilot uses AI. Check for mistakes.
During deserialization, when `RespectNullableAnnotations` is `true`: | ||
|
||
- An **explicit null value** throws an exception for non-nullable properties. For example, `{"Name":null}` throws an exception when deserializing to a non-nullable `string Name` property. | ||
- A **missing property** doesn't throw an exception, even for non-nullable properties. For example, `{}` doesn't throw an exception when deserializing to a non-nullable `string Name` property. The property is set to `null`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot This is not entirely accurate. Missing properties mean that the serializer will not set the properties, leaving them at their default values as set by the constructor. This implies that a non-nullable string Name
that is left uninitialized will give out a warning from the compiler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right. I've updated the documentation to clarify that the serializer doesn't set the property at all—it leaves it at its default value from the constructor. For uninitialized non-nullable reference types, this results in null
and triggers a compiler warning. Fixed in commit e296c5a.
…o null Co-authored-by: eiriktsarpalis <[email protected]>
Original prompt
Fixes #49298
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Internal previews