mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-20 16:52:07 +08:00
9b034f8291
# Description Nullable reference types are now enabled once, in `common.props`, and every project in the repository builds warning-clean with them. **Configuration** - `<Nullable>enable</Nullable>` lives in `common.props`. All per-project `<Nullable>` settings are removed: 178 project files (enable, annotations, and the `disable` in the three Aot test projects). - 419 per-file `#nullable enable`/`restore` directives and the 4 `#nullable disable` directives are removed. - Kept on purpose: - The 5 `<auto-generated>` schema files (`UANodeSet.cs`, `UA Type Dictionary.cs`, `OPCBinarySchema.cs`, `SecuredApplication.cs`, `PInvokeHelper.cs`) keep `#nullable enable`. The compiler ignores the project setting in generated code, so the directive is required there. - `tests/Opc.Ua.Interop.LegacyPeer` keeps `disable`. It builds against the 1.5.378 packages and does not import the repository props. - `docs/DeveloperGuide.md` and `.github/copilot-instructions.md` now describe the central setting. **Migration** (the 43 unset and 13 annotations-only projects, roughly 10k diagnostics) - **Production code: annotated at the declarations.** Covers `tools/Opc.Ua.SourceGeneration.Core`, `src/Opc.Ua.Lds.Server` and the LDS sample. - Members, parameters and returns are nullable where null actually flows. - Null-check helpers carry `[NotNullWhen]` (`IsNull`, `IsValidXmlName`, `IsMethodTypeDesign`). - `CoreUtils.Format`, `Utils.Format`, `ITemplateWriter.WriteLine` take `params object?[]`. `Template.AddReplacement` takes `string?` (rendering already skips null replacements). - `IRegisteredServerStore.RegisterAsync` returns `Task<RegistrationEntry?>`: it returns null when a registration is removed. - Dereferences backed by an invariant (for example, the validated model) use `!`, so runtime behaviour is unchanged. - **Behavioural changes in production code** (small, deliberate): - `NodeSetToModelDesign` throws `InvalidOperationException` when the file is not a NodeSet. Before, it threw a NullReferenceException on the next line. - `ModelDesignValidator` starts its lookup tables empty instead of null. A redundant `if (m_nodes == null) return;` guard is gone. - **Test code: minimal annotations.** - `?` on values that can be null, `!` where the test asserts non-null. - `= null!` for fixture members assigned in SetUp. - `TryGetStructure<T>(out T? x)` where the inferred `T?` broke the `IEncodeable` constraint. - A few redundant `== null` checks on structs (`NodeId`, `ArrayOf<T>`) folded into the adjacent `IsNull`/`Count` checks. **Notes for reviewers** - The diff is large (~1,500 files) but almost entirely mechanical: `?`, `!` and directive removal. The non-annotation changes are listed above. - Known follow-up, not done here: `IModelDesign.Namespaces/TargetNamespace/NamespaceUris` stay non-nullable even though the validator returns null before it loads a dictionary. Making them nullable would ripple through every generator call site. ## Related Issues - Fixes #4439 ## Checklist - [ ] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [ ] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [ ] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. - [ ] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [ ] I have addressed **all** PR feedback received. The "no new warnings" box covers net10.0 and net48: full non-incremental builds of `UA.slnx`, `tools/SourceGeneration.slnx`, `tools/MigrationAnalyzer.slnx` and the Aot test projects finished with 0 warnings and 0 errors under the repository's warnings-as-errors settings. net9.0 and net8.0 also had no nullable diagnostics, but they were not rebuilt after the final fixes. Tests have not been run locally yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
439 lines
12 KiB
C#
439 lines
12 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
|
|
*
|
|
* OPC Foundation MIT License 1.00
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
* The complete license agreement can be found here:
|
|
* http://opcfoundation.org/License/MIT/1.00/
|
|
* ======================================================================*/
|
|
|
|
using System;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Server.Fluent;
|
|
|
|
namespace Opc.Ua.Server.Tests.Fluent
|
|
{
|
|
/// <summary>
|
|
/// Exhaustive coverage for <see cref="FluentVariant.ToVariant{TValue}(TValue)"/>
|
|
/// — the shared marshalling bridge behind the fluent typed variable
|
|
/// surface. Verifies every built-in OPC UA Variant type (scalar,
|
|
/// <see cref="ArrayOf{T}"/> and <see cref="MatrixOf{T}"/>), enumerations,
|
|
/// null handling, and the unsupported-type failure path all route through
|
|
/// the same <see cref="IVariantBuilder{T}"/> registration the rest of the
|
|
/// stack uses.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("Fluent")]
|
|
[Parallelizable]
|
|
public class FluentVariantTests
|
|
{
|
|
private static readonly Guid s_guid =
|
|
new("12345678-1234-1234-1234-123456789012");
|
|
|
|
/// <summary>
|
|
/// Asserts that <see cref="FluentVariant.ToVariant{TValue}(TValue)"/>
|
|
/// produces exactly what the canonical <see cref="IVariantBuilder{T}"/>
|
|
/// registration on <see cref="VariantBuilder"/> produces for the same
|
|
/// value.
|
|
/// </summary>
|
|
private static void AssertMatchesBuilder<T>(T value)
|
|
{
|
|
var builder = (IVariantBuilder<T>)(object)default(VariantBuilder);
|
|
Variant expected = builder.WithValue(value);
|
|
Variant actual = FluentVariant.ToVariant(value);
|
|
Assert.That(
|
|
actual.TypeInfo.BuiltInType,
|
|
Is.EqualTo(expected.TypeInfo.BuiltInType));
|
|
Assert.That(
|
|
actual.TypeInfo.ValueRank,
|
|
Is.EqualTo(expected.TypeInfo.ValueRank));
|
|
Assert.That(actual, Is.EqualTo(expected));
|
|
}
|
|
|
|
private static void AssertArrayMatchesBuilder<T>(params T[] items)
|
|
{
|
|
AssertMatchesBuilder<ArrayOf<T>>(items);
|
|
}
|
|
|
|
private static void AssertMatrixMatchesBuilder<T>(T[,] items)
|
|
{
|
|
AssertMatchesBuilder(MatrixOf.From<T>(items));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarBoolean()
|
|
{
|
|
AssertMatchesBuilder(true);
|
|
AssertMatchesBuilder(false);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarSByte()
|
|
{
|
|
AssertMatchesBuilder(sbyte.MinValue);
|
|
AssertMatchesBuilder(sbyte.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarByte()
|
|
{
|
|
AssertMatchesBuilder(byte.MinValue);
|
|
AssertMatchesBuilder(byte.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarInt16()
|
|
{
|
|
AssertMatchesBuilder(short.MinValue);
|
|
AssertMatchesBuilder(short.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarUInt16()
|
|
{
|
|
AssertMatchesBuilder(ushort.MinValue);
|
|
AssertMatchesBuilder(ushort.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarInt32()
|
|
{
|
|
AssertMatchesBuilder(int.MinValue);
|
|
AssertMatchesBuilder(int.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarUInt32()
|
|
{
|
|
AssertMatchesBuilder(uint.MinValue);
|
|
AssertMatchesBuilder(uint.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarInt64()
|
|
{
|
|
AssertMatchesBuilder(long.MinValue);
|
|
AssertMatchesBuilder(long.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarUInt64()
|
|
{
|
|
AssertMatchesBuilder(ulong.MinValue);
|
|
AssertMatchesBuilder(ulong.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarFloat()
|
|
{
|
|
AssertMatchesBuilder(3.14f);
|
|
AssertMatchesBuilder(float.MinValue);
|
|
AssertMatchesBuilder(float.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarDouble()
|
|
{
|
|
AssertMatchesBuilder(3.14159);
|
|
AssertMatchesBuilder(double.MinValue);
|
|
AssertMatchesBuilder(double.MaxValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarString()
|
|
{
|
|
AssertMatchesBuilder("hello");
|
|
AssertMatchesBuilder(string.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarDateTimeUtc()
|
|
{
|
|
AssertMatchesBuilder(new DateTimeUtc(2024, 6, 15, 12, 30, 0));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarUuid()
|
|
{
|
|
AssertMatchesBuilder(new Uuid(s_guid));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarByteString()
|
|
{
|
|
AssertMatchesBuilder(ByteString.From(new byte[] { 1, 2, 3 }));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarXmlElement()
|
|
{
|
|
AssertMatchesBuilder(XmlElement.From("<test>value</test>"));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarNodeId()
|
|
{
|
|
AssertMatchesBuilder(new NodeId(42));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarExpandedNodeId()
|
|
{
|
|
AssertMatchesBuilder(new ExpandedNodeId(42));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarStatusCode()
|
|
{
|
|
AssertMatchesBuilder(StatusCodes.BadNoData);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarQualifiedName()
|
|
{
|
|
AssertMatchesBuilder(new QualifiedName("test", 2));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarLocalizedText()
|
|
{
|
|
AssertMatchesBuilder(new LocalizedText("en", "text"));
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarExtensionObject()
|
|
{
|
|
AssertMatchesBuilder(ExtensionObject.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void ScalarDataValue()
|
|
{
|
|
AssertMatchesBuilder(new DataValue(Variant.From(42)));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayBoolean()
|
|
{
|
|
AssertArrayMatchesBuilder(true, false, true);
|
|
}
|
|
|
|
[Test]
|
|
public void ArraySByte()
|
|
{
|
|
AssertArrayMatchesBuilder<sbyte>(-1, 0, 1);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayByte()
|
|
{
|
|
AssertArrayMatchesBuilder<byte>(0, 1, 255);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayInt16()
|
|
{
|
|
AssertArrayMatchesBuilder<short>(-1, 0, 1);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayUInt16()
|
|
{
|
|
AssertArrayMatchesBuilder<ushort>(0, 1, 2);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayInt32()
|
|
{
|
|
AssertArrayMatchesBuilder(1, 2, 3);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayUInt32()
|
|
{
|
|
AssertArrayMatchesBuilder(1u, 2u, 3u);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayInt64()
|
|
{
|
|
AssertArrayMatchesBuilder(1L, 2L, 3L);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayUInt64()
|
|
{
|
|
AssertArrayMatchesBuilder(1uL, 2uL, 3uL);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayFloat()
|
|
{
|
|
AssertArrayMatchesBuilder(1.5f, 2.5f);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayDouble()
|
|
{
|
|
AssertArrayMatchesBuilder(1.5, 2.5);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayString()
|
|
{
|
|
AssertArrayMatchesBuilder("a", "b");
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayDateTimeUtc()
|
|
{
|
|
AssertArrayMatchesBuilder(
|
|
new DateTimeUtc(2024, 1, 1, 0, 0, 0),
|
|
new DateTimeUtc(2024, 6, 15, 12, 30, 0));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayUuid()
|
|
{
|
|
AssertArrayMatchesBuilder(new Uuid(s_guid));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayByteString()
|
|
{
|
|
AssertArrayMatchesBuilder(ByteString.From(new byte[] { 1, 2 }));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayXmlElement()
|
|
{
|
|
AssertArrayMatchesBuilder(XmlElement.From("<a/>"));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayNodeId()
|
|
{
|
|
AssertArrayMatchesBuilder(new NodeId(1), new NodeId(2));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayExpandedNodeId()
|
|
{
|
|
AssertArrayMatchesBuilder(new ExpandedNodeId(1));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayStatusCode()
|
|
{
|
|
AssertArrayMatchesBuilder(StatusCodes.Good);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayQualifiedName()
|
|
{
|
|
AssertArrayMatchesBuilder(new QualifiedName("a", 2));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayLocalizedText()
|
|
{
|
|
AssertArrayMatchesBuilder(new LocalizedText("en", "a"));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayExtensionObject()
|
|
{
|
|
AssertArrayMatchesBuilder(ExtensionObject.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayDataValue()
|
|
{
|
|
AssertArrayMatchesBuilder(new DataValue(Variant.From(1)));
|
|
}
|
|
|
|
[Test]
|
|
public void ArrayVariant()
|
|
{
|
|
AssertArrayMatchesBuilder(Variant.From(1), Variant.From("a"));
|
|
}
|
|
|
|
[Test]
|
|
public void MatrixInt32()
|
|
{
|
|
AssertMatrixMatchesBuilder(new[,] { { 1, 2 }, { 3, 4 } });
|
|
}
|
|
|
|
[Test]
|
|
public void MatrixDouble()
|
|
{
|
|
AssertMatrixMatchesBuilder(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 } });
|
|
}
|
|
|
|
[Test]
|
|
public void MatrixString()
|
|
{
|
|
AssertMatrixMatchesBuilder(new[,] { { "a", "b" }, { "c", "d" } });
|
|
}
|
|
|
|
[Test]
|
|
public void MatrixBoolean()
|
|
{
|
|
AssertMatrixMatchesBuilder(new[,] { { true, false }, { false, true } });
|
|
}
|
|
|
|
[Test]
|
|
public void EnumMapsToInt32WireValue()
|
|
{
|
|
Variant v = FluentVariant.ToVariant(TestEnum.Second);
|
|
Assert.That(v, Is.EqualTo(Variant.From(TestEnum.Second)));
|
|
}
|
|
|
|
[Test]
|
|
public void NullReferenceValueBecomesVariantNull()
|
|
{
|
|
Variant v = FluentVariant.ToVariant<string>(null!);
|
|
Assert.That(v, Is.EqualTo(Variant.Null));
|
|
}
|
|
|
|
[Test]
|
|
public void UnsupportedTypeThrowsBadNotSupported()
|
|
{
|
|
ServiceResultException ex = Assert.Throws<ServiceResultException>(
|
|
() => FluentVariant.ToVariant(new UnsupportedType()))!;
|
|
Assert.That(
|
|
ex.StatusCode, Is.EqualTo((uint)StatusCodes.BadNotSupported));
|
|
}
|
|
|
|
private enum TestEnum
|
|
{
|
|
First = 0,
|
|
Second = 5
|
|
}
|
|
|
|
private sealed class UnsupportedType
|
|
{
|
|
}
|
|
}
|
|
}
|