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

Patch for StackOverflow on Recursive Class Definitions. #1408

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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 @@ -93,7 +93,12 @@ internal fun serialName(typeName: String, serializers: List<KSerializer<*>>): St
}
}

private fun descriptorName(typeName: SerialDescriptor): String {
private fun descriptorName(typeName: SerialDescriptor, visited: MutableSet<SerialDescriptor> = mutableSetOf()): String {
// Check if we've already visited this descriptor to avoid infinite recursion
if (!visited.add(typeName)) {
return typeName.serialName
}

return buildString {
append(typeName.serialName)

Expand All @@ -102,7 +107,7 @@ private fun descriptorName(typeName: SerialDescriptor): String {

val elementIndices = 0 until typeName.elementsCount
for (i in elementIndices) {
append(descriptorName(typeName.getElementDescriptor(i)))
append(descriptorName(typeName.getElementDescriptor(i), visited))
if (i < elementIndices.last) {
append(',')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package app.cash.zipline.internal.bridge

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

class SerialNameTest {
Expand Down Expand Up @@ -79,4 +80,26 @@ class SerialNameTest {
serialName,
)
}

@Test
fun recursiveSerializer() {
val serialName = serialName(
"SomeType",
serializers = listOf(
serializer<SomeRecursiveType>(),
),
)
assertEquals(
expected = "SomeType<" +
"app.cash.zipline.internal.bridge.SomeRecursiveType<" +
"app.cash.zipline.internal.bridge.SomeRecursiveType?<app.cash.zipline.internal.bridge.SomeRecursiveType?>" +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's necessary to include the second copy here. If we break before recursing it won't be included, and isn't it entirely redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JakeWharton you are correct, it does exit, in above:

  1. 1st serialName of Non-Nullable SomeRecursiveType.
  2. 1st serialName of Nullable SomeRecursiveType.
  3. 2nd Recurse serialName of Nullable SomeRecursiveType, hence exits with plain serialName of descriptor.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right my question was if the second copy is actually necessary or redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AhUnderstood what you mean,
Yes we could just return empty string once we hit recursion instead of repeating serialName, shouldn't matter.

">>",
actual = serialName,
)
}
}

@Serializable
private data class SomeRecursiveType(
val someData: SomeRecursiveType?,
)
Loading