Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2993065
Generic ComparisonConttext.
sergey-shandar Feb 27, 2019
6388ff3
Minor changes.
sergey-shandar Feb 27, 2019
e282b97
Store JToken.
sergey-shandar Feb 27, 2019
82c5365
readonly properties
sergey-shandar Feb 27, 2019
ea51210
no auto-properties
sergey-shandar Feb 28, 2019
b98151c
ParsedJson
sergey-shandar Feb 28, 2019
8f3ef8b
No more ReadablePath.
sergey-shandar Feb 28, 2019
b14759f
Fix "parameters" in JsonRef.
sergey-shandar Feb 28, 2019
acfbbf0
No `ObjectPath.SelectNode`.
sergey-shandar Mar 1, 2019
4c5ffa7
functional path.
sergey-shandar Mar 1, 2019
5eca637
Remove JsonPath function.
sergey-shandar Mar 1, 2019
f555168
Minimize ObjectPathPart
sergey-shandar Mar 1, 2019
45c3c4e
ObjectPath.
sergey-shandar Mar 2, 2019
310d50c
Schema, path
sergey-shandar Mar 2, 2019
d6f7226
Unreferencing.
sergey-shandar Mar 2, 2019
a7984c4
Properties.
sergey-shandar Mar 4, 2019
7c599cb
Common code
sergey-shandar Mar 4, 2019
b8227ac
Parser update
sergey-shandar Mar 4, 2019
26b144a
Merge from master
sergey-shandar Mar 4, 2019
f5b0d3a
IParsedJson
sergey-shandar Mar 5, 2019
214a92f
No FileObjectPath
sergey-shandar Mar 5, 2019
9dc7796
Tests.
sergey-shandar Mar 5, 2019
ff4f7d7
Location output
sergey-shandar Mar 5, 2019
6fac0b2
locations
sergey-shandar Mar 5, 2019
0825dc0
some comments.
sergey-shandar Mar 6, 2019
0ff00b8
Address comments
sergey-shandar Mar 6, 2019
cbe591e
JsonDocument instead of ParsedJson
sergey-shandar Mar 6, 2019
be1fbbd
better names
sergey-shandar Mar 6, 2019
f258352
more comments.
sergey-shandar Mar 6, 2019
95056c3
more comments.
sergey-shandar Mar 6, 2019
cd9b0ae
address comments.
sergey-shandar Mar 6, 2019
99004e6
location-new
sergey-shandar Mar 6, 2019
2f616d1
test
sergey-shandar Mar 6, 2019
ac387f3
check location format
sergey-shandar Mar 7, 2019
fa1ce75
JsonPath
sergey-shandar Mar 7, 2019
e34b8bb
Normalize file names.
sergey-shandar Mar 7, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions openapi-diff/src/core/OpenApiDiff.Core/JsonDocument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Newtonsoft.Json.Linq;

namespace OpenApiDiff.Core
{
/// <summary>
/// An interface for untyped parsed JSON.
/// </summary>
public interface IJsonDocument
{
JToken Token { get; }

string FileName { get; }
}

/// <summary>
/// A representation of parsed JSON document.
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class JsonDocument<T>: IJsonDocument
{
/// <summary>
/// Untyped raw parsed JSON. The Token also includes information about
/// file location of each item.
/// </summary>
public JToken Token { get; }

/// <summary>
/// Representation of JSON as `T` type.
/// </summary>
public T Typed { get; }

/// <summary>
/// A JSON source file name.
/// </summary>
public string FileName { get; }

public JsonDocument(JToken token, T typed, string fileName)
{
Token = token;
Typed = typed;
FileName = fileName;
}
}

public static class JsonDocument
{
/// <summary>
/// Creates a `JsonDocument` object. It's a syntax sugar for `new JsonDocument`.
/// </summary>
/// <typeparam name="T">Deserialization type.</typeparam>
/// <param name="token">Raw JSON object. The object includes information about JSON token locations</param>
/// <param name="typed">Representation of the JSON as `T` type.</param>
/// <param name="fileName">A JSON source file name.</param>
/// <returns></returns>
public static JsonDocument<T> ToJsonDocument<T>(this JToken token, T typed, string fileName)
=> new JsonDocument<T>(token, typed, fileName);
}
}
31 changes: 0 additions & 31 deletions openapi-diff/src/core/OpenApiDiff.Core/Logging/FileObjectPath.cs

This file was deleted.

70 changes: 49 additions & 21 deletions openapi-diff/src/core/OpenApiDiff.Core/Logging/ObjectPath.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using YamlDotNet.RepresentationModel;

namespace OpenApiDiff.Core.Logging
{
Expand All @@ -13,37 +13,65 @@ namespace OpenApiDiff.Core.Logging
/// </summary>
public class ObjectPath
{
public static ObjectPath Empty => new ObjectPath(Enumerable.Empty<ObjectPathPart>());
public static ObjectPath Empty => new ObjectPath(Enumerable.Empty<Func<JToken, string>>());

private ObjectPath(IEnumerable<ObjectPathPart> path)
private ObjectPath(IEnumerable<Func<JToken, string>> path)
{
Path = path;
}

private ObjectPath Append(ObjectPathPart part) => new ObjectPath(Path.Concat(new[] { part }));
private ObjectPath Append(Func<JToken, string> f) => new ObjectPath(Path.Concat(new[] { f }));

public ObjectPath AppendIndex(int index) => Append(new ObjectPathPartIndex(index));
public ObjectPath AppendProperty(string property) => Append((_) => property);

public ObjectPath AppendProperty(string property) => Append(new ObjectPathPartProperty(property));

public IEnumerable<ObjectPathPart> Path { get; }

// https://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-04
public string JsonPointer => string.Concat(Path.Select(p => p.JsonPointer));
public ObjectPath AppendItemByName(string value) => Append(t =>
{
var list = t
?.Select((v, i) => (v, i))
?.Where(vi => vi.v?["name"]?.Value<string>() == value)
?.ToList();
return list == null || list.Count == 0 ? null : list[0].i.ToString();
});

// http://goessner.net/articles/JsonPath/, https://github.com/jayway/JsonPath
public string JsonPath => "$" + string.Concat(Path.Select(p => p.JsonPath));
public IEnumerable<Func<JToken, string>> Path { get; }

public string ReadablePath => string.Concat(Path.Select(p => p.ReadablePath));
private static ObjectPath ParseRef(string s)
=> new ObjectPath(s
.Split('/')
.Where(v => v != "#")
.Select<string, Func<JToken, string>>(v => _ => v.Replace("~1", "/").Replace("~0", "~"))
);

public YamlNode SelectNode(YamlNode node)
private static JToken FromObject(JObject o, string name)
{
YamlNode result = node;
foreach (var part in Path)
{
result = part.SelectNode(ref node) ?? result;
}
return result;
var @ref = o["$ref"];
var unrefed = @ref != null ? ParseRef(@ref.Value<string>()).CompletePath(o.Root).Last().token : o;
return unrefed[name];
}

private static IEnumerable<(JToken token, string name)> CompletePath(IEnumerable<Func<JToken, string>> p, JToken t)
=> p.Select(v => {
var name = v(t);
t =
t is JArray a ? int.TryParse(name, out var i) ? a[i] : null :
t is JObject o ? FromObject(o, name) :
null;
return (t, name);
});

public IEnumerable<(JToken token, string name)> CompletePath(JToken t)
=> CompletePath(Path, t);

public static string FileNameNorm(string fileName)
=> fileName.Replace("\\", "/");

// https://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-04
public string JsonPointer(IJsonDocument t) => CompletePath(t.Token)
.Select(v => v.name?.Replace("~", "~0")?.Replace("/", "~1"))
.Aggregate(FileNameNorm(t.FileName) + "#", (a, b) => a == null || b == null ? null : a + "/" + b);


public ObjectPath AppendExpression(Func<JToken, string> func)
=> Append(func);
}
}
24 changes: 0 additions & 24 deletions openapi-diff/src/core/OpenApiDiff.Core/Logging/ObjectPathPart.cs

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PropertyGroup>
<AssemblyName>OpenApiDiff.Core</AssemblyName>
<PackageTags>Microsoft AutoRest</PackageTags>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions openapi-diff/src/core/OpenApiDiff/OpenApiDiff.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageProjectUrl>https://github.com/Azure/openapi-diff</PackageProjectUrl>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 7 additions & 1 deletion openapi-diff/src/core/OpenApiDiff/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ private static int Main(string[] args)
string swaggerPrev = File.ReadAllText(settings.OldSpec);
string swaggerNew = File.ReadAllText(settings.NewSpec);

var messages = modeler.Compare(swaggerPrev, swaggerNew, settings);
var messages = modeler.Compare(
settings.OldSpec,
swaggerPrev,
settings.NewSpec,
swaggerNew,
settings
);
foreach (var msg in messages)
{
Console.WriteLine(settings.JsonValidationMessages ? msg.GetValidationMessagesAsJson() : msg.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading