Skip to content

Commit

Permalink
Return empty string if field is null
Browse files Browse the repository at this point in the history
Up until now, if during reflection resolving a field was null,
the resolver continued search further up the object tree.
This should not happen, as a field being null also constitutes
information.
To fix this, from now on a ScalarPlaceholderData(null) is
returned, with the default formatting of null being the empty
string.
  • Loading branch information
AntonOellerer committed May 22, 2023
1 parent acf4e4e commit 8452a8e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.5.11'
version = '1.5.12'

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ private Optional<PlaceholderData> doReflectiveResolve(String placeholderName, Lo
}
var wrappedProperty = getBeanProperty(placeholderName);
if (wrappedProperty.isEmpty()) {
logger.debug("Placeholder {} could not be translated into a property", placeholderName);
return Optional.empty();
return Optional.of(new ScalarPlaceholderData<>(null));
}
var property = resolveNonFinalValue(wrappedProperty.get(), placeholderName);
var simplePlaceholder = resolveSimplePlaceholder(property, placeholderName, locale, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ScalarPlaceholderData<T> implements PlaceholderData {
private final Function<T, String> stringifier;

public ScalarPlaceholderData(T value) {
this(value, Objects::toString);
this(value, v -> Objects.toString(v, ""));
}

public ScalarPlaceholderData(T value, Function<T, String> stringifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static XWPFPicture insertImage(XWPFParagraph paragraph, Path path, ImageS
var contentType = probeImageType(path);

try (var in = Files.newInputStream(path, StandardOpenOption.READ)) {
logger.debug("Adding picture from path {} with content type {} and dimensions {} {}", path, contentType, dim.width, dim.height);
return paragraph.createRun()
.addPicture(in, contentType, path.getFileName().toString(), dim.width, dim.height);
} catch (InvalidFormatException | IOException e) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/docutools/jocument/ReflectionResolvingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,15 @@ void shouldResolveUUIDtoString() {

assertThat(id.get().toString(), equalTo(picardPerson.getId().toString()));
}

@Test
void shouldResolveNullToEmptyString() {
Person picardPerson = SampleModelData.PICARD_NULL;
var resolver = new ReflectionResolver(picardPerson);

var name = resolver.resolve("firstName");

assertThat(name.get().toString(), equalTo(""));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SampleModelData {
public static final Captain PICARD;
public static final FutureCaptain FUTURE_PICARD;
public static final Person PICARD_PERSON = new Person("Jean-Luc", "Picard", LocalDate.of(1948, 9, 23));
public static final Person PICARD_NULL = new Person(null, "Picard", LocalDate.of(1948, 9, 23));
public static final List<Captain> CAPTAINS;
public static final Ship ENTERPRISE;
public static final Ship ENTERPRISE_WITHOUT_SERVICES;
Expand Down

0 comments on commit 8452a8e

Please sign in to comment.