-
Notifications
You must be signed in to change notification settings - Fork 5k
Configuration Json provider: adds empty objects to configuration #40829
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
Merged
maryamariyan
merged 1 commit into
dotnet:master
from
alefranz:configuration-json-emptyobjects
Aug 19, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
src/libraries/Microsoft.Extensions.Configuration.Json/tests/EmptyObjectTest.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration.Test; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.Configuration.Json.Test | ||
{ | ||
public class EmptyObjectTest | ||
{ | ||
[Fact] | ||
public void EmptyObject_AddsAsNull() | ||
{ | ||
var json = @"{ | ||
""key"": { }, | ||
}"; | ||
|
||
var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource()); | ||
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); | ||
|
||
Assert.Null(jsonConfigSource.Get("key")); | ||
halter73 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[Fact] | ||
public void NullObject_AddsEmptyString() | ||
{ | ||
var json = @"{ | ||
""key"": null, | ||
}"; | ||
|
||
var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource()); | ||
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); | ||
|
||
Assert.Equal("", jsonConfigSource.Get("key")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced this is intentional, I've raised #40827 |
||
} | ||
|
||
[Fact] | ||
public void NestedObject_DoesNotAddParent() | ||
{ | ||
var json = @"{ | ||
""key"": { | ||
""nested"": ""value"" | ||
}, | ||
}"; | ||
|
||
var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource()); | ||
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); | ||
|
||
Assert.False(jsonConfigSource.TryGet("key", out _)); | ||
Assert.Equal("value", jsonConfigSource.Get("key:nested")); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/libraries/Microsoft.Extensions.Configuration.Json/tests/IntegrationTest.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration.Test; | ||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace Microsoft.Extensions.Configuration.Json.Test | ||
{ | ||
public class IntegrationTest | ||
{ | ||
[Fact] | ||
public void LoadJsonConfiguration() | ||
{ | ||
var json = @"{ | ||
""a"": ""b"", | ||
""c"": { | ||
""d"": ""e"" | ||
}, | ||
""f"": """", | ||
""g"": null, | ||
""h"": {}, | ||
""i"": { | ||
""k"": {} | ||
} | ||
}"; | ||
|
||
var configurationBuilder = new ConfigurationBuilder(); | ||
configurationBuilder.AddJsonStream(TestStreamHelpers.StringToStream(json)); | ||
var configuration = configurationBuilder.Build(); | ||
|
||
Assert.Collection(configuration.GetChildren(), | ||
new Action<IConfigurationSection>[] { | ||
x => AssertSection(x, "a", "b"), | ||
x => AssertSection(x, "c", null, new Action<IConfigurationSection>[] { | ||
x => AssertSection(x, "d", "e"), | ||
}), | ||
x => AssertSection(x, "f", ""), | ||
x => AssertSection(x, "g", ""), | ||
x => AssertSection(x, "h", null), | ||
x => AssertSection(x, "i", null, new Action<IConfigurationSection>[] { | ||
x => AssertSection(x, "k", null), | ||
}), | ||
}); | ||
} | ||
|
||
private static void AssertSection(IConfigurationSection configurationSection, string key, string value) | ||
=> AssertSection(configurationSection, key, value, new Action<IConfigurationSection>[0]); | ||
|
||
private static void AssertSection(IConfigurationSection configurationSection, string key, string value, Action<IConfigurationSection>[] childrenInspectors) | ||
{ | ||
if (key != configurationSection.Key || value != configurationSection.Value) | ||
{ | ||
throw new EqualException( | ||
expected: GetString(key, value), | ||
actual: GetString(configurationSection)); | ||
} | ||
|
||
Assert.Collection(configurationSection.GetChildren(), childrenInspectors); | ||
} | ||
|
||
private static string GetString(IConfigurationSection configurationSection) => GetString(configurationSection.Key, configurationSection.Value); | ||
private static string GetString(string key, string value) => $"\"{key}\":" + (value is null ? "null" : $"\"{value}\""); | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.