Skip to content

Commit

Permalink
deffensive check agains empty addresses in grscicoll suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Apr 25, 2024
1 parent 683e48f commit 68c9652
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public void newEntitySuggestionTest() {
address.setCountry(Country.DENMARK);
entity.setAddress(address);

Address emptyAddress = new Address();
entity.setMailingAddress(emptyAddress);

Contact contact1 = new Contact();
contact1.setFirstName("first");
contact1.setLastName("last");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ private Set<ChangeDto> extractChanges(T suggestedEntity, T currentEntity) {
+ field.getName().substring(1))
.invoke(suggestedEntity);

if (suggestedValue instanceof Address && isEmptyAddress((Address) suggestedValue)) {
suggestedValue = null;
}

Object previousValue =
clazz
.getMethod(
Expand All @@ -572,6 +576,10 @@ private Set<ChangeDto> extractChanges(T suggestedEntity, T currentEntity) {
+ field.getName().substring(1))
.invoke(currentEntity);

if (previousValue instanceof Address && isEmptyAddress((Address) previousValue)) {
previousValue = null;
}

if (isDifferentValue(suggestedValue, previousValue)) {
changes.add(createChangeDto(field, suggestedValue, previousValue, field.getType()));
}
Expand Down Expand Up @@ -826,6 +834,15 @@ protected boolean hasRightsToSeeProposerEmail(ChangeSuggestionDto dto) {
dto.getKey(), dto.getEntityType().name().toLowerCase(), authentication);
}

private boolean isEmptyAddress(Address address) {
return address != null
&& Strings.isNullOrEmpty(address.getAddress())
&& Strings.isNullOrEmpty(address.getCity())
&& Strings.isNullOrEmpty(address.getPostalCode())
&& Strings.isNullOrEmpty(address.getProvince())
&& address.getCountry() == null;
}

protected abstract R newEmptyChangeSuggestion();

protected abstract ChangeSuggestionDto createConvertToCollectionSuggestionDto(R changeSuggestion);
Expand Down

0 comments on commit 68c9652

Please sign in to comment.