Skip to content

Commit

Permalink
Merge pull request #209 from DDS-GmbH/fix/objects-contain-null-value
Browse files Browse the repository at this point in the history
Return empty string if field is null
  • Loading branch information
alexpartsch authored May 22, 2023
2 parents bfabd1c + 53c42a0 commit 15b478b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Install libvips
run: sudo apt-get install -y libvips42
run: sudo apt-get update && sudo apt-get install -y libvips42
- name: Checkout
uses: actions/checkout@v3
- name: Setup Java
Expand Down
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 15b478b

Please sign in to comment.