Skip to content

Commit

Permalink
Modify the circular detection logic for circular 'allOf' inheritance …
Browse files Browse the repository at this point in the history
…so that there can again be a passing test 'ClientModelWithCircularDependencyThrowsError'.
  • Loading branch information
TimLovellSmith committed May 23, 2016
1 parent 0ba790b commit 5f97447
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion AutoRest/Modelers/Swagger.Tests/SwaggerModelerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ public void ClientModelWithCircularDependencyThrowsError()
Namespace = "Test",
Input = Path.Combine("Swagger", "swagger-allOf-circular.json")
});
Assert.Throws<ArgumentException>(() => modeler.Build());
var ex = Assert.Throws<InvalidOperationException>(() => modeler.Build());
Assert.Contains("circular", ex.Message, StringComparison.InvariantCultureIgnoreCase);
Assert.Contains("siamese", ex.Message, StringComparison.InvariantCultureIgnoreCase);
}

[Fact]
Expand Down
9 changes: 9 additions & 0 deletions AutoRest/Modelers/Swagger/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions AutoRest/Modelers/Swagger/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="CircularBaseSchemaSet" xml:space="preserve">
<value>Found a type set '{0}' which is circularly defined.</value>
</data>
<data name="CircularReference" xml:space="preserve">
<value>Circular reference detected: {0}</value>
</data>
Expand Down
31 changes: 31 additions & 0 deletions AutoRest/Modelers/Swagger/SchemaResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public void ExpandAllOf(Schema schema)

if (schema.AllOf != null)
{
CheckCircularAllOf(schema, null, null);
var references = schema.AllOf.Where(s => s.Reference != null).ToList();

if (references.Count == 1)
{
if (schema.Extends != null)
Expand All @@ -101,6 +103,7 @@ public void ExpandAllOf(Schema schema)
Properties =
schema.Properties
};

var schemaList =
new List<Schema>().Concat(schema.AllOf)
.Concat(new List<Schema> {propertiesOnlySchema});
Expand Down Expand Up @@ -170,6 +173,34 @@ public void ExpandAllOf(Schema schema)
}
}

void CheckCircularAllOf(Schema schema, HashSet<Schema> visited, Stack<string> referenceChain)
{
visited = visited ?? new HashSet<Schema>();
referenceChain = referenceChain ?? new Stack<string>();
if (!visited.Add(schema)) // was already present in the set
{
var setDescription = "(" + String.Join(", ", referenceChain) + ")";
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture,
Properties.Resources.CircularBaseSchemaSet, setDescription));
}

if (schema.AllOf != null)
{
foreach (var reference in schema.AllOf.Select(s => s.Reference).Where(r => r != null))
{
referenceChain.Push(reference);

var deref = Dereference(reference);
CheckCircularAllOf(deref, visited, referenceChain);

Debug.Assert(reference == referenceChain.Peek());
referenceChain.Pop();
}
}
visited.Remove(schema);
}

/// <summary>
/// Determine equivalence between the types described by two schemas.
/// Limit the comparison to exclude comparison of complexe inline schemas.
Expand Down

0 comments on commit 5f97447

Please sign in to comment.