-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEdmOperationImportOperationHandler.cs
More file actions
191 lines (163 loc) · 7.12 KB
/
EdmOperationImportOperationHandler.cs
File metadata and controls
191 lines (163 loc) · 7.12 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
// ------------------------------------------------------------
// 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.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
using Microsoft.OpenApi.OData.Generator;
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
using Microsoft.OpenApi.OData.Vocabulary.Core;
namespace Microsoft.OpenApi.OData.Operation
{
/// <summary>
/// Base class for <see cref="IEdmOperationImport"/>.
/// </summary>
internal abstract class EdmOperationImportOperationHandler : OperationHandler
{
/// <summary>
/// Initializes a new instance of <see cref="EdmOperationImportOperationHandler"/> class.
/// </summary>
/// <param name="document">The document to use to lookup references.</param>
protected EdmOperationImportOperationHandler(OpenApiDocument document):base(document)
{
}
private OperationRestrictionsType _operationRestriction;
/// <summary>
/// Gets the <see cref="IEdmOperationImport"/>.
/// </summary>
protected IEdmOperationImport EdmOperationImport { get; private set; }
/// <summary>
/// Gets the <see cref="IEdmOperationImport"/>.
/// </summary>
protected ODataOperationImportSegment OperationImportSegment { get; private set; }
/// <inheritdoc/>
protected override void Initialize(ODataContext context, ODataPath path)
{
base.Initialize(context, path);
OperationImportSegment = path.LastSegment as ODataOperationImportSegment;
EdmOperationImport = OperationImportSegment.OperationImport;
_operationRestriction = Context.Model.GetRecord<OperationRestrictionsType>(TargetPath, CapabilitiesConstants.OperationRestrictions);
var operationRestrictions = Context.Model.GetRecord<OperationRestrictionsType>(EdmOperationImport, CapabilitiesConstants.OperationRestrictions);
if (_operationRestriction == null)
{
_operationRestriction = operationRestrictions;
}
else
{
_operationRestriction.MergePropertiesIfNull(operationRestrictions);
}
}
/// <inheritdoc/>
protected override void SetBasicInfo(OpenApiOperation operation)
{
operation.Summary = "Invoke " + (EdmOperationImport.IsActionImport() ? "actionImport " : "functionImport ") + EdmOperationImport.Name;
operation.Description = Context.Model.GetDescriptionAnnotation(TargetPath) ?? Context.Model.GetDescriptionAnnotation(EdmOperationImport);
if (Context.Settings.EnableOperationId)
{
if (EdmOperationImport.IsActionImport())
{
operation.OperationId = "ActionImport." + EdmOperationImport.Name;
}
else
{
if (Context.Model.IsOperationImportOverload(EdmOperationImport))
{
operation.OperationId = "FunctionImport." + EdmOperationImport.Name + "-" + Path.LastSegment.GetPathHash(Context.Settings);
}
else
{
operation.OperationId = "FunctionImport." + EdmOperationImport.Name;
}
}
}
base.SetBasicInfo(operation);
}
/// <inheritdoc/>
protected override void SetResponses(OpenApiOperation operation)
{
// The responses object contains a name/value pair for the success case (HTTP response code 200)
// describing the structure of the success response by referencing an appropriate schema
// in the global schemas. In addition, it contains a default name/value pair for
// the OData error response referencing the global responses.
operation.Responses = Context.CreateResponses(EdmOperationImport, _document);
base.SetResponses(operation);
}
/// <inheritdoc/>
protected override void SetSecurity(OpenApiOperation operation)
{
if (_operationRestriction == null || _operationRestriction.Permissions == null)
{
return;
}
operation.Security = Context.CreateSecurityRequirements(_operationRestriction.Permissions, _document).ToList();
}
/// <inheritdoc/>
protected override void AppendCustomParameters(OpenApiOperation operation)
{
if (_operationRestriction == null)
{
return;
}
if (_operationRestriction.CustomHeaders != null)
{
AppendCustomParameters(operation, _operationRestriction.CustomHeaders, ParameterLocation.Header);
}
if (_operationRestriction.CustomQueryOptions != null)
{
AppendCustomParameters(operation, _operationRestriction.CustomQueryOptions, ParameterLocation.Query);
}
}
/// <inheritdoc/>
protected override void SetTags(OpenApiOperation operation)
{
var tag = CreateTag(EdmOperationImport);
tag.Extensions.Add(Constants.xMsTocType, new OpenApiAny("container"));
Context.AppendTag(tag);
operation.Tags ??= new HashSet<OpenApiTagReference>();
operation.Tags.Add(new OpenApiTagReference(tag.Name, _document));
base.SetTags(operation);
}
private static OpenApiTag CreateTag(IEdmOperationImport operationImport)
{
if (operationImport.EntitySet is IEdmPathExpression pathExpression)
{
return new OpenApiTag
{
Name = PathAsString(pathExpression.PathSegments)
};
}
return new OpenApiTag
{
Name = operationImport.Name
};
}
internal static string PathAsString(IEnumerable<string> path)
{
return string.Join("/", path);
}
/// <inheritdoc/>
protected override void SetExternalDocs(OpenApiOperation operation)
{
if (Context.Settings.ShowExternalDocs)
{
var externalDocs = Context.Model.GetLinkRecord(TargetPath, CustomLinkRel) ??
Context.Model.GetLinkRecord(EdmOperationImport, CustomLinkRel);
if (externalDocs != null)
{
operation.ExternalDocs = new OpenApiExternalDocs()
{
Description = CoreConstants.ExternalDocsDescription,
Url = externalDocs.Href
};
}
}
}
}
}