-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix bug where empty metadata List can result in "Index 0 out of bounds for length 0" exceptions in several scenarios #9509
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tdonohue,
IMHO the issue is caused by this hack org.dspace.content.DSpaceObjectServiceImpl.addMetadata(Context, T, MetadataField, String, List<String>, List<String>, List<Integer>, Supplier<Integer>)
around line 255
if (authorities != null && authorities.size() >= i) {
if (StringUtils.startsWith(authorities.get(i), Constants.VIRTUAL_AUTHORITY_PREFIX)) {
continue;
}
}
that was obviously introduced to deal with management of relationship and virtual field.
I argue that it was a hack because it looks odd to me that a method named addMetadata
should eventually return an empty list (i.e. don't add anything).
It is clear that modify the addMetadata method was an easier solution compared to rethink the way that virtual metadata are managed in dspace or to check all the places that would potentially pass a virtual metadata to the ItemServiceImpl but, IMHO, this is a clear sign of bad design that we should note as an "architecture technical debt".
That said, I'm ok with the changes that you have provided but I would suggest two improvements:
- we need to add a test that demonstrate the bug and can be used to avoid regression in future refactoring (if my analysis is right an AIP containing virtual metadata should be enough)
- we should check in the
addMetadata
method that the list received are not empty, otherwise we should throw an IllegalArgumentException. This to avoid that the changes would hide future bugs where the calling code is populating the list wrongly
This PR also fixes the newly reported bug DSpace/dspace-angular#3072, which can be reproduced by attempting to version an Entity that has relationships, as this same empty metadata list can be encountered (at least in some scenarios). I'm working on addressing @abollini 's feedback to ensure this can be merged. UPDATE: In the case of DSpace/dspace-angular#3072, the error there also seems to be caused by virtual metadata existing on the Entity that is being versioned. So, this bug is generally an issue with encountering empty metadata lists which seems to be caused by virtual metadata. |
…. Add unit tests to prove it works
48862cd
to
ed918a8
Compare
…adata_5args_1. It appears this second test was meant to test a different addMetadata() method which accepts a single Value instead of a List
…y values. Minor refactors to MetadataImportIT to make findItemByName more efficient.
@abollini : Thank you for your feedback on this PR! As I dug into your suspicion, I found that you are 100% correct in your analysis. The I took the approaches you suggested with a few minor modifications:
When you have a chance, please give this another review. I believe it now has sufficient tests to prove the bug exists on |
References
Description
This PR fixes scenarios where an empty metadata List can result in an "Index 0 out of bounds for length 0" exception.
There are (at least) two scenarios where I've encountered this exception:
This PR is a small fix to avoid the error. All it does is ensure the List is not empty before returning first value
Instructions for Reviewers
getMetadataFirstValue()
methods in the sameDSpaceObjectServiceImpl