Skip to content

Commit fcfd82f

Browse files
committed
Code and test refactoring to implement the OpenApiAny JsonNode wrapper
1 parent e538742 commit fcfd82f

File tree

100 files changed

+550
-539
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+550
-539
lines changed

src/Microsoft.OpenApi.Readers/Exceptions/OpenApiReaderException.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ public OpenApiReaderException(string message, JsonNode node) : base(message)
4343
{
4444
// This only includes line because using a char range causes tests to break due to CR/LF & LF differences
4545
// See https://tools.ietf.org/html/rfc5147 for syntax
46-
//Pointer = $"#line={node.Start.Line}";
4746
}
48-
47+
4948
/// <summary>
5049
/// Initializes the <see cref="OpenApiReaderException"/> class with a custom message and inner exception.
5150
/// </summary>

src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using Microsoft.OpenApi.Any;
45
using Microsoft.OpenApi.Interfaces;
56
using Microsoft.OpenApi.Readers.Interface;
67
using Microsoft.OpenApi.Validations;
@@ -49,7 +50,7 @@ public class OpenApiReaderSettings
4950
/// <summary>
5051
/// Dictionary of parsers for converting extensions into strongly typed classes
5152
/// </summary>
52-
public Dictionary<string, Func<JsonNode, OpenApiSpecVersion, JsonNode>> ExtensionParsers { get; set; } = new Dictionary<string, Func<JsonNode, OpenApiSpecVersion, JsonNode>>();
53+
public Dictionary<string, Func<OpenApiAny, OpenApiSpecVersion, IOpenApiExtension>> ExtensionParsers { get; set; } = new Dictionary<string, Func<OpenApiAny, OpenApiSpecVersion, IOpenApiExtension>>();
5354

5455
/// <summary>
5556
/// Rules to use for validating OpenAPI specification. If none are provided a default set of rules are applied.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Text.Json.Nodes;
6+
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Models;
78

89
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -13,8 +14,8 @@ internal class AnyFieldMapParameter<T>
1314
/// Constructor.
1415
/// </summary>
1516
public AnyFieldMapParameter(
16-
Func<T, JsonNode> propertyGetter,
17-
Action<T, JsonNode> propertySetter,
17+
Func<T, OpenApiAny> propertyGetter,
18+
Action<T, OpenApiAny> propertySetter,
1819
Func<T, OpenApiSchema> schemaGetter)
1920
{
2021
this.PropertyGetter = propertyGetter;
@@ -25,12 +26,12 @@ public AnyFieldMapParameter(
2526
/// <summary>
2627
/// Function to retrieve the value of the property.
2728
/// </summary>
28-
public Func<T, JsonNode> PropertyGetter { get; }
29+
public Func<T, OpenApiAny> PropertyGetter { get; }
2930

3031
/// <summary>
3132
/// Function to set the value of the property.
3233
/// </summary>
33-
public Action<T, JsonNode> PropertySetter { get; }
34+
public Action<T, OpenApiAny> PropertySetter { get; }
3435

3536
/// <summary>
3637
/// Function to get the schema to apply to the property.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Text.Json.Nodes;
7+
using Microsoft.OpenApi.Any;
78
using Microsoft.OpenApi.Models;
89

910
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -14,8 +15,8 @@ internal class AnyListFieldMapParameter<T>
1415
/// Constructor
1516
/// </summary>
1617
public AnyListFieldMapParameter(
17-
Func<T, IList<JsonNode>> propertyGetter,
18-
Action<T, IList<JsonNode>> propertySetter,
18+
Func<T, IList<OpenApiAny>> propertyGetter,
19+
Action<T, IList<OpenApiAny>> propertySetter,
1920
Func<T, OpenApiSchema> schemaGetter)
2021
{
2122
this.PropertyGetter = propertyGetter;
@@ -26,12 +27,12 @@ public AnyListFieldMapParameter(
2627
/// <summary>
2728
/// Function to retrieve the value of the property.
2829
/// </summary>
29-
public Func<T, IList<JsonNode>> PropertyGetter { get; }
30+
public Func<T, IList<OpenApiAny>> PropertyGetter { get; }
3031

3132
/// <summary>
3233
/// Function to set the value of the property.
3334
/// </summary>
34-
public Action<T, IList<JsonNode>> PropertySetter { get; }
35+
public Action<T, IList<OpenApiAny>> PropertySetter { get; }
3536

3637
/// <summary>
3738
/// Function to get the schema to apply to the property.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Text.Json.Nodes;
7+
using Microsoft.OpenApi.Any;
78
using Microsoft.OpenApi.Interfaces;
89
using Microsoft.OpenApi.Models;
910

@@ -16,8 +17,8 @@ internal class AnyMapFieldMapParameter<T, U>
1617
/// </summary>
1718
public AnyMapFieldMapParameter(
1819
Func<T, IDictionary<string, U>> propertyMapGetter,
19-
Func<U, JsonNode> propertyGetter,
20-
Action<U, JsonNode> propertySetter,
20+
Func<U, OpenApiAny> propertyGetter,
21+
Action<U, OpenApiAny> propertySetter,
2122
Func<T, OpenApiSchema> schemaGetter)
2223
{
2324
this.PropertyMapGetter = propertyMapGetter;
@@ -34,12 +35,12 @@ public AnyMapFieldMapParameter(
3435
/// <summary>
3536
/// Function to retrieve the value of the property from an inner element.
3637
/// </summary>
37-
public Func<U, JsonNode> PropertyGetter { get; }
38+
public Func<U, OpenApiAny> PropertyGetter { get; }
3839

3940
/// <summary>
4041
/// Function to set the value of the property.
4142
/// </summary>
42-
public Action<U, JsonNode> PropertySetter { get; }
43+
public Action<U, OpenApiAny> PropertySetter { get; }
4344

4445
/// <summary>
4546
/// Function to get the schema to apply to the property.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Text.Json.Nodes;
9+
using Microsoft.OpenApi.Any;
910

1011
namespace Microsoft.OpenApi.Readers.ParseNodes
1112
{
@@ -31,7 +32,7 @@ public override List<T> CreateList<T>(Func<MapNode, T> map)
3132
.ToList();
3233
}
3334

34-
public override List<JsonNode> CreateListOfAny()
35+
public override List<OpenApiAny> CreateListOfAny()
3536
{
3637
return _nodeList.Select(n => Create(Context, n).CreateAny())
3738
.Where(i => i != null)
@@ -62,15 +63,15 @@ IEnumerator IEnumerable.GetEnumerator()
6263
/// Create a <see cref="JsonArray"/>
6364
/// </summary>
6465
/// <returns>The created Any object.</returns>
65-
public override JsonNode CreateAny()
66+
public override OpenApiAny CreateAny()
6667
{
6768
var array = new JsonArray();
6869
foreach (var node in this)
6970
{
70-
array.Add(node.CreateAny());
71+
array.Add(node.CreateAny().Node);
7172
}
72-
73-
return array;
73+
74+
return new OpenApiAny(array);
7475
}
7576
}
7677
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Text.Json;
99
using System.Text.Json.Nodes;
10+
using Microsoft.OpenApi.Any;
1011
using Microsoft.OpenApi.Interfaces;
1112
using Microsoft.OpenApi.Models;
1213
using Microsoft.OpenApi.Readers.Exceptions;
@@ -197,15 +198,16 @@ public string GetScalarValue(ValueNode key)
197198
/// Create a <see cref="JsonObject"/>
198199
/// </summary>
199200
/// <returns>The created Json object.</returns>
200-
public override JsonNode CreateAny()
201+
public override OpenApiAny CreateAny()
201202
{
202203
var apiObject = new JsonObject();
203204
foreach (var node in this)
204205
{
205-
apiObject.Add(node.Name, node.Value.CreateAny());
206+
var jsonNode = node.Value.CreateAny().Node;
207+
apiObject.Add(node.Name, jsonNode);
206208
}
207209

208-
return apiObject;
210+
return new OpenApiAny(apiObject);
209211
}
210212
}
211213
}

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Text;
77
using System.Text.Json;
88
using System.Text.Json.Nodes;
9-
using System.Xml.Linq;
109
using Microsoft.OpenApi.Models;
1110

1211
namespace Microsoft.OpenApi.Readers.ParseNodes
@@ -21,14 +20,18 @@ internal static class OpenApiAnyConverter
2120
/// </summary>
2221
public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema schema = null)
2322
{
23+
if(jsonNode == null)
24+
{
25+
return jsonNode;
26+
}
2427
if (jsonNode is JsonArray jsonArray)
2528
{
2629
var newArray = new JsonArray();
2730
foreach (var element in jsonArray)
2831
{
2932
if(element.Parent != null)
3033
{
31-
var newNode = element.Deserialize<JsonNode>();
34+
var newNode = element;
3235
newArray.Add(GetSpecificOpenApiAny(newNode, schema?.Items));
3336

3437
}
@@ -50,7 +53,7 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
5053
{
5154
if (jsonObject[property.Key].Parent != null)
5255
{
53-
var node = jsonObject[property.Key].Deserialize<JsonNode>();
56+
var node = jsonObject[property.Key];
5457
newObject.Add(property.Key, GetSpecificOpenApiAny(node, propertySchema));
5558
}
5659
else
@@ -84,8 +87,10 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
8487
var value = jsonValue.GetScalarValue();
8588
var type = schema?.Type;
8689
var format = schema?.Format;
90+
//var jsonElement = JsonSerializer.Deserialize<JsonElement>(value);
91+
var valueType = value.GetType();
8792

88-
if (value.Contains("\""))
93+
if (jsonValue.ToJsonString().StartsWith("\""))
8994
{
9095
// More narrow type detection for explicit strings, only check types that are passed as strings
9196
if (schema == null)
@@ -141,7 +146,7 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
141146
}
142147
}
143148

144-
return jsonNode;
149+
return value;
145150
}
146151

147152
if (value == null || value == "null")
@@ -273,10 +278,10 @@ public static JsonNode GetSpecificOpenApiAny(JsonNode jsonNode, OpenApiSchema sc
273278
return value;
274279
}
275280

276-
if (type == "string")
277-
{
278-
return value;
279-
}
281+
//if (type == "string")
282+
//{
283+
// return new OpenApiAny(value);
284+
//}
280285

281286
if (type == "boolean")
282287
{

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Text.Json.Nodes;
7+
using Microsoft.OpenApi.Any;
78
using Microsoft.OpenApi.Interfaces;
89
using Microsoft.OpenApi.Models;
910
using Microsoft.OpenApi.Readers.Exceptions;
@@ -72,7 +73,7 @@ public virtual Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
7273
throw new OpenApiReaderException("Cannot create simple map from this type of node.", Context);
7374
}
7475

75-
public virtual JsonNode CreateAny()
76+
public virtual OpenApiAny CreateAny()
7677
{
7778
throw new OpenApiReaderException("Cannot create an Any object this type of node.", Context);
7879
}
@@ -87,7 +88,7 @@ public virtual string GetScalarValue()
8788
throw new OpenApiReaderException("Cannot create a scalar value from this type of node.", Context);
8889
}
8990

90-
public virtual List<JsonNode> CreateListOfAny()
91+
public virtual List<OpenApiAny> CreateListOfAny()
9192
{
9293
throw new OpenApiReaderException("Cannot create a list from this type of node.", Context);
9394
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Text.Json.Nodes;
8+
using Microsoft.OpenApi.Any;
89
using Microsoft.OpenApi.Exceptions;
910
using Microsoft.OpenApi.Models;
1011
using Microsoft.OpenApi.Readers.Exceptions;
@@ -85,7 +86,7 @@ public void ParseField<T>(
8586
}
8687
}
8788

88-
public override JsonNode CreateAny()
89+
public override OpenApiAny CreateAny()
8990
{
9091
throw new NotImplementedException();
9192
}

0 commit comments

Comments
 (0)