From 98dc4c24a14852944e2fda00f26ab178c79d1de4 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 16:28:04 -0500 Subject: [PATCH] refactor(sql): #1076 one shared Sql DriverConfig JSON options (Sql.Contracts SqlDriverConfigJson) for driver factory and deploy gate; parity test --- .../Validation/SqlTagDeployChecks.cs | 17 +--------- .../SqlDriverConfigJson.cs | 31 +++++++++++++++++++ .../SqlDriverFactoryExtensions.cs | 10 +----- .../SqlDriverConfigJsonParityTests.cs | 25 +++++++++++++++ 4 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts/SqlDriverConfigJson.cs create mode 100644 tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Tests/SqlDriverConfigJsonParityTests.cs diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Configuration/Validation/SqlTagDeployChecks.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Configuration/Validation/SqlTagDeployChecks.cs index 122c03927..0be75d713 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Configuration/Validation/SqlTagDeployChecks.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Configuration/Validation/SqlTagDeployChecks.cs @@ -109,21 +109,6 @@ public static class SqlTagDeployChecks /// Error code for a named query the Sql driver would refuse at construction (lmxopcua#1058). public const string NamedQueryInvalidCode = "SqlNamedQueryInvalid"; - /// - /// The driver factory's config reader options (SqlDriverFactoryExtensions.JsonOptions, which lives in the - /// driver assembly this one does not reference): case-insensitive, comments + trailing commas tolerated, unknown - /// members skipped, enums by name. Keep in step — a looser reader here could pass a blob the driver refuses. - /// - private static readonly JsonSerializerOptions SqlConfigJson = new() - { - PropertyNameCaseInsensitive = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - ReadCommentHandling = JsonCommentHandling.Skip, - AllowTrailingCommas = true, - UnmappedMemberHandling = System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip, - Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() }, - }; - /// /// lmxopcua#1058 (#974 follow-up). The driver checks every queries entry with /// under its provider when it is constructed, and ONE refused query @@ -145,7 +130,7 @@ public static class SqlTagDeployChecks { var merged = DriverDeviceConfigMerger.Merge( driver.DriverConfig, devices.Select(d => new DriverDeviceConfigMerger.DeviceRow(d.Name, d.DeviceConfig)).ToList(), []); - dto = JsonSerializer.Deserialize(merged, SqlConfigJson); + dto = JsonSerializer.Deserialize(merged, SqlDriverConfigJson.Options); } catch (JsonException) { diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts/SqlDriverConfigJson.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts/SqlDriverConfigJson.cs new file mode 100644 index 000000000..b1d52a801 --- /dev/null +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts/SqlDriverConfigJson.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts; + +/// +/// The ONE definition of how a Sql DriverConfig blob is read (lmxopcua#1076). The driver factory deserialises +/// with , and the deploy gate (SqlTagDeployChecks, which +/// cannot reference the driver assembly) uses the same instance, so the gate can never be looser or stricter than +/// the driver about what the JSON means. +/// +/// Case-insensitive property names, camelCase naming policy (so a write round-trips to the authored shape). +/// Comments and trailing commas tolerated. +/// Unknown members skipped — a blob authored against another schema version must not brick the driver. +/// Enums read as names or ordinals, always written as names (the enum-serialization guard). +/// +/// Treat as read-only: do not mutate after first use. +/// +public static class SqlDriverConfigJson +{ + /// Serializer options shared by the driver factory, the probe and the deploy gate. + public static readonly JsonSerializerOptions Options = new() + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + ReadCommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true, + UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, + Converters = { new JsonStringEnumConverter() }, + }; +} diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql/SqlDriverFactoryExtensions.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql/SqlDriverFactoryExtensions.cs index 91bbda729..1a52950b4 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql/SqlDriverFactoryExtensions.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql/SqlDriverFactoryExtensions.cs @@ -47,15 +47,7 @@ public static class SqlDriverFactoryExtensions /// what makes a write round-trip to the authored shape. /// /// - internal static readonly JsonSerializerOptions JsonOptions = new() - { - PropertyNameCaseInsensitive = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - ReadCommentHandling = JsonCommentHandling.Skip, - AllowTrailingCommas = true, - UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip, - Converters = { new JsonStringEnumConverter() }, - }; + internal static readonly JsonSerializerOptions JsonOptions = SqlDriverConfigJson.Options; /// The serializer options, exposed so tests can assert the enum guard on a real round-trip. internal static JsonSerializerOptions JsonOptionsForTest => JsonOptions; diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Tests/SqlDriverConfigJsonParityTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Tests/SqlDriverConfigJsonParityTests.cs new file mode 100644 index 000000000..6c4e4113d --- /dev/null +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Sql.Tests/SqlDriverConfigJsonParityTests.cs @@ -0,0 +1,25 @@ +using System.Text.Json; +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts; + +namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Tests; + +/// lmxopcua#1076: the deploy gate and the driver read a Sql DriverConfig through ONE options instance. +public sealed class SqlDriverConfigJsonParityTests +{ + [Fact] + public void Driver_factory_and_deploy_gate_share_one_options_instance() + => ReferenceEquals(SqlDriverFactoryExtensions.JsonOptionsForTest, SqlDriverConfigJson.Options).ShouldBeTrue(); + + [Theory] + [InlineData("{\"provider\":\"Odbc\",\"queries\":{\"q\":{\"sql\":\"select 1\"}}}")] + [InlineData("{\"PROVIDER\":2, // c\n \"unknown\":1, \"queries\":{\"q\":{\"sql\":\"select 1\"},},}")] + public void Both_readers_parse_identically(string json) + { + var a = JsonSerializer.Deserialize(json, SqlDriverFactoryExtensions.JsonOptionsForTest)!; + var b = JsonSerializer.Deserialize(json, SqlDriverConfigJson.Options)!; + JsonSerializer.Serialize(a, SqlDriverConfigJson.Options).ShouldBe(JsonSerializer.Serialize(b, SqlDriverConfigJson.Options)); + a.Provider.ShouldBe(b.Provider); + } +}