1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT license.
3+
4+ using System ;
5+
6+ namespace Microsoft . OpenApi . Services
7+ {
8+ /// <summary>
9+ /// Defines behavior for comparing parts of <see cref="OpenAPiDocument"/> class.
10+ /// </summary>
11+ /// <typeparam name="T">Type of class to compare.</typeparam>
12+ public abstract class OpenApiComparerBase < T >
13+ {
14+ /// <summary>
15+ /// Validates a fragment of <see cref="OpenApiDocument"/>.
16+ /// </summary>
17+ /// <param name="sourceFragment">The source fragment.</param>
18+ /// <param name="targetFragment">The target fragment.</param>
19+ /// <param name="comparisonContext">Context under which to compare fragment.</param>
20+ public abstract void Compare ( T sourceFragment , T targetFragment , ComparisonContext comparisonContext ) ;
21+
22+ /// <summary>
23+ /// Compares two string object.
24+ /// </summary>
25+ /// <param name="source">The source string.</param>
26+ /// <param name="target">The target string.</param>
27+ /// <param name="comparisonContext">The context under which to compare the objects.</param>
28+ internal void Compare ( string source , string target , ComparisonContext comparisonContext )
29+ {
30+ if ( string . IsNullOrWhiteSpace ( source ) && string . IsNullOrWhiteSpace ( target ) )
31+ {
32+ return ;
33+ }
34+
35+ if ( string . Compare ( source , target , StringComparison . CurrentCultureIgnoreCase ) != 0 )
36+ {
37+ comparisonContext . AddOpenApiDifference ( new OpenApiDifference
38+ {
39+ OpenApiDifferenceOperation = OpenApiDifferenceOperation . Update ,
40+ OpenApiComparedElementType = typeof ( string ) ,
41+ SourceValue = source ,
42+ TargetValue = target ,
43+ Pointer = comparisonContext . PathString
44+ } ) ;
45+ }
46+ }
47+
48+ /// <summary>
49+ /// Compares two boolean object.
50+ /// </summary>
51+ /// <param name="source">The source.</param>
52+ /// <param name="target">The target.</param>
53+ /// <param name="comparisonContext">The context under which to compare the objects.</param>
54+ internal void Compare ( bool ? source , bool ? target , ComparisonContext comparisonContext )
55+ {
56+ if ( source == null && target == null )
57+ {
58+ return ;
59+ }
60+
61+ if ( source != target )
62+ {
63+ comparisonContext . AddOpenApiDifference ( new OpenApiDifference
64+ {
65+ OpenApiDifferenceOperation = OpenApiDifferenceOperation . Update ,
66+ OpenApiComparedElementType = typeof ( bool ) ,
67+ SourceValue = source ,
68+ TargetValue = target ,
69+ Pointer = comparisonContext . PathString
70+ } ) ;
71+ }
72+ }
73+
74+ /// <summary>
75+ /// Adds a segment to the context path to enable pointing to the current location in the document.
76+ /// </summary>
77+ /// <param name="comparisonContext">The context under which to compare the objects.</param>
78+ /// <param name="segment">An identifier for the segment.</param>
79+ /// <param name="openApiDifference">The open api difference to add.</param>
80+ internal void WalkAndAddOpenApiDifference (
81+ ComparisonContext comparisonContext ,
82+ string segment ,
83+ OpenApiDifference openApiDifference )
84+ {
85+ comparisonContext . Enter ( segment . Replace ( "/" , "~1" ) ) ;
86+ openApiDifference . Pointer = comparisonContext . PathString ;
87+ comparisonContext . AddOpenApiDifference ( openApiDifference ) ;
88+ comparisonContext . Exit ( ) ;
89+ }
90+
91+ /// <summary>
92+ /// Adds a segment to the context path to enable pointing to the current location in the document.
93+ /// </summary>
94+ /// <param name="comparisonContext">The context under which to compare the objects.</param>
95+ /// <param name="segment">An identifier for the segment.</param>
96+ /// <param name="compare">An action that compares objects within the context.</param>
97+ protected virtual void WalkAndCompare (
98+ ComparisonContext comparisonContext ,
99+ string segment ,
100+ Action compare )
101+ {
102+ comparisonContext . Enter ( segment . Replace ( "/" , "~1" ) ) ;
103+ compare ( ) ;
104+ comparisonContext . Exit ( ) ;
105+ }
106+ }
107+ }
0 commit comments