-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathMapNode.cs
More file actions
210 lines (178 loc) · 6.77 KB
/
MapNode.cs
File metadata and controls
210 lines (178 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
namespace Microsoft.OpenApi.Reader.ParseNodes
{
/// <summary>
/// Abstraction of a Map to isolate semantic parsing from details of JSON DOM
/// </summary>
internal class MapNode : ParseNode, IEnumerable<PropertyNode>
{
private readonly JsonObject _node;
private readonly List<PropertyNode> _nodes;
public MapNode(ParsingContext context, JsonNode node) : base(
context, node)
{
if (node is not JsonObject mapNode)
{
throw new OpenApiReaderException("Expected map.", Context);
}
_node = mapNode;
_nodes = _node.Where(static p => p.Value is not null).Select(p => new PropertyNode(Context, p.Key, p.Value)).ToList();
}
public PropertyNode this[string key]
{
get
{
if (_node.TryGetPropertyValue(key, out var node))
{
return new(Context, key, node);
}
return null;
}
}
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument)
{
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(T).Name}", Context);
var nodes = jsonMap.Select(
n =>
{
var key = n.Key;
T value;
try
{
Context.StartObject(key);
value = n.Value is JsonObject jsonObject
? map(new MapNode(Context, jsonObject), hostDocument)
: default;
}
finally
{
Context.EndObject();
}
return new
{
key,
value
};
});
return nodes.ToDictionary(k => k.key, v => v.value);
}
public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
{
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(T).Name}", Context);
var nodes = jsonMap.Select(
n =>
{
var key = n.Key;
try
{
Context.StartObject(key);
JsonValue valueNode = n.Value is JsonValue value ? value
: throw new OpenApiReaderException($"Expected scalar while parsing {typeof(T).Name}", Context);
return (key, value: map(new ValueNode(Context, valueNode)));
}
finally
{
Context.EndObject();
}
});
return nodes.ToDictionary(k => k.key, v => v.value);
}
public override Dictionary<string, ISet<T>> CreateArrayMap<T>(Func<ValueNode, OpenApiDocument, T> map, OpenApiDocument openApiDocument)
{
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(T).Name}", Context);
var nodes = jsonMap.Select(n =>
{
var key = n.Key;
try
{
Context.StartObject(key);
JsonArray arrayNode = n.Value is JsonArray value
? value
: throw new OpenApiReaderException($"Expected array while parsing {typeof(T).Name}", Context);
ISet<T> values = new HashSet<T>(arrayNode.Select(item => map(new ValueNode(Context, item), openApiDocument)));
return (key, values);
}
finally
{
Context.EndObject();
}
});
return nodes.ToDictionary(kvp => kvp.key, kvp => kvp.values);
}
public IEnumerator<PropertyNode> GetEnumerator()
{
return _nodes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _nodes.GetEnumerator();
}
public override string GetRaw()
{
var x = JsonSerializer.Serialize(_node, SourceGenerationContext.Default.JsonObject);
return x;
}
public T GetReferencedObject<T>(ReferenceType referenceType, string referenceId, string summary = null, string description = null)
where T : IOpenApiReferenceHolder, new()
{
return new()
{
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId, referenceType, summary, description)
};
}
public string GetReferencePointer()
{
if (!_node.TryGetPropertyValue("$ref", out JsonNode refNode))
{
return null;
}
return refNode.GetScalarValue();
}
public string GetSummaryValue()
{
if (!_node.TryGetPropertyValue("summary", out JsonNode summaryNode))
{
return null;
}
return summaryNode.GetScalarValue();
}
public string GetDescriptionValue()
{
if (!_node.TryGetPropertyValue("description", out JsonNode descriptionNode))
{
return null;
}
return descriptionNode.GetScalarValue();
}
public string GetScalarValue(ValueNode key)
{
var scalarNode = _node[key.GetScalarValue()] is JsonValue jsonValue
? jsonValue
: throw new OpenApiReaderException($"Expected scalar while parsing {key.GetScalarValue()}", Context);
return Convert.ToString(scalarNode?.GetValue<object>(), CultureInfo.InvariantCulture);
}
/// <summary>
/// Create an <see cref="OpenApiAny"/>
/// </summary>
/// <returns>The created Json object.</returns>
public override JsonNode CreateAny()
{
return _node;
}
}
[JsonSerializable(typeof(JsonObject))]
internal partial class SourceGenerationContext : JsonSerializerContext { }
}