Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent NPE when creating unknown metadata group and metadata #5603

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -578,16 +578,18 @@ public Collection<Metadata> getMetadata(boolean skipEmpty) throws InvalidMetadat
assert division == null;
MetadataGroup result = new MetadataGroup();
result.setKey(metadataKey);
result.setDomain(DOMAIN_TO_MDSEC.get(metadataView.getDomain().orElse(Domain.DESCRIPTION)));
try {
this.preserve();
} catch (NoSuchMetadataFieldException e) {
throw new IllegalStateException("never happening exception");
}
if (skipEmpty) {
result.setMetadata(metadata instanceof List ? metadata : new HashSet<>(metadata));
} else {
result.setMetadata(new HashSet<>(DataEditorService.getExistingMetadataRows(treeNode.getChildren())));
if (Objects.nonNull(metadataView)) {
result.setDomain(DOMAIN_TO_MDSEC.get(metadataView.getDomain().orElse(Domain.DESCRIPTION)));
try {
this.preserve();
} catch (NoSuchMetadataFieldException e) {
throw new IllegalStateException("never happening exception");
}
if (skipEmpty) {
result.setMetadata(metadata instanceof List ? metadata : new HashSet<>(metadata));
} else {
result.setMetadata(new HashSet<>(DataEditorService.getExistingMetadataRows(treeNode.getChildren())));
}
}
return result.getMetadata().isEmpty() ? Collections.emptyList() : Collections.singletonList(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ProcessTextMetadata(ProcessTextMetadata template) {
}

private String addLeadingZeros(String value) {
if (Objects.equals(super.settings.getInputType(), InputType.INTEGER)) {
if (Objects.nonNull(super.settings) && InputType.INTEGER.equals(super.settings.getInputType())) {
Copy link
Collaborator

@matthias-ronge matthias-ronge Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like better:

Objects.nonNull(super.settings) && Objects.equals(super.settings.getInputType(), InputType.INTEGER)

Not a requirement, but I think it is better readable. And, Objects.equals() does null-checks.

Copy link
Member Author

@solth solth Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this particular case I disagree. The additional Objects.equals is not required and unnecessarily complicates this check, since InputType.INTEGER is not null and therefor we do not need a null check here (calling the equals method on constants or enums when comparing with variables is an encouraged way for avoiding null pointer exceptions). So if you do not mind I would leave this part unchanged.

int valueLength = value.length();
int minDigits = super.settings.getMinDigits();
return valueLength >= minDigits ? value : "0".repeat(minDigits - valueLength).concat(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ public static List<SelectItem> getAddableMetadataForGroup(Ruleset ruleset, TreeN
ProcessFieldedMetadata fieldedMetadata = ((ProcessFieldedMetadata) metadataNode.getData());
ComplexMetadataViewInterface metadataView = fieldedMetadata.getMetadataView();
List<SelectItem> addableMetadata = new ArrayList<>();
for (MetadataViewInterface keyView : metadataView.getAddableMetadata(fieldedMetadata.getChildMetadata(),
fieldedMetadata.getAdditionallySelectedFields())) {
addableMetadata.add(
new SelectItem(keyView.getId(), keyView.getLabel(),
keyView instanceof SimpleMetadataViewInterface
? ((SimpleMetadataViewInterface) keyView).getInputType().toString()
: "dataTable"));
if (Objects.nonNull(metadataView)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why metadataView can be null. This looks like a bug elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you are correct. This pull request is not meant to fix the underlying problem of the metadata handling but is just intended to better deal with the resulting null pointer exception.

for (MetadataViewInterface keyView : metadataView.getAddableMetadata(fieldedMetadata.getChildMetadata(),
fieldedMetadata.getAdditionallySelectedFields())) {
addableMetadata.add(
new SelectItem(keyView.getId(), keyView.getLabel(),
keyView instanceof SimpleMetadataViewInterface
? ((SimpleMetadataViewInterface) keyView).getInputType().toString()
: "dataTable"));
}
}
return sortMetadataList(addableMetadata, ruleset);
}
Expand Down