Skip to content

Commit 47f05a0

Browse files
committed
Use 'optional' instead of 'ignoreMissingFile' for Json to be consistent with UserSecrets.
1 parent 0c1df60 commit 47f05a0

5 files changed

Lines changed: 14 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ up connection information from the execution environment.
101101
<add name="SimpleJson"
102102
[mode|prefix|stripPrefix]
103103
jsonFile="~\config.json"
104-
[ignoreMissingFile="true"]
104+
[optional="true"]
105105
[jsonMode="(Flat|Sectional)"]
106106
type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json" />
107107
```
@@ -114,7 +114,7 @@ begins with 'Simple.' Think of the backing json file as a simple dictionary, rat
114114

115115
There are three additional attributes that can be used to configure this builder:
116116
* `jsonFile` - A required attribute specifying the json file to draw from. The '~' character can be used at the start to reference the app root.
117-
* `ignoreMissingFile` - A simple boolean to avoid throwing exceptions if the secrets file cannot be found. The default is `true`.
117+
* `optional` - A simple boolean to avoid throwing exceptions if the json file cannot be found. The default is `true`.
118118
* `jsonMode` - `[Flat|Sectional]`. 'Flat' is the default.
119119
- This attribute requires a little more explanation. It says above to think of the json file as a single flat key/value source. This is the usual that applies to other key/value config builders like `EnvironmentConfigBuilder` and `AzureKeyVaultConfigBuilder` because those sources provide no other option. If the `SimpleJsonConfigBuilder` is configured in 'Sectional' mode, then the json file is conceptually divided just at the top level into multiple simple dictionaries. Each one of those dictionaries will only be applied to the config section that matches the top-level property name attached to them. For example:
120120
```json

src/Json/SimpleJsonConfigBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ namespace Microsoft.Configuration.ConfigurationBuilders
1616
public class SimpleJsonConfigBuilder : KeyValueConfigBuilder
1717
{
1818
public const string jsonFileTag = "jsonFile";
19-
public const string ignoreMissingFileTag = "ignoreMissingFile";
19+
public const string optionalTag = "optional";
2020
public const string jsonModeTag = "jsonMode";
2121
public const string keyDelimiter = ":";
2222

2323
private string _currentSection;
2424
private Dictionary<string, Dictionary<string, string>> _allSettings;
2525

2626
public string JsonFile { get; protected set; }
27-
public bool IgnoreMissingFile { get; protected set; }
27+
public bool Optional { get; protected set; }
2828
public SimpleJsonConfigBuilderMode JsonMode { get; protected set; } = SimpleJsonConfigBuilderMode.Flat; // Flat dictionary, like core secrets.json
2929

3030
public override void Initialize(string name, NameValueCollection config)
@@ -33,9 +33,9 @@ public override void Initialize(string name, NameValueCollection config)
3333

3434
_allSettings = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
3535

36-
// IgnoreMissingFile
37-
bool ignoreMissing;
38-
IgnoreMissingFile = (Boolean.TryParse(config?[ignoreMissingFileTag], out ignoreMissing)) ? ignoreMissing : true;
36+
// Optional
37+
bool optional;
38+
Optional = (Boolean.TryParse(config?[optionalTag], out optional)) ? optional : true;
3939

4040
// JsonFile
4141
string jsonFile = config?[jsonFileTag];
@@ -46,7 +46,7 @@ public override void Initialize(string name, NameValueCollection config)
4646
JsonFile = Utils.MapPath(jsonFile);
4747
if (!File.Exists(JsonFile))
4848
{
49-
if (IgnoreMissingFile)
49+
if (Optional)
5050
{
5151
// This empty dictionary allows us to effectively no-op any attempt to get values.
5252
_allSettings[""] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

src/UserSecrets/UserSecretsConfigBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public override void Initialize(string name, NameValueCollection config)
2424
{
2525
base.Initialize(name, config);
2626

27-
bool ignoreMissing;
28-
Optional = (Boolean.TryParse(config?[optionalTag], out ignoreMissing)) ? ignoreMissing : true;
27+
bool optional;
28+
Optional = (Boolean.TryParse(config?[optionalTag], out optional)) ? optional : true;
2929

3030
// Explicit file reference takes precedence over an identifier.
3131
string secretsFile = config?[userSecretsFileTag];

test/Microsoft.Configuration.ConfigurationBuilders.Test/Test/SimpleJsonTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public SimpleJsonTests()
1818
// ======================================================================
1919
// - See Parameters section of BaseTests for examples
2020
// jsonFile
21-
// ignoreMissingFile
21+
// optional
2222
// jsonMode
2323

2424
// ======================================================================
@@ -40,6 +40,6 @@ public void SimpleJson_GetAllValues()
4040
// ======================================================================
4141
// Make sure various expected exceptions from SimpleJson contain the name of the builder
4242
// no file specified
43-
// can't find file and ignoreMissingFile is false
43+
// can't find file and optional is false
4444
}
4545
}

test/Microsoft.Configuration.ConfigurationBuilders.Test/Test/UserSecretsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public UserSecretsTests()
1919
// - See Parameters section of BaseTests for examples
2020
// userSecretsFile
2121
// userSecretsId
22-
// ignoreMissingFile
22+
// optional
2323
// Verify that userSecretsFile takes priority over userSecretsId when both are given
2424

2525

@@ -41,6 +41,6 @@ public void UserSecrets_GetAllValues()
4141
// ======================================================================
4242
// Make sure various expected exceptions from UserSecrets contain the name of the builder
4343
// No file AND no ID specified
44-
// File and/or ID specified, but can't be found and ignoreMissingFile = false
44+
// File and/or ID specified, but can't be found and optional = false
4545
}
4646
}

0 commit comments

Comments
 (0)