Skip to content

Commit 235be5c

Browse files
authored
Merge pull request #335 from Microsoft/perthcharern/fixTryParseCulture
Add CultureInfo.InvariantCulture in the TryParse's in CreateAny.
2 parents f1864c6 + 328c380 commit 235be5c

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Globalization;
56
using Microsoft.OpenApi.Any;
6-
using Microsoft.OpenApi.Exceptions;
77
using Microsoft.OpenApi.Readers.Exceptions;
88
using SharpYaml.Serialization;
99

@@ -52,28 +52,26 @@ public override IOpenApiAny CreateAny()
5252
return new OpenApiBoolean(false);
5353
}
5454

55-
if (int.TryParse(value, out var intValue))
55+
// The NumberStyles below are the default ones based on
56+
// https://docs.microsoft.com/en-us/dotnet/api/?view=netframework-4.7.2
57+
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
5658
{
5759
return new OpenApiInteger(intValue);
5860
}
5961

60-
if (long.TryParse(value, out var longValue))
62+
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
6163
{
62-
return
63-
new OpenApiLong(
64-
longValue);
64+
return new OpenApiLong(longValue);
6565
}
6666

67-
if (double.TryParse(value, out var dblValue))
67+
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
6868
{
69-
return
70-
new OpenApiDouble(
71-
dblValue); // Note(darrmi): This may be better as decimal. Further investigation required.
69+
return new OpenApiDouble(doubleValue);
7270
}
7371

74-
if (DateTimeOffset.TryParse(value, out var datetimeValue))
72+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
7573
{
76-
return new OpenApiDateTime(datetimeValue);
74+
return new OpenApiDateTime(dateTimeValue);
7775
}
7876

7977
// if we can't identify the type of value, return it as string.

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiDocumentTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using System.Globalization;
66
using System.Threading;
77
using FluentAssertions;
8+
using Microsoft.OpenApi.Any;
89
using Microsoft.OpenApi.Exceptions;
910
using Microsoft.OpenApi.Models;
10-
using Microsoft.OpenApi.Readers.Exceptions;
1111
using Xunit;
1212

1313
namespace Microsoft.OpenApi.Readers.Tests.V2Tests
@@ -86,6 +86,7 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture)
8686
info:
8787
title: Simple Document
8888
version: 0.9.1
89+
x-extension: 2.335
8990
definitions:
9091
sampleSchema:
9192
type: object
@@ -105,7 +106,11 @@ public void ParseDocumentWithDifferentCultureShouldSucceed(string culture)
105106
Info = new OpenApiInfo
106107
{
107108
Title = "Simple Document",
108-
Version = "0.9.1"
109+
Version = "0.9.1",
110+
Extensions =
111+
{
112+
["x-extension"] = new OpenApiDouble(2.335)
113+
}
109114
},
110115
Components = new OpenApiComponents()
111116
{

0 commit comments

Comments
 (0)