From 9c75743dddcb6aa6ef9ec20270ecc1f687a9ed11 Mon Sep 17 00:00:00 2001 From: Dusan Jakub Date: Thu, 20 Oct 2022 08:55:08 +0200 Subject: [PATCH] Fix StackOverflow on cyclic references involving collections. Fixes fabric8io/kubernetes-client#4510 --- .../generator/visitor/ClassDependenciesVisitor.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/visitor/ClassDependenciesVisitor.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/visitor/ClassDependenciesVisitor.java index 93b733f885e..a143a3300fe 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/visitor/ClassDependenciesVisitor.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/visitor/ClassDependenciesVisitor.java @@ -33,7 +33,7 @@ public class ClassDependenciesVisitor extends TypedVisitor { private static final Map> traversedClasses = new HashMap<>(); private static final Map> crdNameToCrClass = new HashMap<>(); private final Set classesForCR; - private final Set processed = new HashSet<>(); + private final Set processed = new HashSet<>(); public ClassDependenciesVisitor(String crClassName, String crdName) { // need to record all classes associated with the different versions of the CR (not the CRD spec) @@ -49,7 +49,7 @@ public void visit(TypeDefBuilder builder) { // note that we cannot simply check the traversed class set to know if a class has been processed because classes // are usually added to the traversed set before they're looked at in depth final String className = type.getFullyQualifiedName(); - if (ignore(className) || processed.contains(className)) { + if (ignore(className)) { return; } @@ -74,9 +74,6 @@ public void visit(TypeDefBuilder builder) { // add classes from extends list type.getExtendsList().forEach(this::processTypeRef); - - // add class to the processed classes - processed.add(className); } private boolean ignore(String className) { @@ -86,7 +83,11 @@ private boolean ignore(String className) { private void processTypeRef(TypeRef t) { if (t instanceof ClassRef) { ClassRef classRef = (ClassRef) t; - visit(new TypeDefBuilder(Types.typeDefFrom(classRef))); + // only process the class reference if we haven't already + // note that the references are stored in the set including type arguments, so List and List are not the same + if (processed.add(classRef)) { + visit(new TypeDefBuilder(Types.typeDefFrom(classRef))); + } } }