Skip to content

Commit e9a146b

Browse files
committed
- Add comments.
- Fix culture. - Move Field Map parameters out to their own files.
1 parent eee2f60 commit e9a146b

File tree

7 files changed

+102
-65
lines changed

7 files changed

+102
-65
lines changed
Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.Collections.Generic;
6-
using Microsoft.OpenApi.Any;
7-
using Microsoft.OpenApi.Models;
85

96
namespace Microsoft.OpenApi.Readers.ParseNodes
107
{
118
internal class AnyFieldMap<T> : Dictionary<string, AnyFieldMapParameter<T>>
129
{
13-
14-
}
15-
16-
internal class AnyFieldMapParameter<T>
17-
{
18-
public AnyFieldMapParameter(
19-
Func<T, IOpenApiAny> propertyGetter,
20-
Func<T, IOpenApiAny, IOpenApiAny> propertySetter,
21-
Func<T, OpenApiSchema> schemaGetter)
22-
{
23-
this.PropertyGetter = propertyGetter;
24-
this.PropertySetter = propertySetter;
25-
this.SchemaGetter = schemaGetter;
26-
}
27-
28-
public Func<T, IOpenApiAny> PropertyGetter { get; }
29-
30-
public Func<T, IOpenApiAny, IOpenApiAny> PropertySetter { get; }
31-
32-
public Func<T, OpenApiSchema> SchemaGetter { get; }
3310
}
3411
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using Microsoft.OpenApi.Any;
6+
using Microsoft.OpenApi.Models;
7+
8+
namespace Microsoft.OpenApi.Readers.ParseNodes
9+
{
10+
internal class AnyFieldMapParameter<T>
11+
{
12+
/// <summary>
13+
/// Constructor.
14+
/// </summary>
15+
public AnyFieldMapParameter(
16+
Func<T, IOpenApiAny> propertyGetter,
17+
Func<T, IOpenApiAny, IOpenApiAny> propertySetter,
18+
Func<T, OpenApiSchema> schemaGetter)
19+
{
20+
this.PropertyGetter = propertyGetter;
21+
this.PropertySetter = propertySetter;
22+
this.SchemaGetter = schemaGetter;
23+
}
24+
25+
/// <summary>
26+
/// Function to retrieve the value of the property.
27+
/// </summary>
28+
public Func<T, IOpenApiAny> PropertyGetter { get; }
29+
30+
/// <summary>
31+
/// Function to set the value of the property.
32+
/// </summary>
33+
public Func<T, IOpenApiAny, IOpenApiAny> PropertySetter { get; }
34+
35+
/// <summary>
36+
/// Function to get the schema to apply to the property.
37+
/// </summary>
38+
public Func<T, OpenApiSchema> SchemaGetter { get; }
39+
}
40+
}
Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
using System;
54
using System.Collections.Generic;
6-
using Microsoft.OpenApi.Any;
7-
using Microsoft.OpenApi.Models;
85

96
namespace Microsoft.OpenApi.Readers.ParseNodes
107
{
118
internal class AnyListFieldMap<T> : Dictionary<string, AnyListFieldMapParameter<T>>
129
{
1310

1411
}
15-
16-
internal class AnyListFieldMapParameter<T>
17-
{
18-
public AnyListFieldMapParameter(
19-
Func<T, IList<IOpenApiAny>> propertyGetter,
20-
Func<T, IList<IOpenApiAny>, IList<IOpenApiAny>> propertySetter,
21-
Func<T, OpenApiSchema> schemaGetter)
22-
{
23-
this.PropertyGetter = propertyGetter;
24-
this.PropertySetter = propertySetter;
25-
this.SchemaGetter = schemaGetter;
26-
}
27-
28-
public Func<T, IList<IOpenApiAny>> PropertyGetter { get; }
29-
30-
public Func<T, IList<IOpenApiAny>, IList<IOpenApiAny>> PropertySetter { get; }
31-
32-
public Func<T, OpenApiSchema> SchemaGetter { get; }
33-
}
3412
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Microsoft.OpenApi.Any;
7+
using Microsoft.OpenApi.Models;
8+
9+
namespace Microsoft.OpenApi.Readers.ParseNodes
10+
{
11+
internal class AnyListFieldMapParameter<T>
12+
{
13+
/// <summary>
14+
/// Constructor
15+
/// </summary>
16+
public AnyListFieldMapParameter(
17+
Func<T, IList<IOpenApiAny>> propertyGetter,
18+
Func<T, IList<IOpenApiAny>, IList<IOpenApiAny>> propertySetter,
19+
Func<T, OpenApiSchema> schemaGetter)
20+
{
21+
this.PropertyGetter = propertyGetter;
22+
this.PropertySetter = propertySetter;
23+
this.SchemaGetter = schemaGetter;
24+
}
25+
26+
/// <summary>
27+
/// Function to retrieve the value of the property.
28+
/// </summary>
29+
public Func<T, IList<IOpenApiAny>> PropertyGetter { get; }
30+
31+
/// <summary>
32+
/// Function to set the value of the property.
33+
/// </summary>
34+
public Func<T, IList<IOpenApiAny>, IList<IOpenApiAny>> PropertySetter { get; }
35+
36+
/// <summary>
37+
/// Function to get the schema to apply to the property.
38+
/// </summary>
39+
public Func<T, OpenApiSchema> SchemaGetter { get; }
40+
}
41+
}

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Globalization;
56
using System.Linq;
67
using Microsoft.OpenApi.Any;
78
using Microsoft.OpenApi.Exceptions;
@@ -62,22 +63,22 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny)
6263
return new OpenApiBoolean(false);
6364
}
6465

65-
if (int.TryParse(value, out var intValue))
66+
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
6667
{
6768
return new OpenApiInteger(intValue);
6869
}
6970

70-
if (long.TryParse(value, out var longValue))
71+
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
7172
{
7273
return new OpenApiLong(longValue);
7374
}
7475

75-
if (double.TryParse(value, out var doubleValue))
76+
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
7677
{
7778
return new OpenApiDouble(doubleValue);
7879
}
7980

80-
if (DateTimeOffset.TryParse(value, out var dateTimeValue))
81+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
8182
{
8283
return new OpenApiDateTime(dateTimeValue);
8384
}
@@ -146,7 +147,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
146147

147148
if (type == "integer" && format == "int32")
148149
{
149-
if (int.TryParse(value, out var intValue))
150+
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
150151
{
151152
return new OpenApiInteger(intValue);
152153
}
@@ -158,7 +159,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
158159

159160
if (type == "integer" && format == "int64")
160161
{
161-
if (long.TryParse(value, out var longValue))
162+
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
162163
{
163164
return new OpenApiLong(longValue);
164165
}
@@ -170,7 +171,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
170171

171172
if (type == "integer")
172173
{
173-
if (int.TryParse(value, out var intValue))
174+
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
174175
{
175176
return new OpenApiInteger(intValue);
176177
}
@@ -182,7 +183,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
182183

183184
if (type == "number" && format == "float")
184185
{
185-
if ( float.TryParse(value, out var floatValue))
186+
if (float.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var floatValue))
186187
{
187188
return new OpenApiFloat(floatValue);
188189
}
@@ -194,7 +195,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
194195

195196
if (type == "number" && format == "double" )
196197
{
197-
if (double.TryParse(value, out var doubleValue))
198+
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
198199
{
199200
return new OpenApiDouble(doubleValue);
200201
}
@@ -206,7 +207,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
206207

207208
if (type == "number")
208209
{
209-
if (double.TryParse(value, out var doubleValue))
210+
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
210211
{
211212
return new OpenApiDouble(doubleValue);
212213
}
@@ -218,7 +219,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
218219

219220
if (type == "string" && format == "byte")
220221
{
221-
if ( byte.TryParse(value, out var byteValue))
222+
if ( byte.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var byteValue))
222223
{
223224
return new OpenApiByte(byteValue);
224225
}
@@ -232,7 +233,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
232233

233234
if (type == "string" && format == "date")
234235
{
235-
if (DateTime.TryParse(value, out var dateValue))
236+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateValue))
236237
{
237238
return new OpenApiDate(dateValue.Date);
238239
}
@@ -244,7 +245,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
244245

245246
if (type == "string" && format == "date-time")
246247
{
247-
if (DateTime.TryParse(value, out var dateTimeValue))
248+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
248249
{
249250
return new OpenApiDateTime(dateTimeValue);
250251
}

src/Microsoft.OpenApi.Readers/V3/OpenApiSchemaDeserializer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,17 @@ public static OpenApiSchema LoadSchema(ParseNode node)
269269
};
270270
}
271271

272-
var domainObject = new OpenApiSchema();
272+
var schema = new OpenApiSchema();
273273

274274
foreach (var propertyNode in mapNode)
275275
{
276-
propertyNode.ParseField(domainObject, _schemaFixedFields, _schemaPatternFields);
276+
propertyNode.ParseField(schema, _schemaFixedFields, _schemaPatternFields);
277277
}
278278

279-
ProcessAnyFields(mapNode, domainObject, _schemaAnyFields);
280-
ProcessAnyListFields(mapNode, domainObject, _schemaAnyListFields);
279+
ProcessAnyFields(mapNode, schema, _schemaAnyFields);
280+
ProcessAnyListFields(mapNode, schema, _schemaAnyListFields);
281281

282-
return domainObject;
282+
return schema;
283283
}
284284
}
285285
}

test/Microsoft.OpenApi.Readers.Tests/ParseNodes/OpenApiAnyConverterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void ParseObjectAsAnyShouldSucceed()
8181
["aInteger"] = new OpenApiInteger(10),
8282
["aDouble"] = new OpenApiDouble(2.34),
8383
["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01", CultureInfo.InvariantCulture)),
84-
["aDate"] = new OpenApiDate(DateTime.Parse("2017-01-02", CultureInfo.InvariantCulture)),
84+
["aDate"] = new OpenApiDate(DateTimeOffset.Parse("2017-01-02", CultureInfo.InvariantCulture).Date),
8585
});
8686
}
8787

@@ -258,7 +258,7 @@ public void ParseNestedObjectAsAnyShouldSucceed()
258258
},
259259
["aObject"] = new OpenApiObject()
260260
{
261-
["aDate"] = new OpenApiDate(DateTime.Parse("2017-02-03", CultureInfo.InvariantCulture))
261+
["aDate"] = new OpenApiDate(DateTimeOffset.Parse("2017-02-03", CultureInfo.InvariantCulture).Date)
262262
},
263263
["aDouble"] = new OpenApiDouble(2.34),
264264
["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01", CultureInfo.InvariantCulture))

0 commit comments

Comments
 (0)