Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<Content Include="Resource\Swagger\new\added_path.json" />
<Content Include="Resource\Swagger\new\changed_operation_id.json" />
<Content Include="Resource\Swagger\new\enum_values_changed.json" />
<Content Include="Resource\Swagger\new\misc_checks_01.json" />
Expand Down Expand Up @@ -44,6 +45,7 @@
<Content Include="Resource\Swagger\new\version_check_03.json" />
<Content Include="Resource\Swagger\new\version_check_04.json" />
<Content Include="Resource\Swagger\old\changed_operation_id.json" />
<Content Include="Resource\Swagger\old\added_path.json" />
<Content Include="Resource\Swagger\old\required_parameter.json" />
<Content Include="Resource\Swagger\old\removed_operation.json" />
<Content Include="Resource\Swagger\old\removed_path.json" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"swagger": 2.0,
"info": {
"title": "added_path",
"version": "1.0"
},
"host": "localhost:8000",
"schemes": [ "http", "https" ],
"paths": {
"/api/Paths": {
"get": {
"tag": [ "Paths" ],
"operationId": "Paths_Get",
"produces": [
"text/plain"
],
"parameters": [],
"responses": {}
}
},
"/api/Operations": {
"get": {
"tag": [ "Operations" ],
"operationId": "Operations_Get",
"produces": [
"text/plain"
],
"parameters": [],
"responses": {}
},
"post": {
"tag": [ "Operations" ],
"operationId": "Operations_Post",
"produces": [
"text/plain"
],
"parameters": [],
"responses": {}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"swagger": 2.0,
"info": {
"title": "added_path",
"version": "1.0"
},
"host": "localhost:8000",
"schemes": [ "http", "https" ],
"paths": {
"/api/Operations": {
"get": {
"tag": [ "Operations" ],
"operationId": "Operations_Get",
"produces": [
"text/plain"
],
"parameters": [],
"responses": {}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ public void OperationIdChanged()
Assert.NotEmpty(missing.Where(m => m.Severity == Category.Error && m.Path.ReadablePath == "#/paths/api/Operations/post"));
}

/// <summary>
/// Verifies that if you added new paths / operations, it's caught.
/// </summary>
[Fact]
public void AddedPaths()
{
var messages = CompareSwagger("added_path.json").ToArray();
var missing = messages.Where(m => m.Id == ComparisonMessages.AddedPath.Id);
Assert.Equal(1, missing.Count());
Assert.NotEmpty(missing.Where(m => m.Severity == Category.Info && m.Path.ReadablePath == "#/paths/api/Paths"));

missing = messages.Where(m => m.Id == ComparisonMessages.AddedOperation.Id);
Assert.Equal(1, missing.Count());
Assert.NotEmpty(missing.Where(m => m.Severity == Category.Info && m.Path.ReadablePath == "#/paths/api/Operations/post"));
}

/// <summary>
/// Verifies that if you remove a required parameter, it's found.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions openapi-diff/src/modeler/AutoRest.Swagger/ComparisonMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public static class ComparisonMessages
Message = "The new version is missing a path that was found in the old version. Was path '{0}' removed or restructured?"
};

public static MessageTemplate AddedPath = new MessageTemplate
{
Id = 1038,
Message = "The new version is adding a path that was not found in the old version."
};

public static MessageTemplate RemovedOperation = new MessageTemplate
{
Id = 1035,
Expand All @@ -38,6 +44,12 @@ public static class ComparisonMessages
Message = "The operation id has been changed from '{0}' to '{1}'. This will impact generated code."
};

public static MessageTemplate AddedOperation = new MessageTemplate
{
Id = 1039,
Message = "The new version is adding an operation that was not found in the old version."
};

public static MessageTemplate RemovedRequiredParameter = new MessageTemplate
{
Id = 1009,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ public override IEnumerable<ComparisonMessage> Compare(ComparisonContext context
}
else
{
// 1. Remove this path from the current list to find the added paths
newPaths.Remove(p);
Dictionary<string, Operation> copyOfOperations = operations.ToDictionary(e => e.Key, e => e.Value);

// 2. look for operation match inside this path
Dictionary<string, Operation> previousOperations = previousDefinition.Paths[path];
foreach (var previousOperation in previousOperations)
{
Expand All @@ -205,8 +210,21 @@ public override IEnumerable<ComparisonMessage> Compare(ComparisonContext context
// Operation was removed from the path
context.LogBreakingChange(ComparisonMessages.RemovedOperation, previousOperation.Value.OperationId);
}
else
{
copyOfOperations.Remove(previousOperation.Key);
}
}

// Look for added operations
foreach (var copyOfOperation in copyOfOperations)
{
context.PushProperty(copyOfOperation.Key);
context.LogInfo(ComparisonMessages.AddedOperation);
context.Pop();
}

// Compare operations
foreach (var operation in operations)
{
Operation previousOperation = null;
Expand All @@ -220,10 +238,20 @@ public override IEnumerable<ComparisonMessage> Compare(ComparisonContext context
}
context.Pop();
}

// Check wether any new paths are being added
foreach (var path in newPaths.Keys)
{
context.PushProperty(path);
context.LogInfo(ComparisonMessages.AddedPath);
context.Pop();
}


context.Pop();

// Check for custom paths : x-ms-paths
newPaths = RemovePathVariables(CustomPaths);
var newCustomPaths = RemovePathVariables(CustomPaths);

context.PushProperty("x-ms-paths");
foreach (var path in previousDefinition.CustomPaths.Keys)
Expand All @@ -233,12 +261,17 @@ public override IEnumerable<ComparisonMessage> Compare(ComparisonContext context
context.PushProperty(path);

Dictionary<string, Operation> operations = null;
if (!newPaths.TryGetValue(p, out operations))
if (!newCustomPaths.TryGetValue(p, out operations))
{
context.LogBreakingChange(ComparisonMessages.RemovedPath, path);
}
else
{
// 1. Remove this custom path from the current list to find the added paths
newCustomPaths.Remove(p);
Dictionary<string, Operation> copyOfOperations = operations.ToDictionary(e => e.Key, e => e.Value);

// 2. look for operation match inside this path
Dictionary<string, Operation> previousOperations = previousDefinition.CustomPaths[path];
foreach (var previousOperation in previousOperations)
{
Expand All @@ -249,6 +282,15 @@ public override IEnumerable<ComparisonMessage> Compare(ComparisonContext context
}
}

// Look for added operations
foreach (var copyOfOperation in copyOfOperations)
{
context.PushProperty(copyOfOperation.Key);
context.LogInfo(ComparisonMessages.AddedOperation);
context.Pop();
}

// Compare operations
foreach (var operation in operations)
{
Operation previousOperation = null;
Expand All @@ -262,6 +304,15 @@ public override IEnumerable<ComparisonMessage> Compare(ComparisonContext context
}
context.Pop();
}

// Check wether any new paths are being added into x-ms-paths
foreach (var path in newCustomPaths.Keys)
{
context.PushProperty(path);
context.LogInfo(ComparisonMessages.AddedPath);
context.Pop();
}

context.Pop();

ReferenceTrackSchemas(this);
Expand Down