Skip to content

Commit

Permalink
[Bugfix] Detect more schema type reference cycles. (#335)
Browse files Browse the repository at this point in the history
[Bugfix] Detect more schema type reference cycles.

### Motivation

The newly introduced recursive type support had a slight optimization to minimize the number of boxed types, but it contained a bug that only surfaced on larger real-world documents. It's not really necessary, so removed it and the number of boxed types is still reasonable, plus it doesn't lead to non-compiling code anymore.

### Modifications

Removed an optimization that tried to further reduce the number of boxed types, because it actually missed some cycles and produced non-compiling code for some larger OpenAPI documents.

### Result

Code now compiles even for more complex cycles.

### Test Plan

Added more unit tests, plus tested on some real-world docs with cycles that failed before this change.


Reviewed by: glbrntt

Builds:
     ✔︎ pull request validation (5.10) - Build finished. 
     ✔︎ pull request validation (5.8) - Build finished. 
     ✔︎ pull request validation (5.9) - Build finished. 
     ✔︎ pull request validation (compatibility test) - Build finished. 
     ✔︎ pull request validation (docc test) - Build finished. 
     ✔︎ pull request validation (integration test) - Build finished. 
     ✔︎ pull request validation (nightly) - Build finished. 
     ✔︎ pull request validation (soundness) - Build finished. 

#335
  • Loading branch information
czechboy0 authored Oct 23, 2023
1 parent c67892a commit 978e498
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ struct RecursionDetector {
// document.
// - Walk all references and keep track of names already visited.
// - If visiting a schema that is already in the stack, we found a cycle.
// - In the cycle, first identify the set of types involved in it, and
// check if any of the types is already recorded as a recursive type.
// If so, no action needed and terminate this branch and continue with
// the next one.
// - If no type in the cycle is already included in the set of recursive
// types, find the first boxable type starting from the current one
// - Find the first boxable type starting from the current one
// ("causing" the recursion) following the cycle, and add it to this
// set, and then terminate this branch and continue.
// - At the end, return the set of recursive types.
Expand All @@ -102,21 +97,23 @@ struct RecursionDetector {

func visit(_ node: Node) throws {
let name = node.name
let previousStackSet = stackSet

// Add to the stack.
stack.append(node)
stackSet.insert(name)
defer {
stackSet.remove(name)
stack.removeLast()
}

// Check if we've seen this node yet.
if !seen.contains(name) {

// Add to the stack.
stack.append(node)
stackSet.insert(name)
defer {
stackSet.remove(name)
stack.removeLast()
}

// Not seen this node yet, so add it to seen, and then
// visit its edges.
seen.insert(name)

for edge in node.edges {
try visit(container.lookup(edge))
}
Expand All @@ -125,42 +122,38 @@ struct RecursionDetector {

// We have seen this node.

// If the name is not in the stack, this is not a cycle.
if !stackSet.contains(name) {
// If the name is not in the stack twice, this is not a cycle.
if !previousStackSet.contains(name) {
return
}

// It is in the stack, so we just closed a cycle.
// It is in the stack twice, so we just closed a cycle.

// Identify the names involved in the cycle.
// Right now, the stack must have the current node there twice.
// Ignore everything before the first occurrence.

let cycleNodes = stack.drop(while: { $0.name != name })
let cycleNames = Set(cycleNodes.map(\.name))

// Check if any of the names are already boxed.
if cycleNames.contains(where: { boxed.contains($0) }) {
// Found one, so we know this cycle will already be broken.
// No need to add any other type, just return from this
// visit.
return
}

// We now choose which node will be marked as recursive.
// Only consider boxable nodes, trying from the start of the cycle.
guard let firstBoxable = cycleNodes.first(where: \.isBoxable) else {
throw RecursionError.invalidRecursion(name.description)
}

let nameToAdd = firstBoxable.name

// Check if we're already going to box this type, if so, we're done.
if boxed.contains(nameToAdd) {
return
}

// None of the types are boxed yet, so add the current node.
boxed.insert(firstBoxable.name)
boxed.insert(nameToAdd)
}

for node in rootNodes {
try visit(node)
}

return boxed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ The algorithm outputs a list of type names that require boxing.

It iterates over the types defined in `#/components/schemas`, in the order defined in the OpenAPI document, and for each type walks all of its references.

Once it detects a reference cycle, it checks whether any of the types involved in the current cycle are already in the list, and if so, considers this cycle to already be addressed.

If no type in the current cycle is found in the list, it adds the first type in the cycle, in other words the one to which the last reference closed the cycle.
Once it detects a reference cycle, it adds the first type in the cycle, in other words the one to which the last reference closed the cycle.

For example, walking the following:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ class Test_RecursionDetector_Generic: Test_Core {
)
}

func testMultipleCycles3() throws {
try _test(
rootNodes: [
"A",
"B",
"C",
"D",
],
putIntoContainer: [
"A -> C",
"B -> D,A",
"C -> B,D",
"D -> B,C",
],
expected: [
"A",
"B",
"C",
]
)
}

func testNested() throws {
try _test(
rootNodes: [
Expand Down Expand Up @@ -273,6 +295,25 @@ class Test_RecursionDetector_Generic: Test_Core {
)
}

func testDifferentCyclesForSameNode() throws {
try _test(
rootNodes: [
"C",
"A",
"B",
],
putIntoContainer: [
"A -> B",
"B -> C,A",
"C -> A",
],
expected: [
"C",
"A",
]
)
}

// MARK: - Private

private func _test(
Expand Down

0 comments on commit 978e498

Please sign in to comment.