Skip to content

Commit 5f7bd6d

Browse files
Expand on RespectNullableAnnotations semantics.
1 parent edf97c4 commit 5f7bd6d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

xml/System.Text.Json/JsonSerializerOptions.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,18 +1163,27 @@ Use <xref:System.Text.Json.Serialization.ReferenceHandler.Preserve> to enable un
11631163
<value>To be added.</value>
11641164
<remarks>
11651165
<para>
1166-
Nullability annotations are resolved from the properties, fields and constructor parameters
1166+
Configures the serializer to throw an exception when trying to serialize a <see langword="null" /> value from a non-nullable property getter,
1167+
or when deserializing a <see langword="null" /> value into a non-nullable property setter or constructor parameter.
1168+
Nullability annotations are resolved from the properties, fields, and constructor parameters
11671169
that are used by the serializer. This includes annotations stemming from attributes such as
11681170
<see cref="T:System.Diagnostics.CodeAnalysis.NotNullAttribute" />, <see cref="T:System.Diagnostics.CodeAnalysis.MaybeNullAttribute" />,
11691171
<see cref="T:System.Diagnostics.CodeAnalysis.AllowNullAttribute" /> and <see cref="T:System.Diagnostics.CodeAnalysis.DisallowNullAttribute" />.
11701172
</para>
11711173
<para>
11721174
Due to restrictions in how nullable reference types are represented at run time,
1173-
this setting only governs nullability annotations of non-generic properties and fields.
1175+
this setting only governs nullability annotations of non-generic properties, fields, and constructor parameters.
11741176
It cannot be used to enforce nullability annotations of root-level types, collection elements, or generic parameters.
11751177
The default setting for this property can be toggled application-wide using the
11761178
"System.Text.Json.Serialization.RespectNullableAnnotationsDefault" feature switch.
11771179
</para>
1180+
<para>
1181+
Nullability validation functions independently of required-ness validation,
1182+
that is to say the setting has no effect in scenaria where the payload is missing a required property.
1183+
This can be configured independently using the <see langword="required" /> keyword,
1184+
the <see cref="P:System.Text.Json.Serialization.JsonRequiredAttribute" /> attribute,
1185+
or the <see cref="P:System.Text.Json.JsonSerializerOptions.RespectRequiredConstructorParameters" /> property.
1186+
</para>
11781187
<para>
11791188
It is recommended that new applications always set this property to <see langword="true" />,
11801189
in combination with the closely related <see cref="P:System.Text.Json.JsonSerializerOptions.RespectRequiredConstructorParameters" /> property.

0 commit comments

Comments
 (0)