You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When serializing a null JSON property against a nullable init-only class property using a source generated JsonSerializerContext with RespectNullableAnnotations enabled, the serializer throws and exception claiming that the property doesn't allow null values.
This behaviour is not present when using a runtime type serializer instead of a source-generated one.
Reproduction Steps
usingSystem.Text.Json;usingSystem.Text.Json.Serialization;conststringjsonObject=""" { "NullableProperty": null } """;varresult=JsonSerializer.Deserialize(jsonObject,NullableJsonContext.Default.MyNullableObject);Console.WriteLine(result!.NullableProperty);publicclassMyNullableObject{publicstring?NullableProperty{get;init;}}// Setting RespectNullableAnnotations to false will make this pass, as will changing the init accessor to a private setter[JsonSourceGenerationOptions(RespectNullableAnnotations=true)][JsonSerializable(typeof(MyNullableObject))]publicpartialclassNullableJsonContext:JsonSerializerContext;
Expected behavior
The nullable property (NullableProperty in the example) serializes the property as a null value.
Actual behavior
Serialization throws a JsonException:
Unhandled exception. System.Text.Json.JsonException: The constructor parameter 'NullableProperty' on type 'MyNullableObject' doesn't allow null values. Consider updating its nullability annotation. Path: $.NullableProperty | LineNumber: 1 | BytePositionInLine: 28.
at System.Text.Json.ThrowHelper.ThrowJsonException_ConstructorParameterDisallowNull(String parameterName, Type declaringType)
at System.Text.Json.Serialization.Converters.LargeObjectWithParameterizedConstructorConverter`1.ReadAndCacheConstructorArgument(ReadStack& state, Utf8JsonReader& reader, JsonParameterInfo jsonParameterInfo)
at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.Converters.JsonMetadataServicesConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonTypeInfo`1 jsonTypeInfo)
at Program.<Main>$(String[] args) in /.../Program.cs:line 11
Description
When serializing a
null
JSON property against a nullableinit
-only class property using a source generatedJsonSerializerContext
withRespectNullableAnnotations
enabled, the serializer throws and exception claiming that the property doesn't allownull
values.This behaviour is not present when using a runtime type serializer instead of a source-generated one.
Reproduction Steps
Expected behavior
The nullable property (
NullableProperty
in the example) serializes the property as anull
value.Actual behavior
Serialization throws a
JsonException
:Regression?
No response
Known Workarounds
Using a non-source generated serializer works:
Configuration
Tested on:
9.0.0-rc.1.24431.7
Other information
No response
The text was updated successfully, but these errors were encountered: