diff --git a/extensions/resteasy-reactive/quarkus-resteasy-reactive-links/deployment/src/main/java/io/quarkus/resteasy/reactive/links/deployment/LinksProcessor.java b/extensions/resteasy-reactive/quarkus-resteasy-reactive-links/deployment/src/main/java/io/quarkus/resteasy/reactive/links/deployment/LinksProcessor.java index 2066c8c497d4a..6d98dafd087a2 100644 --- a/extensions/resteasy-reactive/quarkus-resteasy-reactive-links/deployment/src/main/java/io/quarkus/resteasy/reactive/links/deployment/LinksProcessor.java +++ b/extensions/resteasy-reactive/quarkus-resteasy-reactive-links/deployment/src/main/java/io/quarkus/resteasy/reactive/links/deployment/LinksProcessor.java @@ -4,7 +4,6 @@ import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.OBJECT_NAME; import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.function.Function; @@ -206,33 +205,42 @@ private RuntimeValue implementPathParameterValueGetter private void validateClassHasFieldId(IndexView index, String entityType) { // create a new independent class name that we can override DotName className = DotName.createSimple(entityType); - List classFields = index.getClassByName(className).fields(); - List classAnnotations = index.getClassByName(className).annotations(); - - LinkedList allFields = new LinkedList<>(classFields); - LinkedList allAnnotations = new LinkedList<>(classAnnotations); - - // also get fields and annotations from all super classes - DotName superClassName = index.getClassByName(className).superName(); - while (superClassName != null) { - List allSuperFields = index.getClassByName(superClassName).fields(); - List allSuperAnnotations = index.getClassByName(superClassName).annotations(); - allFields.addAll(allSuperFields); - allAnnotations.addAll(allSuperAnnotations); - - className = superClassName; - superClassName = index.getClassByName(className).superName(); + ClassInfo classInfo = index.getClassByName(className); + + if (classInfo != null) { + validateRec(index, entityType, classInfo); } + } - List fieldsNamedId = allFields.stream().filter(f -> f.name().equals("id")).collect(Collectors.toList()); - List fieldsAnnotatedWithId = allAnnotations.stream() + /** + * Validates if the given classname contains a field `id` or annotated with `@Id` + * + * @throws IllegalStateException if the classname does not contain any sort of field identifier + */ + private void validateRec(IndexView index, String entityType, ClassInfo classInfo) { + List fieldsNamedId = classInfo.fields().stream() + .filter(f -> f.name().equals("id")) + .collect(Collectors.toList()); + List fieldsAnnotatedWithId = classInfo.annotations().stream() .filter(a -> a.name().toString().endsWith("persistence.Id")) .collect(Collectors.toList()); - if (fieldsNamedId.isEmpty() && fieldsAnnotatedWithId.isEmpty()) { + // Id field found, break the loop + if (!fieldsNamedId.isEmpty() || !fieldsAnnotatedWithId.isEmpty()) + return; + + // Id field not found and hope is gone + DotName superClassName = classInfo.superName(); + if (fieldsNamedId.isEmpty() && fieldsAnnotatedWithId.isEmpty() && superClassName == null) { throw new IllegalStateException("Cannot generate web links for the class " + entityType + " because is either missing an `id` field or a field with an `@Id` annotation"); } + + // Id field not found but there's still hope + classInfo = index.getClassByName(superClassName); + if (superClassName != null && classInfo != null) { + validateRec(index, entityType, classInfo); + } } /**