fix(common): coerce null/empty/whitespace observation Result to UNAVAILABLE - #193
fix(common): coerce null/empty/whitespace observation Result to UNAVAILABLE#193ottobolyos wants to merge 9 commits into
Conversation
66db93a to
0a58cfb
Compare
a2c41aa to
a90e23b
Compare
a90e23b to
9d1e172
Compare
|
@PatrickRitchie, this PR fixes a Part 1 spec violation that touches every wire output: |
|
@PatrickRitchie — heads-up: while validating PR #189's dime-connector build I caught an unrelated multi-TFM Release-pack regression introduced by an earlier warnings sweep. The minimal fix lands in PR #194 (just opened as draft); the broader warnings cleanup plus a Release-pack CI gate ships in PR #195. |
UpdatesI updated the PR to allow this feature to be toggled on/off based on the 'InputValidationLevel = strict' configuration parameter. This should allow any existing implementation to still work while allowing a strict adherence to the standard when required. I also added a separate DeviceValidationLevel configuration parameter in order to allow a 'non-standard' DataItem or Component to work with the 'strict' InputValidationLevel. This is common for end users to use a DataItem with a custom Type or SubType. TestsI'm not sure extactly how this affects the tests but I saw where InputValidationLevel is used a few times so that may be why it was failing. |
a61a364 to
6ab2685
Compare
6ab2685 to
b788148
Compare
|
@PatrickRitchie, following up on the empty-Result coerce in Under The Part 1 mandate this repo already recognises verbatim (see
The empty string is by definition not a Valid Data Value under any typed DataItem schema:
The 8 red tests on ProposalRestore the unconditional coerce — drop the If you want the pre-coerce raw-empty behaviour preserved as an opt-in — e.g. for a downstream tool that inspects the raw payload — we can add an Happy to push the unconditional-coerce fix directly. #193 stays draft until we agree on the shape. |
This is often used for messages and program names. SHDR should pass the value as is to the Agent and the Agent should then decide (based on validation level) whether to accept the value or not.
Although, based on the MTConnect Standard, a value should never be 'empty', an 'empty' value should be able to be accepted as many adpaters/agents don't adhere to this restriction and could cause issues with real world implementations. An InputValidationLevel set to 'strict' should rewrite an 'empty' value as 'UNAVAILABLE' so that it strictly conforms to the standard.
…nectDevices validation. This allows a device to be validated at a different level than observations/assets
The new `DeviceValidationLevel` property + enum added on this branch adds one config key row to `IAgentConfiguration` and `AgentConfiguration`; the drift gate `docs/scripts/generate-reference.sh --check` reports DRIFT until the generated `docs/reference/configuration.md` catches up. Runs the generator to produce the current output.
Commit 8dd28c6 narrowed the empty-Result coerce guard on `MTConnectAgent.AddObservation` from unconditional to `InputValidationLevel == Strict`-only. On the three other levels (`Ignore`, `Warning`, `Remove`) a null / empty-string / whitespace-only Result then flowed verbatim onto every wire transport, contradicting the MTConnect Part 1 mandate this repo already encodes verbatim as `Observation.UnavailableDescription`: If an Agent cannot determine a Valid Data Value for a DataItem, the value returned for the Result for the Data Entity MUST be reported as UNAVAILABLE. The empty string is not a Valid Data Value under any typed DataItem schema — `MTConnectStreams_*.xsd` enumerates `UNAVAILABLE` as a schema token, never as an empty content model; `xs:decimal` samples reject the empty lexical form outright; cppagent, the reference implementation, coerces or rejects empty Results at ingress. Restores the pre-8dd28c6b guard: coerce fires whenever the item is non-CONDITION and the Result is empty, regardless of validation level. Strict still gains the original benefit — the empty observation lands rather than being silently dropped by the validator. The 8 red tests on `AddObservation_NullEmptyOrWhitespaceResult_Coerced_To_Unavailable` were pinning exactly this contract and go green with the restored guard.
5fc3041 to
e64c5f8
Compare
Summary
MTConnectAgent.AddObservationaccepted observations whose Result value wasnull, the empty string"", or whitespace-only and forwarded that value verbatim onto every wire transport (HTTP/current,/sample, MQTT JSON-cppagent, SHDR). MTConnect Part 1 §Observation Information Model — Representation — Observation Values mandates"UNAVAILABLE"as the sole valid representation of a missing or undetermined observation value:The empty string is, by definition, not a Valid Data Value (it parses as no value at all under every typed DataItem schema). This PR adds an unconditional coerce step in the canonical
AddObservation(string deviceKey, IObservationInput, …)overload — null / empty / whitespace Result is rewritten toObservation.Unavailablebefore validation, so:Ignore/Warning/Remove: publishesUNAVAILABLErather than the empty value, satisfying the spec mandate on the wire.Strict: coerces toUNAVAILABLE, the observation lands rather than being silently dropped.The coerce skips CONDITION observations (their Level enum cannot carry the empty pathology). DATA_SET / TABLE / TIME_SERIES Representation pre-fill of Count / SampleCount runs after the coerce — semantics for those representations are unchanged.
No opt-out flag is exposed: the spec mandate is
MUST, so a way to opt out of compliance would itself be non-conformant.Behaviour change
null""(empty)"UNAVAILABLE""""""UNAVAILABLE"" "(whitespace)" ""UNAVAILABLE""AVAILABLE"(concrete)"AVAILABLE""AVAILABLE"(unchanged)""underStrict"UNAVAILABLE"Tests
Unit (NUnit,
tests/MTConnect.NET-Common-Tests/Agents/AddObservationEmptyResultCoerceTests.cs, 10 cases):InputValidationLevelnull/""/ whitespace family underWarningStrict(lands, not silently dropped)Wire-level E2E (xUnit,
[Trait("Category", "E2E")]):tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableWorkflowTests.cs— HTTP/current:UNAVAILABLE(6-case[Theory])UNAVAILABLE)Strictvalidation → AVAILABILITY element body isUNAVAILABLE(lands, not silently dropped; spins its own agent + server harness)tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableSampleStreamWorkflowTests.cs— HTTP/samplestream:UNAVAILABLE(6-case[Theory])UNAVAILABLE); proves the coerce does not silently drop the second observationAll wire surfaces fed by the agent's buffer read the same coerced payload — every transport (HTTP, MQTT relay, SHDR adapter output) inherits the fix because the coerce sits above the buffer.
Files touched
libraries/MTConnect.NET-Common/Agents/MTConnectAgent.cs— invoke the coerce inAddObservation; addIsEmptyResult+CoerceEmptyResultToUnavailablehelpers.tests/MTConnect.NET-Common-Tests/Agents/AddObservationEmptyResultCoerceTests.cs— new unit fixture.tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableWorkflowTests.cs— new wire-level E2E fixture against HTTP/current.tests/MTConnect.NET-Integration-Tests/Workflows/AddObservationEmptyResultUnavailableSampleStreamWorkflowTests.cs— new wire-level E2E fixture against HTTP/samplestream..github/workflows/dotnet.yml— drop matrix expressions from the two matrix jobs'name:fields so skipped-state status checks render cleanly (build-and-test/route-check-e2einstead ofbuild-and-test-${{ matrix.os }}).docs/reference/configuration.md— regenerated to surface the newdeviceValidationLevelconfig key on the docs site (IAgentConfiguration+AgentConfigurationsections). Same regenerator (docs/scripts/generate-reference.sh) that drives the docs-site drift gate.Additional scope (from cascade rebase)
Includes maintainer commit
a61a3647 Added separate DeviceValidationLevel propery and enum to handle MTConnectDevices validation:MTConnect.Agents.DeviceValidationLevel(Ignore/Warning/Remove/Strict) — controls how the agent reacts whenMTConnectDevicesshape data fails validation against the standard.AgentConfiguration.DeviceValidationLevel/IAgentConfiguration.DeviceValidationLevelproperty (defaultWarning). Serialised key:deviceValidationLevel.MTConnectAgent.NormalizeDevice— the three invalid-Component / -Composition / -DataItem branches now consultDeviceValidationLevelinstead ofInputValidationLevel, splitting device-shape validation from observation validation. EnablesInputValidationLevel = Strict(spec-conformant observations) withDeviceValidationLevel = Warning(accept non-standard device shapes).Orthogonal to the empty-Result coerce contract above; the coerce runs unconditionally in
AddObservationregardless of either level, per the Part 1MUST.