Skip to content

Commit

Permalink
Validate that the class has or inherits an id field or an @Id annot…
Browse files Browse the repository at this point in the history
…ated field
  • Loading branch information
leo-bogastry committed Dec 5, 2023
1 parent 6f8da9a commit 0a2558b
Showing 1 changed file with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -206,33 +205,42 @@ private RuntimeValue<GetterAccessorsContainer> implementPathParameterValueGetter
private void validateClassHasFieldId(IndexView index, String entityType) {
// create a new independent class name that we can override
DotName className = DotName.createSimple(entityType);
List<FieldInfo> classFields = index.getClassByName(className).fields();
List<AnnotationInstance> classAnnotations = index.getClassByName(className).annotations();

LinkedList<FieldInfo> allFields = new LinkedList<>(classFields);
LinkedList<AnnotationInstance> allAnnotations = new LinkedList<>(classAnnotations);

// also get fields and annotations from all super classes
DotName superClassName = index.getClassByName(className).superName();
while (superClassName != null) {
List<FieldInfo> allSuperFields = index.getClassByName(superClassName).fields();
List<AnnotationInstance> 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<FieldInfo> fieldsNamedId = allFields.stream().filter(f -> f.name().equals("id")).collect(Collectors.toList());
List<AnnotationInstance> 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<FieldInfo> fieldsNamedId = classInfo.fields().stream()
.filter(f -> f.name().equals("id"))
.collect(Collectors.toList());
List<AnnotationInstance> 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);
}
}

/**
Expand Down

0 comments on commit 0a2558b

Please sign in to comment.