Added nullability handling when using System.Text.Json serializer#27
Open
RSoeborg wants to merge 6 commits intofirenero:mainfrom
Open
Added nullability handling when using System.Text.Json serializer#27RSoeborg wants to merge 6 commits intofirenero:mainfrom
RSoeborg wants to merge 6 commits intofirenero:mainfrom
Conversation
…tory classes that can handle nullability correctly
- Added tests to handle serialization and deserialization of null properties in TypeIdDecodedContainer objects. - Added tests for collections and dictionaries with null values.
- Updated and added tests to ensure correct serialization and deserialization of nullable TypeIdDecoded properties within arrays and plain objects.
Replaced direct converter additions in `Extensions.cs` with factory instances. Improved exception messages with more informative details in `TypeIdConverterFactory.cs` and `TypeIdDecodedConverterFactory.cs`. This enhances code maintainability and readability by leveraging factory patterns and explicit error conditions.
…ndling for TypeId
Owner
|
Hi @RSoeborg, sorry for a slow response. I can't reproduce the original issue based on your example. These tests pass for me: [TestFixture]
public class NullTests
{
private JsonSerializerOptions _options = new JsonSerializerOptions().ConfigureForTypeId();
[Test]
public void Serialized()
{
var item = new Customer(TypeId.Parse("type_01h455vb4pex5vsknk084sn02q").Decode(), null);
var json = JsonSerializer.Serialize(item, _options);
json.Should().Be("{\"Id\":\"type_01h455vb4pex5vsknk084sn02q\",\"DefaultShippingAddressId\":null}");
}
[Test]
public void Deserialized()
{
var json = "{\"Id\":\"type_01h455vb4pex5vsknk084sn02q\",\"DefaultShippingAddressId\":null}";
var item = JsonSerializer.Deserialize<Customer>(json, _options);
item.Should().BeEquivalentTo(new Customer(TypeId.Parse("type_01h455vb4pex5vsknk084sn02q").Decode(), null));
}
[Test]
public void DeserializedWithValue()
{
var json = "{\"Id\":\"type_01h455vb4pex5vsknk084sn02q\",\"DefaultShippingAddressId\":\"type_01h455vb4pex5vsknk084sn02q\"}";
var item = JsonSerializer.Deserialize<Customer>(json, _options);
var expected = new Customer(
TypeId.Parse("type_01h455vb4pex5vsknk084sn02q").Decode(),
TypeId.Parse("type_01h455vb4pex5vsknk084sn02q").Decode()
);
item.Should().BeEquivalentTo(expected);
}
public class Customer {
public TypeIdDecoded Id { get; }
// other props
public TypeIdDecoded? DefaultShippingAddressId { get; }
public Customer(TypeIdDecoded id, TypeIdDecoded? defaultShippingAddressId)
{
Id = id;
DefaultShippingAddressId = defaultShippingAddressId;
}
}
}Could you please provide a code snipped that reproduces the error? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There is an issue handling nullable TypeId fields when deserializing with System.Text.Json
Consider the below:
When trying to deserialize the nullable property a JsonException is raised:
This PR aims to fix that.