Fix NPE in UpgradeTool when no pattern present (#3630)

Reported on the forum. State descriptions do no necessarily contain a pattern and thus `.get("pattern")` can return null.

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-05-25 19:19:06 +02:00 committed by GitHub
parent 0df4857b11
commit a8a5e86fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -94,27 +94,31 @@ public class Upgrader {
ManagedItemProvider.PersistedItem item = itemStorage.get(itemName);
if (item != null && item.itemType.startsWith("Number:")) {
if (metadataStorage.containsKey(NumberItem.UNIT_METADATA_NAMESPACE + ":" + itemName)) {
logger.debug("{}: already contains a 'unit' metadata, skipping it", itemName);
logger.debug("{}: Already contains a 'unit' metadata, skipping it", itemName);
} else {
Metadata metadata = metadataStorage.get("stateDescription:" + itemName);
if (metadata == null) {
logger.debug("{}: Nothing to do, no state description found.", itemName);
} else {
String pattern = (String) metadata.getConfiguration().get("pattern");
if (pattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
logger.warn(
"{}: State description contains unit place-holder '%unit%', check if 'unit' metadata is needed!",
itemName);
} else {
Unit<?> stateDescriptionUnit = UnitUtils.parseUnit(pattern);
if (stateDescriptionUnit != null) {
String unit = stateDescriptionUnit.toString();
MetadataKey defaultUnitMetadataKey = new MetadataKey(NumberItem.UNIT_METADATA_NAMESPACE,
if (pattern != null) {
if (pattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
logger.warn(
"{}: State description contains unit place-holder '%unit%', check if 'unit' metadata is needed!",
itemName);
Metadata defaultUnitMetadata = new Metadata(defaultUnitMetadataKey, unit, null);
metadataStorage.put(defaultUnitMetadataKey.toString(), defaultUnitMetadata);
logger.info("{}: Wrote 'unit={}' to metadata.", itemName, unit);
} else {
Unit<?> stateDescriptionUnit = UnitUtils.parseUnit(pattern);
if (stateDescriptionUnit != null) {
String unit = stateDescriptionUnit.toString();
MetadataKey defaultUnitMetadataKey = new MetadataKey(
NumberItem.UNIT_METADATA_NAMESPACE, itemName);
Metadata defaultUnitMetadata = new Metadata(defaultUnitMetadataKey, unit, null);
metadataStorage.put(defaultUnitMetadataKey.toString(), defaultUnitMetadata);
logger.info("{}: Wrote 'unit={}' to metadata.", itemName, unit);
}
}
} else {
logger.debug("{}: Nothing to do, no pattern found.", itemName);
}
}
}