forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperation.cs
261 lines (217 loc) · 9.66 KB
/
Operation.cs
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Linq;
using AutoRest.Swagger.Validation;
using System.Collections.Generic;
using AutoRest.Core.Utilities;
using AutoRest.Swagger.Validation.Core;
namespace AutoRest.Swagger.Model
{
/// <summary>
/// Describes a single API operation on a path.
/// </summary>
[Rule(typeof(OperationDescriptionRequired))]
public class Operation : SwaggerBase
{
private string _description;
private string _summary;
public Operation()
{
Consumes = new List<string>();
Produces = new List<string>();
}
/// <summary>
/// A list of tags for API documentation control.
/// </summary>
public IList<string> Tags { get; set; }
/// <summary>
/// A friendly serviceTypeName for the operation. The id MUST be unique among all
/// operations described in the API. Tools and libraries MAY use the
/// operation id to uniquely identify an operation.
/// </summary>
[Rule(typeof(OneUnderscoreInOperationId))]
[Rule(typeof(OperationIdNounInVerb))]
[Rule(typeof(GetOperationNameValidation))]
[Rule(typeof(PutOperationNameValidation))]
[Rule(typeof(PatchOperationNameValidation))]
[Rule(typeof(DeleteOperationNameValidation))]
public string OperationId { get; set; }
public string Summary
{
get { return _summary; }
set { _summary = value.StripControlCharacters(); }
}
[Rule(typeof(AvoidMsdnReferences))]
public string Description
{
get { return _description; }
set { _description = value.StripControlCharacters(); }
}
/// <summary>
/// Additional external documentation for this operation.
/// </summary>
public ExternalDoc ExternalDocs { get; set; }
/// <summary>
/// A list of MIME types the operation can consume.
/// </summary>
[CollectionRule(typeof(NonAppJsonTypeWarning))]
public IList<string> Consumes { get; set; }
/// <summary>
/// A list of MIME types the operation can produce.
/// </summary>
[CollectionRule(typeof(NonAppJsonTypeWarning))]
public IList<string> Produces { get; set; }
/// <summary>
/// A list of parameters that are applicable for this operation.
/// If a parameter is already defined at the Path Item, the
/// new definition will override it, but can never remove it.
/// </summary>
[CollectionRule(typeof(OperationParametersValidation))]
[CollectionRule(typeof(BooleanPropertyNotRecommended))]
[CollectionRule(typeof(AnonymousBodyParameter))]
public IList<SwaggerParameter> Parameters { get; set; }
/// <summary>
/// The list of possible responses as they are returned from executing this operation.
/// </summary>
public Dictionary<string, OperationResponse> Responses { get; set; }
/// <summary>
/// The transfer protocol for the operation.
/// </summary>
[CollectionRule(typeof(SupportedSchemesWarning))]
public IList<TransferProtocolScheme> Schemes { get; set; }
public bool Deprecated { get; set; }
/// <summary>
/// A declaration of which security schemes are applied for this operation.
/// The list of values describes alternative security schemes that can be used
/// (that is, there is a logical OR between the security requirements).
/// This definition overrides any declared top-level security. To remove a
/// top-level security declaration, an empty array can be used.
/// </summary>
public IList<Dictionary<string, List<string>>> Security { get; set; }
/// <summary>
/// Compare a modified document node (this) to a previous one and look for breaking as well as non-breaking changes.
/// </summary>
/// <param name="context">The modified document context.</param>
/// <param name="previous">The original document model.</param>
/// <returns>A list of messages from the comparison.</returns>
public override IEnumerable<ComparisonMessage> Compare(ComparisonContext context, SwaggerBase previous)
{
var priorOperation = previous as Operation;
var currentRoot = (context.CurrentRoot as ServiceDefinition);
var previousRoot = (context.PreviousRoot as ServiceDefinition);
if (priorOperation == null)
{
throw new ArgumentException("previous");
}
base.Compare(context, previous);
if (!OperationId.Equals(priorOperation.OperationId))
{
context.LogBreakingChange(ComparisonMessages.ModifiedOperationId);
}
CheckParameters(context, priorOperation);
if (Responses != null && priorOperation.Responses != null)
{
foreach (var response in Responses)
{
var oldResponse = priorOperation.FindResponse(response.Key, priorOperation.Responses);
context.PushProperty(response.Key);
if (oldResponse == null)
{
context.LogBreakingChange(ComparisonMessages.AddingResponseCode, response.Key);
}
else
{
response.Value.Compare(context, oldResponse);
}
context.Pop();
}
foreach (var response in priorOperation.Responses)
{
var newResponse = this.FindResponse(response.Key, this.Responses);
if (newResponse == null)
{
context.PushProperty(response.Key);
context.LogBreakingChange(ComparisonMessages.RemovedResponseCode, response.Key);
context.Pop();
}
}
}
return context.Messages;
}
private void CheckParameters(ComparisonContext context, Operation priorOperation)
{
// Check that no parameters were removed or reordered, and compare them if it's not the case.
var currentRoot = (context.CurrentRoot as ServiceDefinition);
var previousRoot = (context.PreviousRoot as ServiceDefinition);
foreach (var oldParam in priorOperation.Parameters
.Select(p => string.IsNullOrEmpty(p.Reference) ? p : FindReferencedParameter(p.Reference, previousRoot.Parameters)))
{
SwaggerParameter newParam = FindParameter(oldParam.Name, Parameters, currentRoot.Parameters);
context.PushProperty(oldParam.Name);
if (newParam != null)
{
newParam.Compare(context, oldParam);
}
else if (oldParam.IsRequired)
{
context.LogBreakingChange(ComparisonMessages.RemovedRequiredParameter, oldParam.Name);
}
context.Pop();
}
// Check that no required parameters were added.
foreach (var newParam in Parameters
.Select(p => string.IsNullOrEmpty(p.Reference) ? p : FindReferencedParameter(p.Reference, currentRoot.Parameters))
.Where(p => p != null && p.IsRequired))
{
if (newParam == null) continue;
SwaggerParameter oldParam = FindParameter(newParam.Name, priorOperation.Parameters, previousRoot.Parameters);
if (oldParam == null)
{
context.PushProperty(newParam.Name);
context.LogBreakingChange(ComparisonMessages.AddingRequiredParameter, newParam.Name);
context.Pop();
}
}
}
private SwaggerParameter FindParameter(string name, IEnumerable<SwaggerParameter> operationParameters, IDictionary<string, SwaggerParameter> clientParameters)
{
if (Parameters != null)
{
foreach (var param in operationParameters)
{
if (name.Equals(param.Name))
return param;
var pRef = FindReferencedParameter(param.Reference, clientParameters);
if (pRef != null && name.Equals(pRef.Name))
{
return pRef;
}
}
}
return null;
}
private OperationResponse FindResponse(string name, IDictionary<string, OperationResponse> responses)
{
OperationResponse response = null;
this.Responses.TryGetValue(name, out response);
return response;
}
private static SwaggerParameter FindReferencedParameter(string reference, IDictionary<string, SwaggerParameter> parameters)
{
if (reference != null && reference.StartsWith("#", StringComparison.Ordinal))
{
var parts = reference.Split('/');
if (parts.Length == 3 && parts[1].Equals("parameters"))
{
SwaggerParameter p = null;
if (parameters.TryGetValue(parts[2], out p))
{
return p;
}
}
}
return null;
}
}
}