Skip to content

Commit f9bdc15

Browse files
committed
FIx review comments
1 parent a674f47 commit f9bdc15

16 files changed

+398
-482
lines changed

src/Microsoft.OpenApi/Services/OpenApiComparerBase.cs

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -166,64 +166,6 @@ internal void Compare<TEnum>(Enum source, Enum target, ComparisonContext compari
166166
}
167167
}
168168

169-
/// <summary>
170-
/// Compares <see cref="OpenApiReference"/> object.
171-
/// </summary>
172-
/// <param name="sourceReference">The source.</param>
173-
/// <param name="targetReference">The target.</param>
174-
/// <param name="comparisonContext">The context under which to compare the objects.</param>
175-
internal void Compare<TReference>(
176-
OpenApiReference sourceReference,
177-
OpenApiReference targetReference,
178-
ComparisonContext comparisonContext)
179-
{
180-
if (sourceReference == null && targetReference == null)
181-
{
182-
return;
183-
}
184-
185-
if (sourceReference == null || targetReference == null)
186-
{
187-
comparisonContext.AddOpenApiDifference(
188-
new OpenApiDifference
189-
{
190-
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update,
191-
SourceValue = sourceReference,
192-
TargetValue = targetReference,
193-
OpenApiComparedElementType = typeof(OpenApiReference),
194-
Pointer = comparisonContext.PathString
195-
});
196-
197-
return;
198-
}
199-
200-
if (sourceReference.Id != targetReference.Id || sourceReference.Type != targetReference.Type)
201-
{
202-
WalkAndAddOpenApiDifference(
203-
comparisonContext,
204-
OpenApiConstants.DollarRef,
205-
new OpenApiDifference
206-
{
207-
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update,
208-
SourceValue = sourceReference,
209-
TargetValue = targetReference,
210-
OpenApiComparedElementType = typeof(OpenApiReference)
211-
});
212-
213-
return;
214-
}
215-
216-
var source = (TReference) comparisonContext.SourceDocument.ResolveReference(
217-
sourceReference);
218-
219-
var target = (TReference) comparisonContext.TargetDocument.ResolveReference(
220-
targetReference);
221-
222-
comparisonContext
223-
.GetComparer<TReference>()
224-
.Compare(source, target, comparisonContext);
225-
}
226-
227169
/// <summary>
228170
/// Compares <see cref="IDictionary{TKey,TValue}"/> where TKey is <see cref="string"/> and TValue is
229171
/// <see cref="string"/>.
@@ -258,29 +200,29 @@ internal void Compare(IDictionary<string, string> source, IDictionary<string, st
258200

259201
foreach (var newKeyInTarget in newKeysInTarget)
260202
{
261-
comparisonContext.AddOpenApiDifference(
203+
WalkAndAddOpenApiDifference(
204+
comparisonContext,
205+
newKeyInTarget,
262206
new OpenApiDifference
263207
{
264208
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Add,
265-
TargetValue = new KeyValuePair<string, string>(
266-
newKeyInTarget,
267-
target[newKeyInTarget]),
268-
OpenApiComparedElementType = typeof(KeyValuePair<string, string>)
209+
TargetValue = target[newKeyInTarget],
210+
OpenApiComparedElementType = typeof(string)
269211
});
270212
}
271213

272214
var removedKeysFromSource = source.Keys.Except(target.Keys).ToList();
273215

274216
foreach (var removedKeyFromSource in removedKeysFromSource)
275217
{
276-
comparisonContext.AddOpenApiDifference(
218+
WalkAndAddOpenApiDifference(
219+
comparisonContext,
220+
removedKeyFromSource,
277221
new OpenApiDifference
278222
{
279-
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Add,
280-
TargetValue = new KeyValuePair<string, string>(
281-
removedKeyFromSource,
282-
source[removedKeyFromSource]),
283-
OpenApiComparedElementType = typeof(KeyValuePair<string, string>)
223+
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Remove,
224+
SourceValue = source[removedKeyFromSource],
225+
OpenApiComparedElementType = typeof(string)
284226
});
285227
}
286228
}

src/Microsoft.OpenApi/Services/OpenApiDictionaryComparer.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ public override void Compare(
5656
new OpenApiDifference
5757
{
5858
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Add,
59-
TargetValue = new KeyValuePair<string, T>(
60-
newKeyInTarget,
61-
targetFragment[newKeyInTarget]),
62-
OpenApiComparedElementType = typeof(KeyValuePair<string, T>)
59+
TargetValue = targetFragment[newKeyInTarget],
60+
OpenApiComparedElementType = typeof(T)
6361
});
6462
}
6563

@@ -80,8 +78,8 @@ public override void Compare(
8078
new OpenApiDifference
8179
{
8280
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Remove,
83-
SourceValue = source,
84-
OpenApiComparedElementType = typeof(KeyValuePair<string, T>)
81+
SourceValue = source.Value,
82+
OpenApiComparedElementType = typeof(T)
8583
});
8684
}
8785
}

src/Microsoft.OpenApi/Services/OpenApiOperationsComparer.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ public override void Compare(
5656
new OpenApiDifference
5757
{
5858
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Add,
59-
TargetValue = new KeyValuePair<OperationType, OpenApiOperation>(
60-
newOperationKeyInTarget,
61-
targetOperations[newOperationKeyInTarget]),
62-
OpenApiComparedElementType = typeof(KeyValuePair<OperationType, OpenApiOperation>)
59+
TargetValue = targetOperations[newOperationKeyInTarget],
60+
OpenApiComparedElementType = typeof(OpenApiOperation)
6361
});
6462
}
6563

@@ -80,8 +78,8 @@ public override void Compare(
8078
new OpenApiDifference
8179
{
8280
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Remove,
83-
SourceValue = sourceOperation,
84-
OpenApiComparedElementType = typeof(KeyValuePair<OperationType, OpenApiOperation>)
81+
SourceValue = sourceOperation.Value,
82+
OpenApiComparedElementType = typeof(OpenApiOperation)
8583
});
8684
}
8785
}

src/Microsoft.OpenApi/Services/OpenApiParameterComparer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public override void Compare(
4242
return;
4343
}
4444

45-
Compare<OpenApiParameter>(sourceParameter.Reference, targetParameter.Reference, comparisonContext);
45+
new OpenApiReferenceComparer<OpenApiParameter>()
46+
.Compare(sourceParameter.Reference, targetParameter.Reference, comparisonContext);
4647

4748
WalkAndCompare(
4849
comparisonContext,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using Microsoft.OpenApi.Interfaces;
5+
using Microsoft.OpenApi.Models;
6+
7+
namespace Microsoft.OpenApi.Services
8+
{
9+
/// <summary>
10+
/// Defines behavior for comparing properties of <see cref="OpenApiReference"/>.
11+
/// </summary>
12+
public class OpenApiReferenceComparer<T> : OpenApiComparerBase<OpenApiReference> where T : IOpenApiReferenceable
13+
{
14+
/// <summary>
15+
/// Compares <see cref="OpenApiReference"/> object.
16+
/// </summary>
17+
/// <param name="sourceReference">The source.</param>
18+
/// <param name="targetReference">The target.</param>
19+
/// <param name="comparisonContext">The context under which to compare the objects.</param>
20+
public override void Compare(
21+
OpenApiReference sourceReference,
22+
OpenApiReference targetReference,
23+
ComparisonContext comparisonContext)
24+
{
25+
if (sourceReference == null && targetReference == null)
26+
{
27+
return;
28+
}
29+
30+
if (sourceReference == null || targetReference == null)
31+
{
32+
comparisonContext.AddOpenApiDifference(
33+
new OpenApiDifference
34+
{
35+
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update,
36+
SourceValue = sourceReference,
37+
TargetValue = targetReference,
38+
OpenApiComparedElementType = typeof(OpenApiReference),
39+
Pointer = comparisonContext.PathString
40+
});
41+
42+
return;
43+
}
44+
45+
if (sourceReference.Id != targetReference.Id || sourceReference.Type != targetReference.Type)
46+
{
47+
WalkAndAddOpenApiDifference(
48+
comparisonContext,
49+
OpenApiConstants.DollarRef,
50+
new OpenApiDifference
51+
{
52+
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update,
53+
SourceValue = sourceReference,
54+
TargetValue = targetReference,
55+
OpenApiComparedElementType = typeof(OpenApiReference)
56+
});
57+
58+
return;
59+
}
60+
61+
var source = (T) comparisonContext.SourceDocument.ResolveReference(
62+
sourceReference);
63+
64+
var target = (T) comparisonContext.TargetDocument.ResolveReference(
65+
targetReference);
66+
67+
comparisonContext
68+
.GetComparer<T>()
69+
.Compare(source, target, comparisonContext);
70+
}
71+
}
72+
}

src/Microsoft.OpenApi/Services/OpenApiRequestBodyComparer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public override void Compare(
4242
return;
4343
}
4444

45-
Compare<OpenApiRequestBody>(sourceRequestBody.Reference, targetRequestBody.Reference, comparisonContext);
45+
new OpenApiReferenceComparer<OpenApiRequestBody>()
46+
.Compare(sourceRequestBody.Reference, targetRequestBody.Reference, comparisonContext);
4647

4748
WalkAndCompare(comparisonContext, OpenApiConstants.Description,
4849
() => Compare(sourceRequestBody.Description, targetRequestBody.Description, comparisonContext));

0 commit comments

Comments
 (0)