1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT license.
3+
4+ using Microsoft . OpenApi . Any ;
5+ using Microsoft . OpenApi . Models ;
6+
7+ namespace Microsoft . OpenApi . Services
8+ {
9+ /// <summary>
10+ /// Defines behavior for comparing properties of <see cref="OpenApiExample"/>.
11+ /// </summary>
12+ public class OpenApiExampleComparer : OpenApiComparerBase < OpenApiExample >
13+ {
14+ /// <summary>
15+ /// Executes comparision against source and target <see cref="OpenApiExample"/>.
16+ /// </summary>
17+ /// <param name="sourceExample">The source.</param>
18+ /// <param name="targetExample">The target.</param>
19+ /// <param name="comparisonContext">Context under which to compare the source and target.</param>
20+ public override void Compare (
21+ OpenApiExample sourceExample ,
22+ OpenApiExample targetExample ,
23+ ComparisonContext comparisonContext )
24+ {
25+ if ( sourceExample == null && targetExample == null )
26+ {
27+ return ;
28+ }
29+
30+ if ( sourceExample == null || targetExample == null )
31+ {
32+ comparisonContext . AddOpenApiDifference (
33+ new OpenApiDifference
34+ {
35+ OpenApiDifferenceOperation = OpenApiDifferenceOperation . Update ,
36+ SourceValue = sourceExample ,
37+ TargetValue = targetExample ,
38+ OpenApiComparedElementType = typeof ( OpenApiExample ) ,
39+ Pointer = comparisonContext . PathString
40+ } ) ;
41+
42+ return ;
43+ }
44+
45+ new OpenApiReferenceComparer < OpenApiExample > ( )
46+ . Compare ( sourceExample . Reference , targetExample . Reference , comparisonContext ) ;
47+
48+ WalkAndCompare ( comparisonContext , OpenApiConstants . Description ,
49+ ( ) => Compare ( sourceExample . Description , targetExample . Description , comparisonContext ) ) ;
50+
51+ WalkAndCompare ( comparisonContext , OpenApiConstants . Summary ,
52+ ( ) => Compare ( sourceExample . Summary , targetExample . Summary , comparisonContext ) ) ;
53+
54+ WalkAndCompare ( comparisonContext , OpenApiConstants . ExternalValue ,
55+ ( ) => Compare ( sourceExample . ExternalValue , targetExample . ExternalValue , comparisonContext ) ) ;
56+
57+ WalkAndCompare (
58+ comparisonContext ,
59+ OpenApiConstants . Value ,
60+ ( ) => comparisonContext
61+ . GetComparer < IOpenApiAny > ( )
62+ . Compare ( sourceExample . Value , targetExample . Value , comparisonContext ) ) ;
63+ }
64+ }
65+ }
0 commit comments