Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Detect more schema type reference cycles. #335

Merged
merged 1 commit into from
Oct 23, 2023
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 @@ -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.
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved
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