Skip to content

Commit

Permalink
-Fixed not preventing negative integers with StringEnumConverter.Allo…
Browse files Browse the repository at this point in the history
…wIntegerValues
  • Loading branch information
JamesNK committed Jun 11, 2017
1 parent 64a092e commit 4467bdb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
23 changes: 22 additions & 1 deletion Src/Newtonsoft.Json.Tests/Converters/StringEnumConverterTests.cs
Expand Up @@ -586,6 +586,10 @@ public void EnumMemberWithNumbers()
e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"3\"", converter);

Assert.AreEqual(NumberNamesEnum.third, e);

e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"-4\"", converter);

Assert.AreEqual(NumberNamesEnum.fourth, e);
}

[Test]
Expand All @@ -604,6 +608,10 @@ public void EnumMemberWithNumbers_NoIntegerValues()
e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"3\"", converter);

Assert.AreEqual(NumberNamesEnum.third, e);

e = JsonConvert.DeserializeObject<NumberNamesEnum>("\"-4\"", converter);

Assert.AreEqual(NumberNamesEnum.fourth, e);
}
#endif

Expand All @@ -618,6 +626,17 @@ public void AllowIntegerValueAndStringNumber()
Assert.AreEqual("Integer string '1' is not allowed.", ex.InnerException.Message);
}

[Test]
public void AllowIntegerValueAndNegativeStringNumber()
{
JsonSerializationException ex = ExceptionAssert.Throws<JsonSerializationException>(() =>
{
JsonConvert.DeserializeObject<StoreColor>("\"-1\"", new StringEnumConverter { AllowIntegerValues = false });
});

Assert.AreEqual("Integer string '-1' is not allowed.", ex.InnerException.Message);
}

[Test]
public void AllowIntegerValueAndNonNamedValue()
{
Expand Down Expand Up @@ -714,7 +733,9 @@ public enum NumberNamesEnum
[EnumMember(Value = "1")]
second,
[EnumMember(Value = "3")]
third
third,
[EnumMember(Value = "-4")]
fourth
}

[DataContract]
Expand Down
10 changes: 1 addition & 9 deletions Src/Newtonsoft.Json/Utilities/EnumUtils.cs
Expand Up @@ -221,15 +221,7 @@ public static object ParseEnumName(string enumText, bool isNullable, bool disall

if (disallowValue)
{
bool isNumber = true;
for (int i = 0; i < finalEnumText.Length; i++)
{
if (!char.IsNumber(finalEnumText[i]))
{
isNumber = false;
break;
}
}
bool isNumber = int.TryParse(finalEnumText, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out int _);

if (isNumber)
{
Expand Down

0 comments on commit 4467bdb

Please sign in to comment.