-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle number overflow during parsing of min/max values
- Loading branch information
Showing
6 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Globalization; | ||
|
||
namespace Microsoft.OpenApi.Readers.ParseNodes | ||
{ | ||
/// <summary> | ||
/// Useful tools to parse data | ||
/// </summary> | ||
internal class ParserHelper | ||
{ | ||
/// <summary> | ||
/// Parses decimal in invariant culture. | ||
/// If the decimal is too big or small, it returns the default value | ||
/// | ||
/// Note: sometimes developers put Double.MaxValue or Long.MaxValue as min/max values for numbers in json schema even if their numbers are not expected to be that big/small. | ||
/// As we have already released the library with Decimal type for Max/Min, let's not introduce the breaking change and just fallback to Decimal.Max / Min. This should satisfy almost every scenario. | ||
/// We can revisit this if somebody really needs to have double or long here. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static decimal ParseDecimalWithFallbackOnOverflow(string value, decimal defaultValue) | ||
{ | ||
try | ||
{ | ||
return decimal.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture); | ||
} | ||
catch (OverflowException) | ||
{ | ||
return defaultValue; | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
24 changes: 24 additions & 0 deletions
24
test/Microsoft.OpenApi.Readers.Tests/ParseNodes/ParserHelperTests.cs
This file contains 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,24 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Microsoft.OpenApi.Readers.ParseNodes; | ||
using Xunit; | ||
|
||
namespace Microsoft.OpenApi.Readers.Tests.ParseNodes | ||
{ | ||
[Collection("DefaultSettings")] | ||
public class ParserHelperTests | ||
{ | ||
[Fact] | ||
public void ParseDecimalWithFallbackOnOverflow_ReturnsParsedValue() | ||
{ | ||
Assert.Equal(23434, ParserHelper.ParseDecimalWithFallbackOnOverflow("23434", 10)); | ||
} | ||
|
||
[Fact] | ||
public void ParseDecimalWithFallbackOnOverflow_Overflows_ReturnsFallback() | ||
{ | ||
Assert.Equal(10, ParserHelper.ParseDecimalWithFallbackOnOverflow(double.MaxValue.ToString(), 10)); | ||
} | ||
} | ||
} |