Skip to content

Commit e320397

Browse files
authored
Stops null values from overriding existing values (#8)
1 parent 1f5a6fe commit e320397

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/Objects/Internal/JsonConfigurationSerializer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ private void VisitPrimitive(JValue data)
108108
{
109109
throw new FormatException($"A duplicate key '{key}' was found.");
110110
}
111-
_data[key] = data.ToString(CultureInfo.InvariantCulture);
111+
112+
var stringValue = data.ToString(CultureInfo.InvariantCulture);
113+
114+
if (!string.IsNullOrEmpty(stringValue))
115+
{
116+
_data[key] = stringValue;
117+
}
112118
}
113119

114120
private void EnterContext(string context)

tests/Tests.Objects/ConfigurationTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,37 @@ public void Object_can_be_retrieved_from_configuration(ConfigurationBuilder conf
123123
}
124124

125125
bool Comparison(ObjectWithSimpleProperties first, ObjectWithSimpleProperties second) => first.Text == second.Text && first.Value == second.Value;
126+
127+
128+
[Test, CustomAutoData]
129+
[Property("Issue", "3")]
130+
public void Null_values_should_not_override_existing_values(ConfigurationBuilder configurationBuilder, ObjectWithSimpleProperties testSource)
131+
{
132+
configurationBuilder.AddObject(testSource);
133+
134+
configurationBuilder.AddObject(new ObjectWithSimpleProperties{ Text = null, Value = testSource.Value });
135+
136+
var configuration = configurationBuilder.Build();
137+
138+
var result = configuration.Get<ObjectWithSimpleProperties>();
139+
140+
Assert.That(result.Text, Is.EqualTo(testSource.Text));
141+
Assert.That(result.Value, Is.EqualTo(testSource.Value));
142+
}
143+
144+
[Test, CustomAutoData]
145+
[Property("Issue", "3")]
146+
public void Null_values_should_not_override_existing_values(ConfigurationBuilder configurationBuilder, ObjectWithSimpleIntArray testSource)
147+
{
148+
configurationBuilder.AddObject(testSource);
149+
150+
configurationBuilder.AddObject(new ObjectWithSimpleIntArray { Values = new int[0] });
151+
152+
var configuration = configurationBuilder.Build();
153+
154+
var result = configuration.Get<ObjectWithSimpleIntArray>();
155+
156+
Assert.That(result.Values, Is.EquivalentTo(testSource.Values));
157+
}
126158
}
127159
}

tests/Tests.Objects/Internal/JsonConfigurationSerializerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,19 @@ public void Importing_same_object_twice_should_not_throw(JsonConfigurationSerial
8383
Assert.That(result[$"{rootSectionName}:{nameof(testSource.Items)}:0:{nameof(ObjectWithSimpleProperties.Text)}"], Is.EqualTo($"{testSource.Items[0].Text}"));
8484
Assert.That(result[$"{rootSectionName}:{nameof(testSource.Items)}:0:{nameof(ObjectWithSimpleProperties.Value)}"], Is.EqualTo($"{testSource.Items[0].Value}"));
8585
}
86+
87+
[Test, CustomAutoData]
88+
[Property("Issue", "2")]
89+
public void Null_values_should_not_be_added(JsonConfigurationSerializer sut, ObjectWithSimpleProperties testSource, string rootSectionName)
90+
{
91+
testSource.Text = null;
92+
93+
var result = sut.Serialize(testSource, rootSectionName);
94+
95+
Assert.That(result, Does.Not.ContainKey($"{rootSectionName}:{nameof(testSource.Text)}"));
96+
Assert.That(result, Contains.Key($"{rootSectionName}:{nameof(testSource.Value)}"));
97+
98+
Assert.That(result[$"{rootSectionName}:{nameof(testSource.Value)}"], Is.EqualTo($"{testSource.Value}"));
99+
}
86100
}
87101
}

0 commit comments

Comments
 (0)