Skip to content
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

Nullable JsonDocument property will deserialized to JsonValueKind.Null instead of null #110231

Closed
aco-mreble opened this issue Nov 27, 2024 · 3 comments

Comments

@aco-mreble
Copy link

aco-mreble commented Nov 27, 2024

Description

After upgrading from dotnet 8 to dotnet 9 our applications don't work anymore. This is because we are using HttpClient generation with NSwag and unknown objects are serialized to JsonDocument? properties. This is a breaking change. I have tried a little bit with the new RespectNullableAnnotations and DefaultIgnoreCondition but this has not worked either.

Reproduction Steps

using System.Text.Json;

//lang=json
var json = """
           {
             "Properties": null
           }
           """;
var dto = JsonSerializer.Deserialize<MyTestClass>(json);

//Returns true in dotnet 8 and false in dotnet 9
Console.WriteLine(dto!.Properties is null);

//Returns false in dotnet 8 and true in dotnet 9
Console.WriteLine(dto!.Properties is JsonDocument { RootElement.ValueKind: JsonValueKind.Null });

public class MyTestClass()
{
	public JsonDocument? Properties { get; set; }
}

Expected behavior

Properties property in MyTestClass should be null

Actual behavior

Properties property in MyTestClass is JsonDocument with RootElement.ValueKind = JsonValueKind.Null

Regression?

It worked on dotnet 8

Known Workarounds

public class JsonDocumentNullableConverter : JsonConverter<JsonDocument?>
{
	public override JsonDocument? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType == JsonTokenType.Null)
			return null;
		return JsonDocument.ParseValue(ref reader);
	}

	public override void Write(Utf8JsonWriter writer, JsonDocument? value, JsonSerializerOptions options)
	{
		if (value is null)
		{
			writer.WriteNullValue();
			return;
		}

		value.WriteTo(writer);
	}
}

Configuration

dotnet 9.0.100 SDK

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 27, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis
Copy link
Member

The change was made intentionally in #97596 to address an inconsistency between JsonDocument.Parse which correcty maps null to JsonValueKind.Null and the built-in converter for JsonDocument.

@jeffhandley
Copy link
Member

Thank you for reporting that you encountered this, @aco-mreble. I've referenced the code sample you provided in the breaking change documentation issue: dotnet/docs#43869.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants