forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceDefinition.cs
584 lines (508 loc) · 24 KB
/
ServiceDefinition.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// 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 System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using AutoRest.Core.Logging;
using AutoRest.Core.Utilities.Collections;
using AutoRest.Swagger.Validation;
using System.Text.RegularExpressions;
using AutoRest.Swagger.Validation.Core;
namespace AutoRest.Swagger.Model
{
/// <summary>
/// Class that represents Swagger 2.0 schema
/// http://json.schemastore.org/swagger-2.0
/// Swagger Object - https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#swagger-object-
/// </summary>
public class ServiceDefinition : SpecObject
{
// as long as AAAS does not provide that
public static ServiceDefinition Instance { get; set; }
public ServiceDefinition()
{
Definitions = new Dictionary<string, Schema>();
Schemes = new List<TransferProtocolScheme>();
Consumes = new List<string>();
Produces = new List<string>();
Paths = new Dictionary<string, Dictionary<string, Operation>>();
CustomPaths = new Dictionary<string, Dictionary<string, Operation>>();
Parameters = new Dictionary<string, SwaggerParameter>();
Responses = new Dictionary<string, OperationResponse>();
SecurityDefinitions = new Dictionary<string, SecurityDefinition>();
Security = new List<Dictionary<string, List<string>>>();
Tags = new List<Tag>();
}
/// <summary>
/// Specifies the Swagger Specification version being used.
/// </summary>
public string Swagger { get; set; }
/// <summary>
/// Provides metadata about the API. The metadata can be used by the clients if needed.
/// </summary>
public Info Info { get; set; }
/// <summary>
/// The host (serviceTypeName or ip) serving the API.
/// </summary>
public string Host { get; set; }
/// <summary>
/// The base path on which the API is served, which is relative to the host.
/// </summary>
public string BasePath { get; set; }
/// <summary>
/// The transfer protocol of the API.
/// </summary>
[CollectionRule(typeof(SupportedSchemesWarning))]
public IList<TransferProtocolScheme> Schemes { get; set; }
/// <summary>
/// A list of MIME types the service can consume.
/// </summary>
[CollectionRule(typeof(NonAppJsonTypeWarning))]
public IList<string> Consumes { get; set; }
/// <summary>
/// A list of MIME types the APIs can produce.
/// </summary>
[CollectionRule(typeof(NonAppJsonTypeWarning))]
public IList<string> Produces { get; set; }
/// <summary>
/// Key is actual path and the value is serializationProperty of http operations and operation objects.
/// </summary>
[Rule(typeof(UniqueResourcePaths))]
[Rule(typeof(ListOperationNamingWarning))]
[Rule(typeof(CollectionObjectPropertiesNamingValidation))]
[Rule(typeof(PutGetPatchResponseValidation))]
[Rule(typeof(OperationsAPIImplementationValidation))]
[Rule(typeof(ProvidersPathValidation))]
[Rule(typeof(PutResponseResourceValidation))]
[CollectionRule(typeof(BodyTopLevelProperties))]
[CollectionRule(typeof(HttpVerbValidation))]
[CollectionRule(typeof(DeleteMustNotHaveRequestBody))]
[CollectionRule(typeof(BodyPropertiesNamesCamelCase))]
[Rule(typeof(TrackedResourceListByImmediateParent))]
[Rule(typeof(XmsExamplesProvidedValidation))]
public Dictionary<string, Dictionary<string, Operation>> Paths { get; set; }
/// <summary>
/// Key is actual path and the value is serializationProperty of http operations and operation objects.
/// </summary>
[JsonProperty("x-ms-paths")]
[Rule(typeof(ListOperationNamingWarning))]
[Rule(typeof(CollectionObjectPropertiesNamingValidation))]
[Rule(typeof(ProvidersPathValidation))]
[CollectionRule(typeof(XmsPathsMustOverloadPaths))]
[CollectionRule(typeof(BodyPropertiesNamesCamelCase))]
[Rule(typeof(XmsExamplesProvidedValidation))]
public Dictionary<string, Dictionary<string, Operation>> CustomPaths { get; set; }
/// <summary>
/// Key is the object serviceTypeName and the value is swagger definition.
/// </summary>
[Rule(typeof(ArmResourcePropertiesBag))]
[Rule(typeof(BooleanPropertyNotRecommended))]
[CollectionRule(typeof(BooleanPropertyNotRecommended))]
[Rule(typeof(ResourceModelValidation))]
[Rule(typeof(ResourceIsMsResourceValidation))]
[Rule(typeof(GuidValidation))]
[Rule(typeof(SkuModelValidation))]
[Rule(typeof(DefinitionsPropertiesNamesCamelCase))]
[Rule(typeof(TrackedResourceGetOperationValidation))]
[Rule(typeof(TrackedResourceListByResourceGroup))]
[Rule(typeof(TrackedResourceListBySubscription))]
[Rule(typeof(TrackedResourcePatchOperationValidation))]
[Rule(typeof(DescriptionMissing))]
[Rule(typeof(PatchBodyParametersSchemaValidation))]
[CollectionRule(typeof(RequiredReadOnlyPropertiesValidation))]
public Dictionary<string, Schema> Definitions { get; set; }
/// <summary>
/// Dictionary of parameters that can be used across operations.
/// This property does not define global parameters for all operations.
/// </summary>
[Rule(typeof(ServiceDefinitionParameters))]
[CollectionRule(typeof(AnonymousBodyParameter))]
public Dictionary<string, SwaggerParameter> Parameters { get; set; }
/// <summary>
/// Dictionary of responses that can be used across operations. The key indicates status code.
/// </summary>
public Dictionary<string, OperationResponse> Responses { get; set; }
/// <summary>
/// Key is the object serviceTypeName and the value is swagger security definition.
/// </summary>
[Rule(typeof(SecurityDefinitionsStructureValidation))]
public Dictionary<string, SecurityDefinition> SecurityDefinitions { get; set; }
/// <summary>
/// A declaration of which security schemes are applied for the API as a whole.
/// The list of values describes alternative security schemes that can be used
/// (that is, there is a logical OR between the security requirements). Individual
/// operations can override this definition.
/// </summary>
public IList<Dictionary<string, List<string>>> Security { get; set; }
/// <summary>
/// A list of tags used by the specification with additional metadata. The order
/// of the tags can be used to reflect on their order by the parsing tools. Not all
/// tags that are used by the Operation Object must be declared. The tags that are
/// not declared may be organized randomly or based on the tools' logic. Each
/// tag name in the list MUST be unique.
/// </summary>
public IList<Tag> Tags { get; set; }
/// <summary>
/// Additional external documentation
/// </summary>
public ExternalDoc ExternalDocs { get; set; }
/// <summary>
/// Path to this Swagger.
/// </summary>
internal Uri FilePath { 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)
{
if (previous == null)
throw new ArgumentNullException("previous");
context.CurrentRoot = this;
context.PreviousRoot = previous;
base.Compare(context, previous);
var previousDefinition = previous as ServiceDefinition;
if (previousDefinition == null)
throw new ArgumentException("Comparing a service definition with something else.");
if (Info != null && previousDefinition.Info != null)
{
context.PushProperty("info");
context.PushProperty("version");
CompareVersions(context, Info.Version, previousDefinition.Info.Version);
context.Pop();
context.Pop();
}
if (context.Strict)
{
// There was no version change between the documents. This is not an error, but noteworthy.
context.LogInfo(ComparisonMessages.NoVersionChange);
}
// Check that all the protocols of the old version are supported by the new version.
context.PushProperty("schemes");
foreach (var scheme in previousDefinition.Schemes)
{
if (!Schemes.Contains(scheme))
{
context.LogBreakingChange(ComparisonMessages.ProtocolNoLongerSupported, scheme);
}
}
context.Pop();
// Check that all the request body formats that were accepted still are.
context.PushProperty("consumes");
foreach (var format in previousDefinition.Consumes)
{
if (!Consumes.Contains(format))
{
context.LogBreakingChange(ComparisonMessages.RequestBodyFormatNoLongerSupported, format);
}
}
context.Pop();
// Check that all the response body formats were also supported by the old version.
context.PushProperty("produces");
foreach (var format in Produces)
{
if (!previousDefinition.Produces.Contains(format))
{
context.LogBreakingChange(ComparisonMessages.ResponseBodyFormatNowSupported, format);
}
}
context.Pop();
// Check that no paths were removed, and compare the paths that are still there.
var newPaths = RemovePathVariables(Paths);
context.PushProperty("paths");
foreach (var path in previousDefinition.Paths.Keys)
{
var p = Regex.Replace(path, @"\{\w*\}", @"{}");
context.PushProperty(path);
Dictionary<string, Operation> operations = null;
if (!newPaths.TryGetValue(p, out operations))
{
context.LogBreakingChange(ComparisonMessages.RemovedPath, path);
}
else
{
Dictionary<string, Operation> previousOperations = previousDefinition.Paths[path];
foreach (var previousOperation in previousOperations)
{
Operation newOperation = null;
if (!operations.TryGetValue(previousOperation.Key, out newOperation))
{
context.LogBreakingChange(ComparisonMessages.RemovedOperation, previousOperation.Value.OperationId);
}
}
foreach (var operation in operations)
{
Operation previousOperation = null;
if (previousDefinition.Paths[path].TryGetValue(operation.Key, out previousOperation))
{
context.PushProperty(operation.Key);
operation.Value.Compare(context, previousOperation);
context.Pop();
}
}
}
context.Pop();
}
context.Pop();
newPaths = RemovePathVariables(CustomPaths);
context.PushProperty("x-ms-paths");
foreach (var path in previousDefinition.CustomPaths.Keys)
{
var p = Regex.Replace(path, @"\{\w*\}", @"{}");
context.PushProperty(path);
Dictionary<string, Operation> operations = null;
if (!newPaths.TryGetValue(p, out operations))
{
context.LogBreakingChange(ComparisonMessages.RemovedPath, path);
}
else
{
Dictionary<string, Operation> previousOperations = previousDefinition.CustomPaths[path];
foreach (var previousOperation in previousOperations)
{
Operation newOperation = null;
if (!operations.TryGetValue(previousOperation.Key, out newOperation))
{
context.LogBreakingChange(ComparisonMessages.RemovedOperation, previousOperation.Value.OperationId);
}
}
foreach (var operation in operations)
{
Operation previousOperation = null;
if (previousDefinition.CustomPaths[path].TryGetValue(operation.Key, out previousOperation))
{
context.PushProperty(operation.Key);
operation.Value.Compare(context, previousOperation);
context.Pop();
}
}
}
context.Pop();
}
context.Pop();
ReferenceTrackSchemas(this);
ReferenceTrackSchemas(previousDefinition);
context.PushProperty("parameters");
foreach (var def in previousDefinition.Parameters.Keys)
{
SwaggerParameter parameter = null;
if (!Parameters.TryGetValue(def, out parameter))
{
context.LogBreakingChange(ComparisonMessages.RemovedClientParameter, def);
}
else
{
context.PushProperty(def);
parameter.Compare(context, previousDefinition.Parameters[def]);
context.Pop();
}
}
context.Pop();
context.PushProperty("responses");
foreach (var def in previousDefinition.Responses.Keys)
{
OperationResponse response = null;
if (!Responses.TryGetValue(def, out response))
{
context.LogBreakingChange(ComparisonMessages.RemovedDefinition, def);
}
else
{
context.PushProperty(def);
response.Compare(context, previousDefinition.Responses[def]);
context.Pop();
}
}
context.Pop();
context.PushProperty("definitions");
foreach (var def in previousDefinition.Definitions.Keys)
{
Schema schema = null;
Schema oldSchema = previousDefinition.Definitions[def];
if (!Definitions.TryGetValue(def, out schema))
{
if (oldSchema.IsReferenced)
// It's only an error if the definition is referenced in the old service.
context.LogBreakingChange(ComparisonMessages.RemovedDefinition, def);
}
else if (schema.IsReferenced && oldSchema.IsReferenced)
{
context.PushProperty(def);
schema.Compare(context, previousDefinition.Definitions[def]);
context.Pop();
}
}
context.Pop();
context.Pop();
return context.Messages;
}
/// <summary>
/// Since renaming a path parameter doesn't logically alter the path, we must remove the parameter names
/// before comparing paths using string comparison.
/// </summary>
/// <param name="paths">A dictionary of paths, potentially with embedded parameter names.</param>
/// <returns>A transformed dictionary, where paths do not embed parameter names.</returns>
private Dictionary<string, Dictionary<string, Operation>> RemovePathVariables(Dictionary<string, Dictionary<string, Operation>> paths)
{
var result = new Dictionary<string, Dictionary<string, Operation>>();
foreach (var kv in paths)
{
var p = Regex.Replace(kv.Key, @"\{\w*\}", @"{}");
result[p] = kv.Value;
}
return result;
}
/// <summary>
/// Since some services may rely on semantic versioning, comparing versions is fairly complex.
/// </summary>
/// <param name="context">A comparison context.</param>
/// <param name="newVer">The new version string.</param>
/// <param name="oldVer">The old version string</param>
/// <remarks>
/// In semantic versioning schemes, only the major and minor version numbers are considered when comparing versions.
/// Build numbers are ignored.
/// </remarks>
private void CompareVersions(ComparisonContext context, string newVer, string oldVer)
{
var oldVersion = oldVer.Split('.');
var newVersion = newVer.Split('.');
// If the version consists only of numbers separated by '.', we'll consider it semantic versioning.
if (!context.Strict && oldVersion.Length > 0 && newVersion.Length > 0)
{
bool versionChanged = false;
// Situation 1: The versioning scheme is semantic, i.e. it uses a major.minr.build-number scheme, where each component is an integer.
// In this case, we care about the major/minor numbers, but not the build number. In othe words, ifall that is different
// is the build number, it will not be treated as a version change.
int oldMajor = 0, newMajor = 0;
bool integers = int.TryParse(oldVersion[0], out oldMajor) && int.TryParse(newVersion[0], out newMajor);
if (integers && oldMajor != newMajor)
{
versionChanged = true;
if (oldMajor > newMajor)
{
context.LogError(ComparisonMessages.VersionsReversed, oldVer, newVer);
}
}
if (!versionChanged && integers && oldVersion.Length > 1 && newVersion.Length > 1)
{
int oldMinor = 0, newMinor = 0;
integers = int.TryParse(oldVersion[1], out oldMinor) && int.TryParse(newVersion[1], out newMinor);
if (integers && oldMinor != newMinor)
{
versionChanged = true;
if (oldMinor > newMinor)
{
context.LogError(ComparisonMessages.VersionsReversed, oldVer, newVer);
}
}
}
// Situation 2: The versioning scheme is something else, maybe a date or just a label?
// Regardless of what it is, we just check whether the two strings are equal or not.
if (!versionChanged && !integers)
{
versionChanged = !oldVer.ToLower().Equals(newVer.ToLower());
}
context.Strict = !versionChanged;
}
}
/// <summary>
/// In order to avoid comparing definitions (schemas) that are not used, we go through all references that are
/// found in operations, global parameters, and global responses. Definitions that are referenced from other
/// definitions are included only by transitive closure.
/// </summary>
private static void ReferenceTrackSchemas(ServiceDefinition service)
{
foreach (var schema in service.Definitions.Values)
{
schema.IsReferenced = false;
}
foreach (var path in service.Paths.Values)
{
foreach (var operation in path.Values)
{
foreach (var parameter in operation.Parameters)
{
if (parameter.Schema != null && !string.IsNullOrWhiteSpace(parameter.Schema.Reference))
{
var schema = FindReferencedSchema(parameter.Schema.Reference, service.Definitions);
schema.IsReferenced = true;
}
}
}
}
foreach (var path in service.CustomPaths.Values)
{
foreach (var operation in path.Values)
{
foreach (var parameter in operation.Parameters)
{
if (parameter.Schema != null && !string.IsNullOrWhiteSpace(parameter.Schema.Reference))
{
var schema = FindReferencedSchema(parameter.Schema.Reference, service.Definitions);
schema.IsReferenced = true;
}
}
}
}
foreach (var parameter in service.Parameters.Values)
{
if (parameter.Schema != null && !string.IsNullOrWhiteSpace(parameter.Schema.Reference))
{
var schema = FindReferencedSchema(parameter.Schema.Reference, service.Definitions);
schema.IsReferenced = true;
}
}
foreach (var response in service.Responses.Values)
{
if (response.Schema != null && !string.IsNullOrWhiteSpace(response.Schema.Reference))
{
var schema = FindReferencedSchema(response.Schema.Reference, service.Definitions);
schema.IsReferenced = true;
}
}
var changed = true;
while (changed)
{
changed = false;
foreach (var schema in service.Definitions.Values.Where(d => d.IsReferenced))
{
foreach (var property in schema.Properties.Values)
{
if (!string.IsNullOrWhiteSpace(property.Reference))
{
var s = FindReferencedSchema(property.Reference, service.Definitions);
changed = changed || !s.IsReferenced;
s.IsReferenced = true;
}
}
}
}
}
/// <summary>
/// Retrieve a schema from the definitions section.
/// </summary>
/// <param name="reference">A document-relative reference path -- #/definitions/XXX</param>
/// <param name="definitions">The definitions dictionary to use</param>
/// <returns></returns>
private static Schema FindReferencedSchema(string reference, IDictionary<string, Schema> definitions)
{
if (reference != null && reference.StartsWith("#", StringComparison.Ordinal))
{
var parts = reference.Split('/');
if (parts.Length == 3 && parts[1].Equals("definitions"))
{
Schema p = null;
if (definitions.TryGetValue(parts[2], out p))
{
return p;
}
}
}
return null;
}
}
}