diff --git a/docs/VirtualTags.md b/docs/VirtualTags.md index be6cc0858..a744753df 100644 --- a/docs/VirtualTags.md +++ b/docs/VirtualTags.md @@ -140,7 +140,7 @@ processes: previous value and status with only a server-log warning). A jagged array (`int[][]`, `byte[][]`) is refused the same way since lmxopcua#1104 (`… cannot carry: an array of arrays …`, with a flatten hint): it used to be the one result divergence — in process it was published as an array of Variants on a node - whose DataType is a scalar type, while the wire has no nested-array tag and the worker answered `unsupported type`. A returned collection (`List`) is copied + whose DataType is a scalar type, while the wire has no nested-array tag and the worker answered `unsupported type`. `byte[][]` is not read as an array of ByteStrings (a script result has no ByteString-array form); its reason says so and also offers a `string[]` of Base64 blobs (lmxopcua#1109). A returned collection (`List`) is copied into a typed array by both runners first (lmxopcua#1092). - **Attribution happens before release.** The worker's exit path runs attribution (strike, quarantine, suspect marks) synchronously **before** it completes any evaluation the worker held, so a script killed for its deadline is diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptResultMaterializer.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptResultMaterializer.cs index b26df033d..19166cfd3 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptResultMaterializer.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting/ScriptResultMaterializer.cs @@ -128,6 +128,18 @@ public static class ScriptResultMaterializer private static void EnsurePublishable(Type type) { if (IsPublishable(type)) return; + // lmxopcua#1109: byte[][] reads like "an array of ByteStrings", which OPC UA does have (the OpcUaClient driver even + // surfaces a ByteString[] as byte[][]). But a script result has no ByteString-array form: the result rule's only + // byte shape is a one-dimensional Byte array, and the out-of-process wire (VariantCodec) has no ByteString or + // nested-array tag — adding one is deliberately out of scope. So byte[][] stays refused with every other jagged + // array; the reason just says so explicitly and offers the string[] form, which does publish. + if (type == typeof(byte[][])) + throw new ScriptResultException( + "The script returned a value of type 'Byte[][]', which an OPC UA node cannot carry: an array of arrays has " + + "no representation on a node whose DataType is a scalar type, and a script result cannot be an array of " + + "ByteStrings either (its only byte shape is one flat Byte array); return one flat byte[] " + + "(e.g. a.SelectMany(x => x).ToArray()) or a string[] of encoded blobs " + + "(e.g. a.Select(Convert.ToBase64String).ToArray())."); if (type.IsArray && type.GetElementType()!.IsArray) throw new ScriptResultException( $"The script returned a value of type '{Describe(type)}', which an OPC UA node cannot carry: an array of " + diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptResultMaterializerTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptResultMaterializerTests.cs index dbbd4d0cb..29ee21a15 100644 --- a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptResultMaterializerTests.cs +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptResultMaterializerTests.cs @@ -120,6 +120,32 @@ public sealed class ScriptResultMaterializerTests .Message.ShouldStartWith("The script returned a value of type 'Int32[][]'"); } + /// + /// lmxopcua#1109: byte[][] stays refused (a script result has no ByteString-array form and the worker wire has + /// no tag for one — none was added), but its reason says so and offers the two forms that do publish. A lazy / + /// collection sequence of byte[] is refused earlier (its element type is itself a sequence), before it runs. + /// + [Fact] + public void A_byte_array_of_arrays_is_refused_with_a_ByteString_specific_reason() + { + ScriptResultMaterializer.IsPublishable(typeof(byte[][])).ShouldBeFalse(); + var message = Should.Throw( + () => ScriptResultMaterializer.Materialize(new[] { new byte[] { 1, 2 }, new byte[] { 3 } }, 100)).Message; + message.ShouldStartWith("The script returned a value of type 'Byte[][]', which an OPC UA node cannot carry: an array of arrays"); + message.ShouldContain("cannot be an array of ByteStrings"); + message.ShouldContain("SelectMany"); + message.ShouldContain("Convert.ToBase64String"); + + var ran = 0; + Should.Throw(() => ScriptResultMaterializer.Materialize(new List { new byte[] { 1 } }, 100)); + Should.Throw(() => ScriptResultMaterializer.Materialize( + Enumerable.Range(0, 2).Select(i => { ran++; return new[] { (byte)i }; }), 100)); + ran.ShouldBe(0); + // The flat forms the reason offers do publish. + ScriptResultMaterializer.Materialize(new byte[] { 1, 2, 3 }, 100).ShouldBeOfType(); + ScriptResultMaterializer.Materialize(new[] { "AQI=" }, 100).ShouldBeOfType(); + } + [Fact] public void An_unpublishable_lazy_sequence_is_refused_before_it_runs() {