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

Implement recursion guard for aggregate inspection #271

Merged
merged 1 commit into from
Dec 12, 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 @@ -97,13 +97,18 @@ class AggregateStructureResolver(private val project: Project) {
.flatMap {
AnnotatedElementsSearch.searchPsiClasses(it.psiClass, project.axonScope()).findAll()
}
.mapNotNull { inspect(it, null) }
.mapNotNull { inspect(it, emptyList()) }
.flatMap { it.flatten() }

private fun inspect(clazz: PsiClass, parent: PsiClass?): Entity? {
private fun inspect(clazz: PsiClass, parents: List<PsiClass>, depth: Int = 0): Entity? {
if (clazz.isEnum) {
return null
}
if(parents.contains(clazz) || depth > 20) {
// Guard for infinite recursion; we already have this class indexed, or we exceed an exorbitant depth
return null
}
val parent = parents.lastOrNull()
val children = clazz.fields.toList()
.filter { it.isAnnotated(AxonAnnotation.AGGREGATE_MEMBER) }
.mapNotNull { field ->
Expand All @@ -112,7 +117,7 @@ class AggregateStructureResolver(private val project: Project) {
val qualifiedName = psiType.toQualifiedName() ?: return@mapNotNull null
val targetClass = clazz.javaFacade().findClass(qualifiedName, clazz.project.axonScope())
?: return@mapNotNull null
val modelMember = inspect(targetClass, clazz) ?: return@mapNotNull null
val modelMember = inspect(targetClass, parents + clazz, depth + 1) ?: return@mapNotNull null
val routingKey = field.resolveAnnotationStringValue(AxonAnnotation.AGGREGATE_MEMBER, "routingKey")
val eventForwardingMode = field.resolveAnnotationClassValue(AxonAnnotation.AGGREGATE_MEMBER, "eventForwardingMode")
EntityMember(field, field.name, modelMember, isCollection, routingKey, eventForwardingMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ class ClassLineMarkerProviderTest : AbstractAxonFixtureTestCase() {
)
}

fun `test shows line marker on aggregate of hierarchy when recursive`() {
addFile(
"MyAggregate.kt", """
class RecursiveAggregateMember {
@AggregateMember
private var subItems: List<RecursiveAggregateMember>
}

@AggregateRoot
class MyAggregate {<caret>
@AggregateMember
private lateinit var singleMember: RecursiveAggregateMember
}
""".trimIndent(), open = true
)
Assertions.assertThat(hasLineMarker(ClassLineMarkerProvider::class.java)).isTrue
Assertions.assertThat(getLineMarkerOptions(ClassLineMarkerProvider::class.java)).containsExactly(
OptionSummary("MyAggregate", null, AxonIcons.Axon),
OptionSummary("- RecursiveAggregateMember", null, AxonIcons.Axon),
)
}

fun `test shows line marker on child of hierarchy`() {
addFile(
"MyAggregate.kt", """
Expand Down
Loading