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

restapi: prefer parentType over subclasses in getParent #320

Merged
merged 1 commit into from
May 2, 2022
Merged
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 @@ -225,18 +225,26 @@ private static void unsetInlineResource(BaseResource model, Class<?> type) {
* @return the parent object, or null if not set
*/
private static <R extends BaseResource> BaseResource getParent(R model, Class<?> parentType) {
Object assignedFromParent = null;

// Try to match a method whose parent matches parentType first, only fallback to a subclass if parentType was
// not found in the list.
for (Method method : getRelevantMethods(model.getClass())) {
try {
Object potentialParent = method.invoke(model);
if (potentialParent != null && potentialParent.getClass().equals(parentType)) {
return (BaseResource) potentialParent;
}
if (potentialParent != null && parentType.isAssignableFrom(potentialParent.getClass())) {
return (BaseResource)potentialParent;
assignedFromParent = potentialParent;
}
} catch (Exception e) {
log.error("Error invoking method when adding links to an API entity", e);
continue;
}
}
return null;

return (BaseResource) assignedFromParent;
}

/**
Expand Down