|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | +using FluentAssertions; |
| 4 | +using MaLoIdentModels.JsonSettings; |
| 5 | + |
| 6 | +namespace MaLoIdentModelsTests.v1Tests; |
| 7 | + |
| 8 | +public class SinglePropertyDeserializationTests |
| 9 | +{ |
| 10 | + private const string JsonWithEmptyString = "{\"OptionalStringField\":\"\"}"; |
| 11 | + |
| 12 | + private const string JsonWithEmptyLists = "{\"EmptylistField\":[]}"; |
| 13 | + |
| 14 | + private const string JsonWithEmptyObjectsList = |
| 15 | + "{\"ListWithEmptyObjectsField\":[{\"Field\":\"\"}]}"; |
| 16 | + |
| 17 | + private const string JsonWithEmptyNestedObjectsList = |
| 18 | + "{\"ListWithEmptyObjectsField\":[{\"Field\":{\"Field\":\"\"}}]}"; |
| 19 | + |
| 20 | + internal class SomethingWithAOptionalStringField |
| 21 | + { |
| 22 | + [JsonConverter(typeof(EmptyStringConverter))] |
| 23 | + [JsonPropertyName("OptionalStringField")] |
| 24 | + public string? OptionalStringField { get; set; } |
| 25 | + } |
| 26 | + |
| 27 | + internal class SomethingWithAnEmptyListField |
| 28 | + { |
| 29 | + [JsonConverter(typeof(EmptyListToNullConverter<string>))] |
| 30 | + [JsonPropertyName("EmptyListField")] |
| 31 | + public List<string>? EmptyListField { get; set; } |
| 32 | + } |
| 33 | + |
| 34 | + internal class ObjectForList |
| 35 | + { |
| 36 | + [JsonPropertyName("Field")] |
| 37 | + public string? Field { get; set; } |
| 38 | + } |
| 39 | + |
| 40 | + internal class SomethingWithAnEmptyObjectListField |
| 41 | + { |
| 42 | + [JsonConverter(typeof(EmptyListToNullConverter<ObjectForList>))] |
| 43 | + [JsonPropertyName("ListWithEmptyObjectsField")] |
| 44 | + public List<ObjectForList>? ListWithEmptyObjectsField { get; set; } |
| 45 | + } |
| 46 | + |
| 47 | + internal class NestedObject |
| 48 | + { |
| 49 | + [JsonPropertyName("Field")] |
| 50 | + public ObjectForList? Field { get; set; } |
| 51 | + } |
| 52 | + |
| 53 | + internal class SomethingWithAnNestedEmptyObjectListField |
| 54 | + { |
| 55 | + [JsonConverter(typeof(EmptyListToNullConverter<ObjectForList>))] |
| 56 | + [JsonPropertyName("ListWithEmptyNestedObjectsField")] |
| 57 | + public List<ObjectForList>? ListWithEmptyNestedObjectsField { get; set; } |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Test_Models_With_Empty_Strings() |
| 62 | + { |
| 63 | + var deserializing = () => |
| 64 | + JsonSerializer.Deserialize<SomethingWithAOptionalStringField>(JsonWithEmptyString); |
| 65 | + var deserializedModel = deserializing(); |
| 66 | + deserializedModel.Should().NotBeNull(); |
| 67 | + deserializedModel |
| 68 | + .OptionalStringField.Should() |
| 69 | + .BeNull(because: "empty strings should be intpreted as null"); |
| 70 | + var reSererialized = JsonSerializer.Serialize(deserializedModel); |
| 71 | + reSererialized.Should().BeEquivalentTo("{\"OptionalStringField\":null}"); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Test_Models_With_Empty_Lists() |
| 76 | + { |
| 77 | + var deserializing = () => |
| 78 | + JsonSerializer.Deserialize<SomethingWithAnEmptyListField>(JsonWithEmptyLists); |
| 79 | + var deserializedModel = deserializing(); |
| 80 | + deserializedModel.Should().NotBeNull(); |
| 81 | + deserializedModel |
| 82 | + .EmptyListField.Should() |
| 83 | + .BeNull(because: "empty lists should be intpreted as null"); |
| 84 | + var reSererialized = JsonSerializer.Serialize(deserializedModel); |
| 85 | + reSererialized.Should().BeEquivalentTo("{\"EmptylistField\":null}"); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Test_List_With_Empty_Models() |
| 90 | + { |
| 91 | + var deserializing = () => |
| 92 | + JsonSerializer.Deserialize<SomethingWithAnEmptyObjectListField>( |
| 93 | + JsonWithEmptyObjectsList |
| 94 | + ); |
| 95 | + var deserializedModel = deserializing(); |
| 96 | + deserializedModel.Should().NotBeNull(); |
| 97 | + deserializedModel |
| 98 | + .ListWithEmptyObjectsField.Should() |
| 99 | + .BeNull( |
| 100 | + because: "lists with objects where all fields are an empty string should be null" |
| 101 | + ); |
| 102 | + var reSererialized = JsonSerializer.Serialize(deserializedModel); |
| 103 | + reSererialized.Should().BeEquivalentTo("{\"ListWithEmptyObjectsField\":null}"); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void Test_List_With_Empty_Nested_Models() |
| 108 | + { |
| 109 | + var deserializing = () => |
| 110 | + JsonSerializer.Deserialize<SomethingWithAnNestedEmptyObjectListField>( |
| 111 | + JsonWithEmptyNestedObjectsList |
| 112 | + ); |
| 113 | + var deserializedModel = deserializing(); |
| 114 | + deserializedModel.Should().NotBeNull(); |
| 115 | + deserializedModel |
| 116 | + .ListWithEmptyNestedObjectsField.Should() |
| 117 | + .BeNull( |
| 118 | + because: "lists with objects where all fields are an empty string should be null" |
| 119 | + ); |
| 120 | + var reSererialized = JsonSerializer.Serialize(deserializedModel); |
| 121 | + reSererialized.Should().BeEquivalentTo("{\"ListWithEmptyNestedObjectsField\":null}"); |
| 122 | + } |
| 123 | +} |
0 commit comments