-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Fixes #5521 ResourceCollection NPE #5527
Conversation
Fix constructor and addPath so that all resources in a RC must exist when created.
@joakime So the approach I've taken is to make all methods check that all resources exist - rather than just some. I just don't think we should have some that allow and some that don't. Thoughts? |
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.
Just a question.
if (resources == null) | ||
{ | ||
if (addedResource != null) | ||
return addedResource; // This will not exist |
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.
Is it possible to get here if there are no resources in the RC?
I thought we prevented that in the constructors.
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.
perhaps not... but I think the EmptyResource is a good safety net in case anything changes.
Although I'm now thinking more fondly of allowing a RC to have directories that don't exist... as we can't prevent them from being deleted anyway.
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.
The addPath method IMHO is problematic:
- the javadoc is wrong, it does NOT only return the first contained resource, it can return a ResourceCollection of directories
- what is returned is inconsistent: you either get back a single directory that does not exist, or a ResourceCollection of directories that do
- you get different results depending on the implementation of addPath by the various Resource subclasses: for example, PathResource will throw MalformedURLException if you give it a path that tries to escape above the root, but URLResource will just return you the original path
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.
I agree the javadoc could be improved, but I don't see the problem with the variable return, as that is just what all the different scenarioes that do exist. Specifically if the result is
- a file that exists in at least one of the collection, then the first one found is returned.
- a subdirectory that exists in at exactly one of the collection, then that directory is returned
- a subdirectory that exists in at more than one of the collection, then a resource collection is returned
- a resource that doesn't exist in any of the collection, then the non existent resource of the first collection is returned.
- if any of the collection is asked to form a resource that it cannot handle, then MalformedURLException is thrown
If I updated the javadoc to say that, then would that be OK?
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.
- a file that exists in at least one of the collection, then the first one found is returned.
Makes sense
- a subdirectory that exists in at exactly one of the collection, then that directory is returned
Yup, agreed.
- a subdirectory that exists in at more than one of the collection, then a resource collection is returned
This makes sense to me, a sub-collection.
But what if the asked for path has hits for both for files and directories (of the same addPath) across the RC?
What does that mean?
Example:
The file /tmp/foo/zed exists
The directory /tmp/bar/zed/ exists
RC of [/tmp/foo, /tmp/bar] is created.
RC.addPath("zed") means what?
Is this case 1?
- a resource that doesn't exist in any of the collection, then the non existent resource of the first collection is returned.
This is a tough one.
I could argue that the return on this should be another ResourceCollection (of the same size) with each entry being the original RC with the non-existent path tacked on.
But what is the value on either approach?
In both approaches, a user of the Resource would test for exists() on the result and then move on.
But in the second approach, we allow for "potential" resources.
But that only works for directories IMO.
Having potential files across multiple hits would just be extra confusing.
Yeah, I'm not sure there is a perfect solution here.
- if any of the collection is asked to form a resource that it cannot handle, then MalformedURLException is thrown
Yup, this one makes sense too.
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.
@joakime and @janbartel we can only merge directories, we cannot merge files, nor can we merge files with directories nor directories with errors or files with errors.
ResourceCollection is implementing defacto standard directory overlays as done by things like docker and fancy file systems. If you mount layer A on top of layer B, then files in A replace files in B; if you list files then you see the union of files in A & B; if you create a new file it is created in A.
I believe the semantics we have implemented does that.
... and also we are just fixing NPE here, not re-inventing this class :)
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.
The questions still stand.
Even if they are not relevant for this PR, they could lead to filing a new issue and PR.
If you are going to fix the javadoc here, then the javadoc needs to be clear.
So far, your first proposal for the javadoc changes has vague parts (hence the questions).
IMO, If there are known warts, then the javadoc should point out those warts.
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.
- a file that exists in at least one of the collection, then the first one found is returned.
OK.
- a subdirectory that exists in at exactly one of the collection, then that directory is returned
OK.
- a subdirectory that exists in at more than one of the collection, then a resource collection is returned
OK (so long as it is javadoc'ed).
- a resource that doesn't exist in any of the collection, then the non existent resource of the first collection is returned.
? What do you mean "the non-existent resource of the first collection"??? I'm not in favour of returning something that is known not to exist. The caller should be able to expect consistency: either this method returns something that exists, or it throws an exception.
- if any of the collection is asked to form a resource that it cannot handle, then MalformedURLException is thrown
OK.
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.
@janbartel We have always been able to get resources that do not exist. In fact that is how we can create new resources eg when implementing PUT methods.
I'll update the javadoc.... but remember we have a real NPE here that we need to fix for a 10.0.0 before we go redesigning the entire resource contract.
Cleanup the vestiges of non existent directories detected by resource ending in /
…5521-ResourceCollection-NPE
Revert adding paths ending in / as jar:file resource needs them
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.
I think addPath needs work. Happy to discuss.
if (resources == null) | ||
{ | ||
if (addedResource != null) | ||
return addedResource; // This will not exist |
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.
The addPath method IMHO is problematic:
- the javadoc is wrong, it does NOT only return the first contained resource, it can return a ResourceCollection of directories
- what is returned is inconsistent: you either get back a single directory that does not exist, or a ResourceCollection of directories that do
- you get different results depending on the implementation of addPath by the various Resource subclasses: for example, PathResource will throw MalformedURLException if you give it a path that tries to escape above the root, but URLResource will just return you the original path
improved javadoc.
Fix constructor and addPath so that all resources in a RC must exist when created for #5521