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 <lg.hc@free.fr>

* Extended integration test testSquareBracketsInFormat

Signed-off-by: Laurent Garnier <lg.hc@free.fr>

---------

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2024-01-27 18:00:56 +01:00 committed by GitHub
parent 5f8da67983
commit a9571228d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View File

@ -255,6 +255,8 @@ public class GenericItemProvider extends AbstractProvider<Item>
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());

View File

@ -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"));
}
}