refactor(sql): #1076 one shared Sql DriverConfig JSON options (Sql.Contracts SqlDriverConfigJson) for driver factory and deploy gate; parity test

This commit is contained in:
Joseph Doherty
2026-12-03 16:28:04 -05:00
parent fe4bbbe256
commit 98dc4c24a1
4 changed files with 58 additions and 25 deletions
@@ -109,21 +109,6 @@ public static class SqlTagDeployChecks
/// <summary>Error code for a named query the Sql driver would refuse at construction (lmxopcua#1058).</summary>
public const string NamedQueryInvalidCode = "SqlNamedQueryInvalid";
/// <summary>
/// The driver factory's config reader options (<c>SqlDriverFactoryExtensions.JsonOptions</c>, 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.
/// </summary>
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() },
};
/// <summary>
/// lmxopcua#1058 (#974 follow-up). The driver checks every <c>queries</c> entry with
/// <see cref="SqlNamedQueryRules.Describe"/> 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<SqlDriverConfigDto>(merged, SqlConfigJson);
dto = JsonSerializer.Deserialize<SqlDriverConfigDto>(merged, SqlDriverConfigJson.Options);
}
catch (JsonException)
{
@@ -0,0 +1,31 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ZB.MOM.WW.OtOpcUa.Driver.Sql.Contracts;
/// <summary>
/// The ONE definition of how a Sql <c>DriverConfig</c> blob is read (lmxopcua#1076). The driver factory deserialises
/// <see cref="SqlDriverConfigDto"/> with <see cref="Options"/>, and the deploy gate (<c>SqlTagDeployChecks</c>, 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.
/// <list type="bullet">
/// <item>Case-insensitive property names, camelCase naming policy (so a write round-trips to the authored shape).</item>
/// <item>Comments and trailing commas tolerated.</item>
/// <item>Unknown members skipped — a blob authored against another schema version must not brick the driver.</item>
/// <item>Enums read as names or ordinals, always written as names (the enum-serialization guard).</item>
/// </list>
/// Treat as read-only: do not mutate after first use.
/// </summary>
public static class SqlDriverConfigJson
{
/// <summary>Serializer options shared by the driver factory, the probe and the deploy gate.</summary>
public static readonly JsonSerializerOptions Options = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Skip,
Converters = { new JsonStringEnumConverter() },
};
}
@@ -47,15 +47,7 @@ public static class SqlDriverFactoryExtensions
/// what makes a <em>write</em> round-trip to the authored shape.</item>
/// </list>
/// </summary>
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;
/// <summary>The serializer options, exposed so tests can assert the enum guard on a real round-trip.</summary>
internal static JsonSerializerOptions JsonOptionsForTest => JsonOptions;
@@ -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;
/// <summary>lmxopcua#1076: the deploy gate and the driver read a Sql DriverConfig through ONE options instance.</summary>
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<SqlDriverConfigDto>(json, SqlDriverFactoryExtensions.JsonOptionsForTest)!;
var b = JsonSerializer.Deserialize<SqlDriverConfigDto>(json, SqlDriverConfigJson.Options)!;
JsonSerializer.Serialize(a, SqlDriverConfigJson.Options).ShouldBe(JsonSerializer.Serialize(b, SqlDriverConfigJson.Options));
a.Provider.ShouldBe(b.Provider);
}
}