diff --git a/bundles/org.openhab.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/PersistentInbox.java b/bundles/org.openhab.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/PersistentInbox.java index 06666e1f20..3cdc625288 100644 --- a/bundles/org.openhab.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/PersistentInbox.java +++ b/bundles/org.openhab.core.config.discovery/src/main/java/org/openhab/core/config/discovery/internal/PersistentInbox.java @@ -36,6 +36,7 @@ import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.common.AbstractUID; import org.openhab.core.common.ThreadPoolManager; import org.openhab.core.config.core.ConfigDescription; import org.openhab.core.config.core.ConfigDescriptionParameter; @@ -183,6 +184,9 @@ public final class PersistentInbox implements Inbox, DiscoveryListener, ThingReg if (results.isEmpty()) { throw new IllegalArgumentException("No Thing with UID " + thingUID.getAsString() + " in inbox"); } + if (newThingId != null && newThingId.contains(AbstractUID.SEPARATOR)) { + throw new IllegalArgumentException("New Thing ID " + newThingId + " must not contain multiple segments"); + } DiscoveryResult result = results.get(0); final Map properties = new HashMap<>(); final Map configParams = new HashMap<>(); @@ -191,11 +195,12 @@ public final class PersistentInbox implements Inbox, DiscoveryListener, ThingReg ThingTypeUID thingTypeUID = result.getThingTypeUID(); ThingUID newThingUID = thingUID; if (newThingId != null) { + String newUID = thingUID.getAsString().substring(0, + thingUID.getAsString().lastIndexOf(AbstractUID.SEPARATOR) + 1) + newThingId; try { - newThingUID = new ThingUID(thingTypeUID, newThingId); + newThingUID = new ThingUID(newUID); } catch (IllegalArgumentException e) { - logger.warn("Cannot create thing: {}", e.getMessage()); - return null; + throw new IllegalArgumentException("Invalid thing UID " + newUID, e); } } Thing newThing = ThingFactory.createThing(newThingUID, config, properties, result.getBridgeUID(), thingTypeUID, diff --git a/bundles/org.openhab.core.config.discovery/src/test/java/org/openhab/core/config/discovery/internal/PersistentInboxTest.java b/bundles/org.openhab.core.config.discovery/src/test/java/org/openhab/core/config/discovery/internal/PersistentInboxTest.java index 2b364a887b..8d1529064c 100644 --- a/bundles/org.openhab.core.config.discovery/src/test/java/org/openhab/core/config/discovery/internal/PersistentInboxTest.java +++ b/bundles/org.openhab.core.config.discovery/src/test/java/org/openhab/core/config/discovery/internal/PersistentInboxTest.java @@ -172,7 +172,15 @@ public class PersistentInboxTest { when(storage.getValues()).thenReturn(List.of(result)); inbox.activate(); - assertNull(inbox.approve(THING_UID, "Test", "invalid:id")); + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + inbox.approve(THING_UID, "Test", "invalid:id"); + }); + assertEquals("New Thing ID invalid:id must not contain multiple segments", exception.getMessage()); + + exception = assertThrows(IllegalArgumentException.class, () -> { + inbox.approve(THING_UID, "Test", "invalid$id"); + }); + assertEquals("Invalid thing UID test:test:invalid$id", exception.getMessage()); } @Test