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