Probable bug: method that nullifies model (#1735)

* Sus method that nullifies model
* Make it clear
* Same change in new API
* Fix the logic to return the old value when unchanged

---------

Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
This commit is contained in:
Tamas Cservenak 2024-10-01 18:55:06 +02:00 committed by GitHub
parent 82cf96094f
commit 1062d05cb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 6 deletions

View File

@ -94,7 +94,7 @@ public class DefaultModelPathTranslator implements ModelPathTranslator {
for (int i = 0; i < resources.size(); i++) {
T resource = resources.get(i);
T newResource = mapper.apply(resource);
if (newResource != null) {
if (newResource != resource) {
if (newResources == null) {
newResources = new ArrayList<>(resources);
}
@ -105,18 +105,39 @@ public class DefaultModelPathTranslator implements ModelPathTranslator {
return newResources;
}
/**
* 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 newDir = alignToBaseDirectory(resource.getDirectory(), basedir);
if (newDir != null) {
return resource.withDirectory(newDir);
String oldDir = resource.getDirectory();
String newDir = alignToBaseDirectory(oldDir, basedir);
if (newDir != oldDir) {
return resource.with().directory(newDir).build();
}
}
return resource;
}
/**
* 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) ? null : newPath;
return Objects.equals(path, newPath) ? path : newPath;
}
}

View File

@ -118,7 +118,7 @@ public class DefaultModelPathTranslator implements ModelPathTranslator {
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) {
return resource.withDirectory(newDir);
}
@ -127,6 +127,17 @@ public class DefaultModelPathTranslator implements ModelPathTranslator {
}
private String alignToBaseDirectory(String path, Path basedir) {
String newPath = mayAlignToBaseDirectoryOrNull(path, basedir);
if (newPath != null) {
return newPath;
}
return path;
}
/**
* 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;
}