-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Probable bug: method that nullifies model #1735
Conversation
/** | ||
* Returns aligned path or {@code null} if no need for change. | ||
*/ | ||
private String mayAlignToBaseDirectoryOrNull(String path, Path basedir) { | ||
String newPath = pathTranslator.alignToBaseDirectory(path, basedir); | ||
return Objects.equals(path, newPath) ? null : newPath; |
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 the above line should be removed.
@@ -107,7 +107,7 @@ private <T> List<T> map(List<T> resources, Function<T, T> mapper) { | |||
|
|||
private Resource alignToBaseDirectory(Resource resource, Path basedir) { | |||
if (resource != null) { | |||
String newDir = alignToBaseDirectory(resource.getDirectory(), basedir); | |||
String newDir = mayAlignToBaseDirectoryOrNull(resource.getDirectory(), basedir); | |||
if (newDir != null) { |
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.
This check should be changed to if (!Objects.equals(newDir, resource.getDirectory())
/** | ||
* Returns aligned path or {@code null} if no need for change. | ||
*/ | ||
private String mayAlignToBaseDirectoryOrNull(String path, Path basedir) { |
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.
Remove this new method.
Originally, the code had the following:
without the null return value. |
A slight amendment is to modify the method as below (with /**
* Returns a path relocated to the given base directory. If the result of this operation
* is the same path as before, then this method returns the old {@code path} instance.
* It is okay for the caller to compare the {@link String} instances using the identity
* comparator for detecting changes.
*
* @param path the path to relocate, or {@code null}
* @param basedir the new base directory
* @return relocated path, or {@code null} if the given path was null
*/
private String alignToBaseDirectory(String path, Path basedir) {
String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
return Objects.equals(path, newPath) ? path : newPath;
} And replace null check by identity check: /**
* Returns a resource with all properties identical to the given resource, except the paths
* which are resolved according the given {@code basedir}. If the paths are unchanged, then
* this method returns the previous instance.
*
* @param resource the resource to relocate, or {@code null}
* @param basedir the new base directory
* @return relocated resource, or {@code null} if the given resource was null
*/
@SuppressWarnings("StringEquality") // Identity comparison is ok in this method.
private Resource alignToBaseDirectory(Resource resource, Path basedir) {
if (resource != null) {
String oldDir = resource.getDirectory();
String newDir = alignToBaseDirectory(oldDir, basedir);
if (newDir != oldDir) {
return resource.withDirectory(newDir);
}
}
return resource;
} |
Good point. In order to optimise things further, I think the above function needs to return |
I was thinking to cascade the identity checks, as |
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.
LGTM
@desruisseaux do you want to add anything to this PR ?
Nothing to add. Thanks |
As discovered by @desruisseaux , this method is once used for "change detection" (if result is not null, apply change), and once directly where it -- probably by mistake -- nullifies the field value.
@gnodet