From a9571228d2eba492285169745287efb244e6f45f Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sat, 27 Jan 2024 18:00:56 +0100 Subject: [PATCH] Remove state description when loading an item with a removed pattern (#4068) * Remove state description when loading an item with a removed pattern Fix openhab/openhab-webui#2251 Signed-off-by: Laurent Garnier * Extended integration test testSquareBracketsInFormat Signed-off-by: Laurent Garnier --------- Signed-off-by: Laurent Garnier --- .../item/internal/GenericItemProvider.java | 2 ++ .../internal/GenericItemProviderTest.java | 25 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java index f7281af85..55383cf35 100644 --- a/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java +++ b/bundles/org.openhab.core.model.item/src/org/openhab/core/model/item/internal/GenericItemProvider.java @@ -255,6 +255,8 @@ public class GenericItemProvider extends AbstractProvider label = label.substring(0, label.indexOf("[")).trim(); stateDescriptionFragments.put(modelItem.getName(), StateDescriptionFragmentBuilder.create().withPattern(format).build()); + } else { + stateDescriptionFragments.remove(modelItem.getName()); } activeItem.setLabel(label); activeItem.setCategory(modelItem.getIcon()); diff --git a/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java b/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java index ee1453045..5c19baeef 100644 --- a/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java +++ b/itests/org.openhab.core.model.item.tests/src/main/java/org/openhab/core/model/item/internal/GenericItemProviderTest.java @@ -12,7 +12,7 @@ */ package org.openhab.core.model.item.internal; -import static java.util.stream.Collectors.*; +import static java.util.stream.Collectors.joining; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; @@ -655,5 +655,28 @@ public class GenericItemProviderTest extends JavaOSGiTest { StateDescription stateDescription = item.getStateDescription(); assertThat(stateDescription, is(notNullValue())); assertThat(stateDescription.getPattern(), is("XPATH(/*[name()='liveStreams']/*[name()='stream']):%s")); + + model = "Switch s \"Info [%s]\""; + modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); + item = itemRegistry.get("s"); + assertThat(item, is(notNullValue())); + stateDescription = item.getStateDescription(); + assertThat(stateDescription, is(notNullValue())); + assertThat(stateDescription.getPattern(), is("%s")); + + model = "Switch s \"Info\""; + modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); + item = itemRegistry.get("s"); + assertThat(item, is(notNullValue())); + stateDescription = item.getStateDescription(); + assertThat(stateDescription, is(nullValue())); + + model = "Switch s \"Info [%s]\""; + modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes())); + item = itemRegistry.get("s"); + assertThat(item, is(notNullValue())); + stateDescription = item.getStateDescription(); + assertThat(stateDescription, is(notNullValue())); + assertThat(stateDescription.getPattern(), is("%s")); } }