Skip to content

Commit 1c8398c

Browse files
authored
Merge branch 'vnext' into vvk/diagnostic-in-context
2 parents f621768 + 38287d7 commit 1c8398c

File tree

55 files changed

+11533
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+11533
-12
lines changed

build.cmd

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
@echo off
2-
if "%~1"=="" goto :error
3-
4-
SET VERSION=%~1
5-
62
Echo Building Microsoft.OpenApi
73

84
SET PROJ=%~dp0src\Microsoft.OpenApi\Microsoft.OpenApi.csproj
95
dotnet build %PROJ% /t:restore /p:Configuration=Release
106
dotnet build %PROJ% /t:build /p:Configuration=Release
11-
dotnet build %PROJ% /t:pack /p:Configuration=Release;PackageOutputPath=%~dp0artifacts;Version=%VERSION%
7+
dotnet build %PROJ% /t:pack /p:Configuration=Release;PackageOutputPath=%~dp0artifacts
128

139
Echo Building Microsoft.OpenApi.Readers
1410

1511
SET PROJ=%~dp0src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj
1612
dotnet build %PROJ% /t:restore /p:Configuration=Release
1713
dotnet build %PROJ% /t:build /p:Configuration=Release
18-
dotnet build %PROJ% /t:pack /p:Configuration=Release;PackageOutputPath=%~dp0artifacts;Version=%VERSION%
14+
dotnet build %PROJ% /t:pack /p:Configuration=Release;PackageOutputPath=%~dp0artifacts
1915

2016
goto :end
2117
:error

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi.Readers</Title>
1212
<PackageId>Microsoft.OpenApi.Readers</PackageId>
13-
<Version>1.1.4</Version>
13+
<Version>1.2.0-preview</Version>
1414
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi</Title>
1212
<PackageId>Microsoft.OpenApi</PackageId>
13-
<Version>1.1.4</Version>
13+
<Version>1.2.0-preview</Version>
1414
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.OpenApi.Models;
7+
8+
namespace Microsoft.OpenApi.Services
9+
{
10+
/// <summary>
11+
/// A class encapsulating the comparision context.
12+
/// </summary>
13+
public class ComparisonContext
14+
{
15+
private readonly IList<OpenApiDifference> _openApiDifferences = new List<OpenApiDifference>();
16+
private readonly Stack<string> _path = new Stack<string>();
17+
internal readonly OpenApiDocument SourceDocument;
18+
internal readonly Stack<OpenApiSchema> SourceSchemaLoop = new Stack<OpenApiSchema>();
19+
internal readonly OpenApiDocument TargetDocument;
20+
internal readonly Stack<OpenApiSchema> TargetSchemaLoop = new Stack<OpenApiSchema>();
21+
internal OpenApiComparerFactory OpenApiComparerFactory;
22+
23+
/// <summary>
24+
/// Creates instance of <see cref="ComparisonContext"/>.
25+
/// </summary>
26+
public ComparisonContext(
27+
OpenApiComparerFactory openApiComparerFactory,
28+
OpenApiDocument sourceDocument,
29+
OpenApiDocument targetDocument)
30+
{
31+
OpenApiComparerFactory = openApiComparerFactory;
32+
SourceDocument = sourceDocument;
33+
TargetDocument = targetDocument;
34+
}
35+
36+
/// <summary>
37+
/// Gets the list of open api differences.
38+
/// </summary>
39+
public IEnumerable<OpenApiDifference> OpenApiDifferences => _openApiDifferences;
40+
41+
/// <summary>
42+
/// Pointer to the source of difference in the document.
43+
/// </summary>
44+
public string PathString => "#/" + string.Join("/", _path.Reverse());
45+
46+
/// <summary>
47+
/// Adds an open api difference.
48+
/// </summary>
49+
/// <param name="openApiDifference">The open api difference to add.</param>
50+
public void AddOpenApiDifference(OpenApiDifference openApiDifference)
51+
{
52+
if (openApiDifference == null)
53+
{
54+
throw Error.ArgumentNull(nameof(openApiDifference));
55+
}
56+
57+
_openApiDifferences.Add(openApiDifference);
58+
}
59+
60+
/// <summary>
61+
/// Allow Rule to indicate difference occured at a deeper context level.
62+
/// </summary>
63+
/// <param name="segment">Identifier for the context.</param>
64+
public void Enter(string segment)
65+
{
66+
_path.Push(segment);
67+
}
68+
69+
/// <summary>
70+
/// Exit from path context level. Enter and Exit calls should be matched.
71+
/// </summary>
72+
public void Exit()
73+
{
74+
_path.Pop();
75+
}
76+
77+
/// <summary>
78+
/// Gets the comparer instance for the requested type.
79+
/// </summary>
80+
/// <typeparam name="T">Type of requested comparer.</typeparam>
81+
/// <returns>Comparer instance to use when comparing requested type.</returns>
82+
internal OpenApiComparerBase<T> GetComparer<T>()
83+
{
84+
return OpenApiComparerFactory.GetComparer<T>();
85+
}
86+
}
87+
}
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 System;
5+
using System.IO;
6+
using Microsoft.OpenApi.Any;
7+
using Microsoft.OpenApi.Writers;
8+
9+
namespace Microsoft.OpenApi.Services
10+
{
11+
/// <summary>
12+
/// Defines behavior for comparing properties of <see cref="IOpenApiAny"/>.
13+
/// </summary>
14+
public class OpenApiAnyComparer : OpenApiComparerBase<IOpenApiAny>
15+
{
16+
/// <summary>
17+
/// Executes comparision against source and target <see cref="IOpenApiAny"/>.
18+
/// </summary>
19+
/// <param name="source">The source.</param>
20+
/// <param name="target">The target.</param>
21+
/// <param name="comparisonContext">Context under which to compare the source and target.</param>
22+
public override void Compare(
23+
IOpenApiAny source,
24+
IOpenApiAny target,
25+
ComparisonContext comparisonContext)
26+
{
27+
if (source == null && target == null)
28+
{
29+
return;
30+
}
31+
32+
if (source == null || target == null)
33+
{
34+
comparisonContext.AddOpenApiDifference(
35+
new OpenApiDifference
36+
{
37+
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update,
38+
SourceValue = source,
39+
TargetValue = target,
40+
OpenApiComparedElementType = typeof(IOpenApiAny),
41+
Pointer = comparisonContext.PathString
42+
});
43+
44+
return;
45+
}
46+
47+
var sourceStringWriter = new StringWriter();
48+
var sourceWriter = new OpenApiJsonWriter(sourceStringWriter);
49+
50+
source.Write(sourceWriter, OpenApiSpecVersion.OpenApi3_0);
51+
var sourceValue = sourceStringWriter.GetStringBuilder().ToString();
52+
53+
var targetStringWriter = new StringWriter();
54+
var targetWriter = new OpenApiJsonWriter(targetStringWriter);
55+
56+
target.Write(targetWriter, OpenApiSpecVersion.OpenApi3_0);
57+
var targetValue = targetStringWriter.GetStringBuilder().ToString();
58+
59+
if (string.Compare(sourceValue, targetValue, StringComparison.InvariantCulture) != 0)
60+
{
61+
comparisonContext.AddOpenApiDifference(new OpenApiDifference
62+
{
63+
OpenApiDifferenceOperation = OpenApiDifferenceOperation.Update,
64+
OpenApiComparedElementType = typeof(IOpenApiAny),
65+
SourceValue = source,
66+
TargetValue = target,
67+
Pointer = comparisonContext.PathString
68+
});
69+
}
70+
}
71+
}
72+
}

src/Microsoft.OpenApi/Services/OpenApiComparer.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,23 @@ public static class OpenApiComparer
1414
/// <summary>
1515
/// Compares two <see cref="OpenApiDocument"/>s and returns a list of differences.
1616
/// </summary>
17-
public static List<OpenApiDifference> Compare(OpenApiDocument source, OpenApiDocument target)
17+
public static IEnumerable<OpenApiDifference> Compare(OpenApiDocument source, OpenApiDocument target)
1818
{
19-
var diffs = new List<OpenApiDifference>();
20-
return diffs;
19+
if (source == null)
20+
{
21+
throw Error.ArgumentNull(nameof(source));
22+
}
23+
24+
if (target == null)
25+
{
26+
throw Error.ArgumentNull(nameof(target));
27+
}
28+
29+
var comparisonContext = new ComparisonContext(new OpenApiComparerFactory(), source, target);
30+
31+
new OpenApiDocumentComparer().Compare(source, target, comparisonContext);
32+
33+
return comparisonContext.OpenApiDifferences;
2134
}
2235
}
2336
}

0 commit comments

Comments
 (0)