-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathODataContext.cs
More file actions
220 lines (194 loc) · 7.9 KB
/
ODataContext.cs
File metadata and controls
220 lines (194 loc) · 7.9 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
211
212
213
214
215
216
217
218
219
220
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Generator;
using Microsoft.OpenApi.OData.Operation;
using Microsoft.OpenApi.OData.PathItem;
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
using Microsoft.OpenApi.OData.Vocabulary.Core;
namespace Microsoft.OpenApi.OData.Edm
{
/// <summary>
/// Context information for the <see cref="IEdmModel"/>, configuration, etc.
/// </summary>
internal class ODataContext
{
private IEnumerable<ODataPath>? _allPaths;
private readonly IODataPathProvider _pathProvider;
/// <summary>
/// Initializes a new instance of <see cref="ODataContext"/> class.
/// </summary>
/// <param name="model">The Edm model.</param>
public ODataContext(IEdmModel model)
: this(model, new OpenApiConvertSettings())
{
}
/// <summary>
/// Initializes a new instance of <see cref="ODataContext"/> class.
/// </summary>
/// <param name="model">The Edm model.</param>
/// <param name="settings">The convert setting.</param>
public ODataContext(IEdmModel model, OpenApiConvertSettings settings)
{
Model = model ?? throw Error.ArgumentNull(nameof(model));
Settings = settings ?? throw Error.ArgumentNull(nameof(settings));
EdmSpatialTypeVisitor visitor = new EdmSpatialTypeVisitor();
visitor.Visit(model);
IsSpatialTypeUsed = visitor.IsSpatialTypeUsed;
OperationHandlerProvider = new OperationHandlerProvider();
PathItemHandlerProvider = new PathItemHandlerProvider();
// If no path provider, use the default path provider.
_pathProvider = settings.PathProvider ?? new ODataPathProvider();
if (settings.EnableKeyAsSegment != null)
{
// We have the global setting, use the global setting
KeyAsSegment = settings.EnableKeyAsSegment.Value;
}
else
{
KeyAsSegment = false;
if (model.EntityContainer != null)
{
var keyAsSegment = model.GetBoolean(model.EntityContainer, CapabilitiesConstants.KeyAsSegmentSupported);
if (keyAsSegment != null)
{
KeyAsSegment = keyAsSegment.Value;
}
}
}
}
/// <summary>
/// Gets the path item handler provider.
/// </summary>
public IPathItemHandlerProvider PathItemHandlerProvider { get; }
/// <summary>
/// Gets the operation handler provider.
/// </summary>
public IOperationHandlerProvider OperationHandlerProvider { get; }
/// <summary>
/// Gets the Edm model.
/// </summary>
public IEdmModel Model { get; }
/// <summary>
/// Gets the Entity Container.
/// </summary>
public IEdmEntityContainer EntityContainer
{
get
{
return Model.EntityContainer;
}
}
/// <summary>
/// Gets the <see cref="ODataPath"/>s.
/// </summary>
public IEnumerable<ODataPath> AllPaths
{
get
{
if (_allPaths == null)
{
_allPaths = LoadAllODataPaths().ToList();
}
return _allPaths;
}
}
/// <summary>
/// Gets the boolean value indicating to support key as segment.
/// </summary>
public bool KeyAsSegment { get; }
/// <summary>
/// Gets the value indicating the Edm spatial type used.
/// </summary>
public bool IsSpatialTypeUsed { get; private set; }
/// <summary>
/// Gets the convert settings.
/// </summary>
public OpenApiConvertSettings Settings { get; }
/// <summary>
/// Gets all tags.
/// </summary>
public HashSet<OpenApiTag>? Tags { get; private set; }
/// <summary>
/// Append tag.
/// </summary>
/// <param name="tagItem">The tag item.</param>
internal void AppendTag(OpenApiTag tagItem)
{
Tags ??= [];
if (tagItem.Name is not null && FindTagByName(tagItem.Name) is not null)
{
return;
}
Tags.Add(tagItem);
}
/// <summary>
/// Find tag by name.
/// </summary>
/// <param name="name">The name to lookup the tag.</param>
/// <returns></returns>
internal OpenApiTag? FindTagByName(string name)
{
Utils.CheckArgumentNullOrEmpty(name, nameof(name));
return Tags?.FirstOrDefault(t => StringComparer.Ordinal.Equals(t.Name, name));
}
/// <summary>
/// Sets the extension for the existing tag, or create a new tag with the extension.
/// </summary>
/// <param name="tagName">The tag name to lookup.</param>
/// <param name="extensionName">The extension name.</param>
/// <param name="extensionValue">The extension value to set.</param>
/// <param name="initialValueFactory">The tag default value factory.</param>
internal void AddExtensionToTag(string tagName, string extensionName, JsonNodeExtension extensionValue, Func<OpenApiTag> initialValueFactory)
{
Utils.CheckArgumentNullOrEmpty(tagName, nameof(tagName));
Utils.CheckArgumentNullOrEmpty(extensionName, nameof(extensionName));
Utils.CheckArgumentNull(extensionValue, nameof(extensionValue));
Utils.CheckArgumentNull(initialValueFactory, nameof(initialValueFactory));
if (FindTagByName(tagName) is {} foundTag)
{
foundTag.Extensions ??= new Dictionary<string, IOpenApiExtension>();
foundTag.Extensions.TryAdd(extensionName, extensionValue);
}
else
{
var tag = initialValueFactory();
tag.Extensions ??= new Dictionary<string, IOpenApiExtension>();
tag.Extensions.TryAdd(extensionName, extensionValue);
AppendTag(tag);
}
}
/// <summary>
/// Gets all OData paths
/// </summary>
/// <returns>All acceptable OData path.</returns>
private IEnumerable<ODataPath> LoadAllODataPaths()
{
IEnumerable<ODataPath> allPaths = _pathProvider.GetPaths(Model, Settings);
foreach (var path in allPaths)
{
if ((path.Kind == ODataPathKind.Operation && !Settings.EnableOperationPath) ||
(path.Kind == ODataPathKind.OperationImport && !Settings.EnableOperationImportPath) ||
((path.Kind == ODataPathKind.NavigationProperty || path.Kind == ODataPathKind.ComplexProperty) && !Settings.EnableNavigationPropertyPath))
{
continue;
}
yield return path;
}
}
internal IEnumerable<RevisionRecord> GetDeprecationInformations(IEdmVocabularyAnnotatable annotable)
{
return annotable == null ?
Enumerable.Empty<RevisionRecord>() :
(Model?.GetCollection<RevisionRecord>(annotable, CoreConstants.Revisions)?.Where(x => x.Kind == RevisionKind.Deprecated) ??
Enumerable.Empty<RevisionRecord>());
}
}
}