Skip to content

Commit

Permalink
replace decimal.Parse with double.Parse to include support for parsin…
Browse files Browse the repository at this point in the history
…g values exceeding range of decimal
  • Loading branch information
Afsalmc committed Dec 13, 2022
1 parent ac02124 commit 1592710
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiHeaderDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand Down Expand Up @@ -57,7 +57,7 @@ internal static partial class OpenApiV2Deserializer
{
"maximum", (o, n) =>
{
GetOrCreateSchema(o).Maximum = decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
GetOrCreateSchema(o).Maximum = double.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
}
},
{
Expand All @@ -69,7 +69,7 @@ internal static partial class OpenApiV2Deserializer
{
"minimum", (o, n) =>
{
GetOrCreateSchema(o).Minimum = decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
GetOrCreateSchema(o).Minimum = double.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
}
},
{
Expand Down Expand Up @@ -117,7 +117,7 @@ internal static partial class OpenApiV2Deserializer
{
"multipleOf", (o, n) =>
{
GetOrCreateSchema(o).MultipleOf = decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
GetOrCreateSchema(o).MultipleOf = double.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ internal static partial class OpenApiV2Deserializer
{
"minimum", (o, n) =>
{
GetOrCreateSchema(o).Minimum = decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
GetOrCreateSchema(o).Minimum = double.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
}
},
{
"maximum", (o, n) =>
{
GetOrCreateSchema(o).Maximum = decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
GetOrCreateSchema(o).Maximum = double.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture);
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ internal static partial class OpenApiV2Deserializer
{
"multipleOf", (o, n) =>
{
o.MultipleOf = decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
o.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
}
},
{
"maximum", (o, n) =>
{
o.Maximum = decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
o.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
}
},
{
Expand All @@ -44,7 +44,7 @@ internal static partial class OpenApiV2Deserializer
{
"minimum", (o, n) =>
{
o.Minimum = decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
o.Minimum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi.Readers/V3/OpenApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ internal static partial class OpenApiV3Deserializer
{
"multipleOf", (o, n) =>
{
o.MultipleOf = decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
o.MultipleOf = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
}
},
{
"maximum", (o, n) =>
{
o.Maximum = decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
o.Maximum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
}
},
{
Expand All @@ -45,7 +45,7 @@ internal static partial class OpenApiV3Deserializer
{
"minimum", (o, n) =>
{
o.Minimum = decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
o.Minimum = double.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture);
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class OpenApiSchema : IOpenApiSerializable, IOpenApiReferenceable, IEffec
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public decimal? Maximum { get; set; }
public double? Maximum { get; set; }

/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
Expand All @@ -51,7 +51,7 @@ public class OpenApiSchema : IOpenApiSerializable, IOpenApiReferenceable, IEffec
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public decimal? Minimum { get; set; }
public double? Minimum { get; set; }

/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
Expand All @@ -77,7 +77,7 @@ public class OpenApiSchema : IOpenApiSerializable, IOpenApiReferenceable, IEffec
/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
public decimal? MultipleOf { get; set; }
public double? MultipleOf { get; set; }

/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Writers/IOpenApiWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public interface IOpenApiWriter
void WriteValue(string value);

/// <summary>
/// Write the decimal value.
/// Write the double value.
/// </summary>
void WriteValue(decimal value);
void WriteValue(double value);

/// <summary>
/// Write the int value.
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ public virtual void WriteValue(double value)
}

/// <summary>
/// Write decimal value.
/// Write
///
/// value.
/// </summary>
/// <param name="value">The decimal value.</param>
/// <param name="value">The double value.</param>
public virtual void WriteValue(decimal value)
{
WriteValueSeparator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ internal static string GetYamlCompatibleString(this string input)
return $"'{input}'";
}

// If string can be mistaken as a number, c-style hexadecimal notation, a boolean, or a timestamp,
// wrap it in quote to indicate that this is indeed a string, not a number, c-style hexadecimal notation, a boolean, or a timestamp
if (decimal.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out var _) ||
IsHexadecimalNotation(input) ||
// If string can be mistaken as a number, c-style hexa
//
// notation, a boolean, or a timestamp,
// wrap it in quote to indicate that this is indeed a string, not a number, c-style hexadouble notation, a boolean, or a timestamp
if (double.TryParse(input, NumberStyles.Float, CultureInfo.InvariantCulture, out var _) ||
IsHexadoubleNotation(input) ||
bool.TryParse(input, out var _) ||
DateTime.TryParse(input, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _))
{
Expand Down Expand Up @@ -227,7 +229,7 @@ internal static string GetJsonCompatibleString(this string value)
return $"\"{value}\"";
}

internal static bool IsHexadecimalNotation(string input)
internal static bool IsHexadoubleNotation(string input)
{
return input.StartsWith("0x") && int.TryParse(input.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var _);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture)
["sampleProperty"] = new OpenApiSchema()
{
Type = "double",
Minimum = (decimal)100.54,
Maximum = (decimal)60000000.35,
Minimum = (double)100.54,
Maximum = (double)60000000.35,
ExclusiveMaximum = true,
ExclusiveMinimum = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture)
["sampleProperty"] = new OpenApiSchema()
{
Type = "double",
Minimum = (decimal)100.54,
Maximum = (decimal)60000000.35,
Minimum = (double)100.54,
Maximum = (double)60000000.35,
ExclusiveMaximum = true,
ExclusiveMinimum = false
}
Expand Down
11 changes: 6 additions & 5 deletions test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,13 @@ namespace Microsoft.OpenApi.Models
public int? MaxItems { get; set; }
public int? MaxLength { get; set; }
public int? MaxProperties { get; set; }
public decimal? Maximum { get; set; }
public
? Maximum { get; set; }
public int? MinItems { get; set; }
public int? MinLength { get; set; }
public int? MinProperties { get; set; }
public decimal? Minimum { get; set; }
public decimal? MultipleOf { get; set; }
public double? Minimum { get; set; }
public double? MultipleOf { get; set; }
public Microsoft.OpenApi.Models.OpenApiSchema Not { get; set; }
public bool Nullable { get; set; }
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> OneOf { get; set; }
Expand Down Expand Up @@ -1404,7 +1405,7 @@ namespace Microsoft.OpenApi.Writers
void WriteStartArray();
void WriteStartObject();
void WriteValue(bool value);
void WriteValue(decimal value);
void WriteValue(double value);
void WriteValue(int value);
void WriteValue(object value);
void WriteValue(string value);
Expand Down Expand Up @@ -1467,7 +1468,7 @@ namespace Microsoft.OpenApi.Writers
public virtual void WriteValue(bool value) { }
public virtual void WriteValue(System.DateTime value) { }
public virtual void WriteValue(System.DateTimeOffset value) { }
public virtual void WriteValue(decimal value) { }
public virtual void WriteValue(double value) { }
public virtual void WriteValue(double value) { }
public virtual void WriteValue(int value) { }
public virtual void WriteValue(long value) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void WriteValueRecursive(OpenApiJsonWriter writer, object value)
{
if (value == null
|| value.GetType().IsPrimitive
|| value is decimal
|| value is double
|| value is string
|| value is DateTimeOffset
|| value is DateTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private void WriteValueRecursive(OpenApiYamlWriter writer, object value)
{
if (value == null
|| value.GetType().IsPrimitive
|| value is decimal
|| value is double
|| value is string
|| value is DateTimeOffset
|| value is DateTime)
Expand Down

0 comments on commit 1592710

Please sign in to comment.