fix(mqtt-contracts): strict retainSeed, real bounds instead of dead [Range]s, DateTime edges, closed topic formatting
Driver.Mqtt.Contracts-004/-005/-006/-007. -004: retainSeed was the last behavioural key read leniently, so "retainSeed":"false" — what a CSV import produces — silently became the default TRUE, the INVERSE of the authored intent. Both mappers now read it through TryReadBoolStrict (the qos absent/valid/present-but-invalid split) and DescribeRejection names it. The authoring side needed no hand-mirrored rule: MqttTagConfigModel.Validate already consults this same parse through TagConfigParseGate, so the save is blocked with the inspector's wording — pinned by a new AdminUI parity theory pair. -005: the seven [Range] attributes on MqttDriverOptions/MqttPlainOptions never ran — the options are bound by JsonSerializer, and nothing in the four MQTT projects calls Validator.TryValidateObject. Two of them CONTRADICTED the real authority (KeepAlive's [Range(1,…)] forbade the zero Normalize admits as legal MQTT; MaxPayloadBytes' forbade the non-positive value its own remarks document as falling back to 1 MiB), so wiring them up would have broken deliberate behaviour. Deleted, with each property naming where its bound actually lives. The one escape they papered over is closed: an out-of-range defaultQos rode into the SUBSCRIBE verbatim, and MqttSubscriptionManager's constructor now clamps it exactly as the AdminUI driver form already persists. -006: the DateTime write arm cast a negative long to ulong, so 1969-12-31 published a timestamp ~584 million years in the future; it is now refused as ValueNotRepresentable. An Unspecified kind was handed to ToUniversalTime(), which reads it as LOCAL — the same authored instant encoded differently on the two nodes of a redundant pair; it is now read as UTC. -007: Format/FormatState enforced only null/empty, and nothing at all on deviceId, so the type was not round-trip-closed. They now apply the parser's IsValidSegment (plus '/', which only the format side can see). MqttWriteEncoder's own guards stay — TryEncode returns an error rather than throwing, and that contract does not change.
This commit is contained in:
@@ -195,10 +195,24 @@ public sealed record SparkplugTopic(
|
||||
/// it does not have to hand-concatenate segments itself.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <paramref name="groupId"/>/<paramref name="edgeNodeId"/> is null/empty, or
|
||||
/// <paramref name="groupId"/>/<paramref name="edgeNodeId"/> is null/empty, any supplied id is
|
||||
/// not a legal single topic segment (see <see cref="IsValidSegment"/>), or
|
||||
/// <paramref name="type"/> is <see cref="SparkplugMessageType.STATE"/> (use
|
||||
/// <see cref="FormatState"/> instead — STATE has no group/edge-node/device segments).
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// <b>The per-segment rule is enforced here, not left to the caller</b>, so the type is
|
||||
/// round-trip-closed: everything this emits re-parses through <see cref="TryParse"/> to the
|
||||
/// instance it was built from. Without it, a <c>+</c>/<c>#</c> id produced a topic MQTT-3.3.2-2
|
||||
/// forbids in a PUBLISH — a protocol violation a conformant broker answers by dropping the
|
||||
/// connection — and a <c>deviceId</c> containing <c>/</c> silently added a segment, i.e. a
|
||||
/// well-formed topic addressing a different device than the caller named.
|
||||
/// <para>
|
||||
/// ⚠️ This THROWS where it previously succeeded, so a caller that must not throw keeps its own
|
||||
/// guard: <c>MqttWriteEncoder.TryEncode</c> returns <c>NoWriteDestination</c> for these ids
|
||||
/// rather than propagating, and that contract does not change.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static string Format(string groupId, SparkplugMessageType type, string edgeNodeId, string? deviceId = null)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(groupId);
|
||||
@@ -208,15 +222,26 @@ public sealed record SparkplugTopic(
|
||||
throw new ArgumentException("Use FormatState to build a STATE topic.", nameof(type));
|
||||
}
|
||||
|
||||
ThrowIfNotASegment(groupId, nameof(groupId));
|
||||
ThrowIfNotASegment(edgeNodeId, nameof(edgeNodeId));
|
||||
if (deviceId is not null) ThrowIfNotASegment(deviceId, nameof(deviceId));
|
||||
|
||||
return deviceId is null
|
||||
? $"{Namespace}/{groupId}/{type}/{edgeNodeId}"
|
||||
: $"{Namespace}/{groupId}/{type}/{edgeNodeId}/{deviceId}";
|
||||
}
|
||||
|
||||
/// <summary>Builds the v3.0 STATE topic string <c>spBv1.0/STATE/{hostId}</c>.</summary>
|
||||
/// <param name="hostId">The primary-host id; must be a legal single topic segment.</param>
|
||||
/// <returns>The STATE topic string.</returns>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// <paramref name="hostId"/> is null/empty or is not a legal single topic segment — see the
|
||||
/// round-trip-closure remarks on <see cref="Format"/>.
|
||||
/// </exception>
|
||||
public static string FormatState(string hostId)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(hostId);
|
||||
ThrowIfNotASegment(hostId, nameof(hostId));
|
||||
return $"{Namespace}/STATE/{hostId}";
|
||||
}
|
||||
|
||||
@@ -288,4 +313,27 @@ public sealed record SparkplugTopic(
|
||||
|
||||
private static bool IsValidSegment(string segment) =>
|
||||
segment.Length > 0 && segment.IndexOf('+') < 0 && segment.IndexOf('#') < 0;
|
||||
|
||||
/// <summary>
|
||||
/// Applies <see cref="IsValidSegment"/> on the FORMAT side, so the parser and the formatter judge
|
||||
/// an id by one rule — plus <c>/</c>, which only the format side can see.
|
||||
/// </summary>
|
||||
/// <param name="segment">The candidate segment.</param>
|
||||
/// <param name="paramName">The argument name to report.</param>
|
||||
/// <exception cref="ArgumentException">The segment is empty, carries an MQTT wildcard, or carries <c>/</c>.</exception>
|
||||
/// <remarks>
|
||||
/// <c>/</c> cannot appear in a parsed id — <see cref="TryParse"/> has already split on it — so
|
||||
/// <see cref="IsValidSegment"/> has no reason to test for one and does not. On this side it is
|
||||
/// the difference between an id and two ids: a formatted value carrying one produces a topic with
|
||||
/// an extra segment, which addresses something other than what the caller named and no longer
|
||||
/// re-parses at all. Rejecting it is what makes the round trip total.
|
||||
/// </remarks>
|
||||
private static void ThrowIfNotASegment(string segment, string paramName)
|
||||
{
|
||||
if (IsValidSegment(segment) && segment.IndexOf('/') < 0) return;
|
||||
throw new ArgumentException(
|
||||
$"'{segment}' is not a legal Sparkplug topic segment: it must be non-empty and carry no '/' "
|
||||
+ "and no MQTT wildcard (+ or #).",
|
||||
paramName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user