Skip to content

Commit

Permalink
* Look for appsettings.json (netcore webapp convention)
Browse files Browse the repository at this point in the history
* Ensure XML config parser only looks for keyvalues inside appSettings and connectionStrings elements.
* Gracefully ignore elements with key attribute but not value attribute.
  • Loading branch information
smithrobs committed Dec 7, 2016
1 parent 6096405 commit 4e9ffc9
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,6 @@ FakesAssemblies/
web.config
app.config
settings.json
appsettings.json
Nexmo.Api.Test.Unit/private.key
project.lock.json
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.1.2 (2016-12-07)

* Look for ```appsettings.json``` (netcore webapp convention)
* Ensure XML config parser only looks for keyvalues inside ```<appSettings>``` and ```<connectionStrings>``` elements.
* Gracefully ignore elements with key attribute but not value attribute.

# 2.1.1 (2016-12-06)

* Look for ```<executing process>.exe.config``` file for XML configuration.
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions Nexmo.Api/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private Configuration()
.AddConfigFile("app.config", true)
.AddConfigFile($"{System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName)}.config", true)
.AddJsonFile("settings.json", true, true)
.AddJsonFile("appsettings.json", true, true)
;

Settings = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ConfigFileConfigurationSource(string configuration, bool loadFromFile, bo

var parsersToUse = new List<IConfigurationParser> {
new KeyValueParser(),
new KeyValueParser("name", "connectionString")
new KeyValueParser("connectionStrings", "name", "connectionString")
};

parsersToUse.AddRange(parsers);
Expand Down
49 changes: 27 additions & 22 deletions Nexmo.Api/ConfigurationExtensions/KeyValueParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,25 @@ namespace Nexmo.Api.ConfigurationExtensions
public class KeyValueParser : IConfigurationParser
{
private readonly ILogger _logger;
private readonly string _parentElementName = "appSettings";
private readonly string _keyName = "key";
private readonly string _valueName = "value";
private readonly string[] _supportedActions = Enum.GetNames(typeof(ConfigurationAction)).Select(x => x.ToLowerInvariant()).ToArray();

public KeyValueParser()
: this("key", "value")
: this("appSettings", "key", "value")
{ }

/// <summary>
/// The key/value attribute names.
/// </summary>
public KeyValueParser(string key, string value)
: this(key, value, null)
public KeyValueParser(string parentElementName, string key, string value)
: this(parentElementName, key, value, null)
{ }

public KeyValueParser(string key, string value, ILogger logger)
public KeyValueParser(string parentElementName, string key, string value, ILogger logger)
{
_parentElementName = parentElementName;
_keyName = key;
_valueName = value;
_logger = logger;
Expand Down Expand Up @@ -93,10 +95,7 @@ public void ParseElement(XElement element, Stack<string> context, SortedDictiona

if (!hasSupportedAction)
{
if (_logger != null)
{
_logger.LogWarning($"Contains an unsupported config element. [{node.ToString()}]");
}
_logger?.LogWarning($"Contains an unsupported config element. [{node.ToString()}]");

continue;
}
Expand All @@ -111,12 +110,24 @@ private void AddToDictionary(XElement element, Stack<string> context, SortedDict
{
ConfigurationAction action;

// ensure parent element matches
if (element.Parent == null && !string.IsNullOrEmpty(_parentElementName))
{
_logger?.LogInformation($"No parent element, but expected parent element. [{element.ToString()}]");

return;
}
if (element.Parent != null && element.Parent.Name.ToString() != _parentElementName)
{
_logger?.LogInformation($"Matched element with an unsupported parent. [{element.ToString()}]");

return;
}

// matched, ensure we support action
if (!Enum.TryParse(element.Name.ToString(), true, out action))
{
if (_logger != null)
{
_logger.LogInformation($"Element with an unsupported action. [{element.ToString()}]");
}
_logger?.LogInformation($"Element with an unsupported action. [{element.ToString()}]");

return;
}
Expand All @@ -126,10 +137,7 @@ private void AddToDictionary(XElement element, Stack<string> context, SortedDict

if (key == null)
{
if (_logger != null)
{
_logger.LogInformation($"[{element.ToString()}] is not supported because it does not have an attribute with {_keyName}");
}
_logger?.LogInformation($"[{element.ToString()}] is not supported because it does not have an attribute with {_keyName}");

return;
}
Expand All @@ -141,9 +149,9 @@ private void AddToDictionary(XElement element, Stack<string> context, SortedDict
case ConfigurationAction.Add:
string valueToAdd = null;

if (value == null && _logger != null)
if (value == null)
{
_logger.LogWarning($"Could not parse the value attribute [{_valueName}] from [{element.ToString()}]. Using null as value...");
_logger?.LogWarning($"Could not parse the value attribute [{_valueName}] from [{element.ToString()}]. Using null as value...");
}
else
{
Expand All @@ -152,10 +160,7 @@ private void AddToDictionary(XElement element, Stack<string> context, SortedDict

if (results.ContainsKey(fullkey))
{
if (_logger != null)
{
_logger.LogWarning($"{fullkey} exists. Replacing existing value [{results[fullkey]}] with {valueToAdd}");
}
_logger?.LogWarning($"{fullkey} exists. Replacing existing value [{results[fullkey]}] with {valueToAdd}");

results[fullkey] = valueToAdd;
}
Expand Down
2 changes: 1 addition & 1 deletion Nexmo.Api/Nexmo.Api.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Nexmo.Csharp.Client</id>
<version>2.1.1</version>
<version>2.1.2</version>
<title>Nexmo API Client</title>
<authors>Nexmo</authors>
<owners>Nexmo</owners>
Expand Down
4 changes: 2 additions & 2 deletions Nexmo.Api/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.1.0")]
[assembly: AssemblyVersion("2.1.2.0")]
[assembly: AssemblyFileVersion("2.1.2.0")]
2 changes: 1 addition & 1 deletion Nexmo.Api/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.1.1-*",
"version": "2.1.2-*",

"dependencies": {
"Microsoft.Extensions.Configuration": "1.0.*",
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ either including them with your project's NuGet dependencies or manually referen

Configuration:
--------------
* Provide the nexmo URLs, API key, secret, and application credentials (for JWT) in ```settings.json```:
* Provide the nexmo URLs, API key, secret, and application credentials (for JWT) in ```appsettings.json```:

```json
{
Expand Down Expand Up @@ -62,6 +62,7 @@ Configuration:

* In the event multiple configuration files are found, the order of precedence is as follows:

* ```appsettings.json``` which overrides
* ```settings.json``` which overrides
* ```<executing process name>.config``` which overrides
* ```app.config``` which overrides
Expand Down Expand Up @@ -136,7 +137,7 @@ Call.Do(new Call.CallCommand
### Additional Examples

* Check out the sample MVC application and tests for more examples.
Make sure to copy settings.json.example/web.config.example to settings.json/web.config and enter your key/secret.
Make sure to copy appsettings.json.example/web.config.example to appsettings.json/web.config and enter your key/secret.

API Coverage
------------
Expand Down

0 comments on commit 4e9ffc9

Please sign in to comment.