mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-20 00:31:32 +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>
361 lines
15 KiB
C#
361 lines
15 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2026 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 System.Threading;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Tests;
|
|
|
|
namespace Opc.Ua.Core.Tests.Stack.Nodes
|
|
{
|
|
[TestFixture]
|
|
[Category("Nodes")]
|
|
[Parallelizable]
|
|
public sealed class NodeTableSupportTests
|
|
{
|
|
[Test]
|
|
public void ViewTableDefaultViewAcceptsAnyNodeAndReference()
|
|
{
|
|
var table = new ViewTable();
|
|
var defaultView = new ViewDescription();
|
|
|
|
Assert.That(table.IsValid(defaultView), Is.True);
|
|
Assert.That(table.IsNodeInView(defaultView, new NodeId(123)), Is.True);
|
|
Assert.That(table.IsReferenceInView(defaultView, new ReferenceDescription()), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void ViewTableAddValidatesNodeIdAndDuplicates()
|
|
{
|
|
var table = new ViewTable();
|
|
var invalid = new ViewNode { NodeId = NodeId.Null };
|
|
var view = new ViewNode { NodeId = new NodeId(5000) };
|
|
|
|
Assert.That(() => table.Add(null!), Throws.ArgumentNullException);
|
|
ServiceResultException invalidEx = Assert.Throws<ServiceResultException>(() => table.Add(invalid));
|
|
Assert.That(invalidEx.StatusCode, Is.EqualTo(StatusCodes.BadNodeIdInvalid));
|
|
|
|
table.Add(view);
|
|
|
|
Assert.That(table.IsValid(new ViewDescription { ViewId = view.NodeId }), Is.True);
|
|
ServiceResultException duplicateEx = Assert.Throws<ServiceResultException>(() => table.Add(view));
|
|
Assert.That(duplicateEx.StatusCode, Is.EqualTo(StatusCodes.BadNodeIdExists));
|
|
}
|
|
|
|
[Test]
|
|
public void ViewTableRemoveValidatesNullUnknownAndExistingView()
|
|
{
|
|
var table = new ViewTable();
|
|
NodeId viewId = new(6000);
|
|
table.Add(new ViewNode { NodeId = viewId });
|
|
|
|
Assert.That(() => table.Remove(NodeId.Null), Throws.ArgumentNullException);
|
|
ServiceResultException unknownEx = Assert.Throws<ServiceResultException>(
|
|
() => table.Remove(new NodeId(7000)));
|
|
Assert.That(unknownEx.StatusCode, Is.EqualTo(StatusCodes.BadViewIdUnknown));
|
|
|
|
table.Remove(viewId);
|
|
|
|
Assert.That(table.IsValid(new ViewDescription { ViewId = viewId }), Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void ViewTableRegisteredViewExcludesNodesAndReferences()
|
|
{
|
|
var table = new ViewTable();
|
|
NodeId viewId = new(8000);
|
|
var description = new ViewDescription { ViewId = viewId };
|
|
table.Add(new ViewNode { NodeId = viewId });
|
|
|
|
Assert.That(table.IsNodeInView(description, new NodeId(1)), Is.False);
|
|
Assert.That(table.IsReferenceInView(description, new ReferenceDescription()), Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void ViewTableMissingViewOperationsThrowBadViewIdUnknown()
|
|
{
|
|
var table = new ViewTable();
|
|
var description = new ViewDescription { ViewId = new NodeId(9000) };
|
|
|
|
ServiceResultException nodeEx = Assert.Throws<ServiceResultException>(
|
|
() => table.IsNodeInView(description, new NodeId(1)));
|
|
ServiceResultException referenceEx = Assert.Throws<ServiceResultException>(
|
|
() => table.IsReferenceInView(description, new ReferenceDescription()));
|
|
|
|
Assert.That(nodeEx.StatusCode, Is.EqualTo(StatusCodes.BadViewIdUnknown));
|
|
Assert.That(referenceEx.StatusCode, Is.EqualTo(StatusCodes.BadViewIdUnknown));
|
|
}
|
|
|
|
[Test]
|
|
public void FilterContextWithoutOperationContextReturnsDefaults()
|
|
{
|
|
NamespaceTable namespaces = new();
|
|
ITypeTable typeTree = new TypeTable(namespaces);
|
|
var preferredLocales = new ArrayOf<string>(s_preferredLocales);
|
|
|
|
var context = new FilterContext(
|
|
namespaces,
|
|
typeTree,
|
|
preferredLocales,
|
|
NUnitTelemetryContext.Create());
|
|
|
|
Assert.That(context.NamespaceUris, Is.SameAs(namespaces));
|
|
Assert.That(context.TypeTree, Is.SameAs(typeTree));
|
|
Assert.That(context.PreferredLocales, Is.EqualTo(preferredLocales));
|
|
Assert.That(context.DiagnosticsMask, Is.EqualTo(DiagnosticsMasks.SymbolicId));
|
|
Assert.That(context.OperationDeadline, Is.EqualTo(DateTime.MaxValue));
|
|
Assert.That(context.OperationStatus, Is.EqualTo(StatusCodes.Good));
|
|
Assert.That(context.SessionId, Is.Null);
|
|
Assert.That(context.UserIdentity, Is.Null);
|
|
Assert.That(context.StringTable, Is.Null);
|
|
Assert.That(context.AuditEntryId, Is.Null);
|
|
Assert.That(context.Telemetry, Is.Not.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void FilterContextValidatesRequiredTables()
|
|
{
|
|
NamespaceTable namespaces = new();
|
|
ITypeTable typeTree = new TypeTable(namespaces);
|
|
|
|
Assert.That(
|
|
() => _ = new FilterContext(null!, typeTree, NUnitTelemetryContext.Create()),
|
|
Throws.ArgumentNullException);
|
|
Assert.That(
|
|
() => _ = new FilterContext(namespaces, null!, NUnitTelemetryContext.Create()),
|
|
Throws.ArgumentNullException);
|
|
}
|
|
|
|
[Test]
|
|
public void AsyncTypeTableAdapterForwardsSynchronousCalls()
|
|
{
|
|
var asyncTable = new RecordingAsyncTypeTable();
|
|
ITypeTable table = asyncTable.AsTypeTable();
|
|
ExpandedNodeId expandedTypeId = ObjectTypeIds.BaseObjectType;
|
|
NodeId typeId = VariableTypeIds.BaseVariableType;
|
|
QualifiedName browseName = new(BrowseNames.HasSubtype);
|
|
|
|
Assert.That(table.IsKnown(expandedTypeId), Is.True);
|
|
Assert.That(table.IsKnown(typeId), Is.False);
|
|
Assert.That(table.FindSuperType(expandedTypeId), Is.EqualTo(ObjectTypeIds.BaseObjectType));
|
|
Assert.That(table.FindSuperType(typeId), Is.EqualTo(VariableTypeIds.BaseVariableType));
|
|
Assert.That(table.FindSuperTypeAsync(expandedTypeId).GetAwaiter().GetResult(), Is.EqualTo(ObjectTypeIds.BaseObjectType));
|
|
Assert.That(table.FindSuperTypeAsync(typeId).GetAwaiter().GetResult(), Is.EqualTo(VariableTypeIds.BaseVariableType));
|
|
Assert.That(table.FindSubTypes(expandedTypeId), Is.EqualTo(new ArrayOf<NodeId>(new[] { typeId })));
|
|
Assert.That(table.IsTypeOf(expandedTypeId, ObjectTypeIds.BaseObjectType), Is.True);
|
|
Assert.That(table.IsTypeOf(typeId, ObjectTypeIds.BaseObjectType), Is.False);
|
|
Assert.That(table.FindReferenceTypeName(ReferenceTypeIds.HasSubtype), Is.EqualTo(browseName));
|
|
Assert.That(table.FindReferenceType(browseName), Is.EqualTo(ReferenceTypeIds.HasSubtype));
|
|
Assert.That(table.IsEncodingOf(DataTypeIds.Structure, DataTypeIds.BaseDataType), Is.True);
|
|
Assert.That(table.IsEncodingFor(DataTypeIds.Structure, new ExtensionObject()), Is.True);
|
|
Assert.That(table.IsEncodingFor(DataTypeIds.Structure, new Variant(123)), Is.True);
|
|
Assert.That(table.FindDataTypeId(DataTypeIds.Structure), Is.EqualTo(DataTypeIds.BaseDataType));
|
|
Assert.That(table.FindDataTypeId((NodeId)DataTypeIds.Structure), Is.EqualTo(DataTypeIds.BaseDataType));
|
|
Assert.That(asyncTable.CallCount, Is.EqualTo(16));
|
|
}
|
|
|
|
[Test]
|
|
public void AsyncNodeTableAdapterForwardsSynchronousCallsAndTables()
|
|
{
|
|
var asyncTypeTable = new RecordingAsyncTypeTable();
|
|
var asyncNodeTable = new RecordingAsyncNodeTable(asyncTypeTable);
|
|
INodeTable table = asyncNodeTable.AsNodeTable();
|
|
ExpandedNodeId sourceId = ObjectIds.Server;
|
|
|
|
Assert.That(table.NamespaceUris, Is.SameAs(asyncNodeTable.NamespaceUris));
|
|
Assert.That(table.ServerUris, Is.SameAs(asyncNodeTable.ServerUris));
|
|
Assert.That(table.TypeTree.IsKnown(ObjectTypeIds.BaseObjectType), Is.True);
|
|
Assert.That(table.Exists(sourceId), Is.True);
|
|
Assert.That(table.Find(sourceId), Is.Null);
|
|
Assert.That(
|
|
table.Find(sourceId, ReferenceTypeIds.HasComponent, false, true, new QualifiedName(BrowseNames.Server)),
|
|
Is.Null);
|
|
Assert.That(
|
|
table.Find(sourceId, ReferenceTypeIds.HasComponent, false, true),
|
|
Is.Empty);
|
|
Assert.That(asyncNodeTable.CallCount, Is.EqualTo(4));
|
|
}
|
|
|
|
private static readonly string[] s_preferredLocales = ["en-US", "de-DE"];
|
|
|
|
private sealed class RecordingAsyncNodeTable : IAsyncNodeTable
|
|
{
|
|
public RecordingAsyncNodeTable(IAsyncTypeTable typeTree)
|
|
{
|
|
TypeTree = typeTree;
|
|
}
|
|
|
|
public int CallCount { get; private set; }
|
|
|
|
public NamespaceTable NamespaceUris { get; } = new();
|
|
|
|
public StringTable ServerUris { get; } = new();
|
|
|
|
public IAsyncTypeTable TypeTree { get; }
|
|
|
|
public ValueTask<bool> ExistsAsync(ExpandedNodeId nodeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(nodeId == ObjectIds.Server);
|
|
}
|
|
|
|
public ValueTask<INode?> FindAsync(ExpandedNodeId nodeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<INode?>((INode?)null);
|
|
}
|
|
|
|
public ValueTask<INode?> FindAsync(
|
|
ExpandedNodeId sourceId,
|
|
NodeId referenceTypeId,
|
|
bool isInverse,
|
|
bool includeSubtypes,
|
|
QualifiedName browseName,
|
|
CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<INode?>((INode?)null);
|
|
}
|
|
|
|
public ValueTask<ArrayOf<INode>> FindAsync(
|
|
ExpandedNodeId sourceId,
|
|
NodeId referenceTypeId,
|
|
bool isInverse,
|
|
bool includeSubtypes,
|
|
CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<ArrayOf<INode>>(new ArrayOf<INode>(Array.Empty<INode>()));
|
|
}
|
|
}
|
|
|
|
private sealed class RecordingAsyncTypeTable : IAsyncTypeTable
|
|
{
|
|
public int CallCount { get; private set; }
|
|
|
|
public ValueTask<bool> IsKnownAsync(ExpandedNodeId typeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(typeId == ObjectTypeIds.BaseObjectType);
|
|
}
|
|
|
|
public ValueTask<bool> IsKnownAsync(NodeId typeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(typeId == ObjectTypeIds.BaseObjectType);
|
|
}
|
|
|
|
public ValueTask<NodeId> FindSuperTypeAsync(ExpandedNodeId typeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<NodeId>(ObjectTypeIds.BaseObjectType);
|
|
}
|
|
|
|
public ValueTask<NodeId> FindSuperTypeAsync(NodeId typeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<NodeId>(VariableTypeIds.BaseVariableType);
|
|
}
|
|
|
|
public ValueTask<ArrayOf<NodeId>> FindSubTypesAsync(ExpandedNodeId typeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<ArrayOf<NodeId>>(
|
|
new ArrayOf<NodeId>(new[] { VariableTypeIds.BaseVariableType }));
|
|
}
|
|
|
|
public ValueTask<bool> IsTypeOfAsync(
|
|
ExpandedNodeId subTypeId,
|
|
ExpandedNodeId superTypeId,
|
|
CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(true);
|
|
}
|
|
|
|
public ValueTask<bool> IsTypeOfAsync(NodeId subTypeId, NodeId superTypeId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(false);
|
|
}
|
|
|
|
public ValueTask<QualifiedName> FindReferenceTypeNameAsync(
|
|
NodeId referenceTypeId,
|
|
CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<QualifiedName>(new QualifiedName(BrowseNames.HasSubtype));
|
|
}
|
|
|
|
public ValueTask<NodeId> FindReferenceTypeAsync(QualifiedName browseName, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<NodeId>(ReferenceTypeIds.HasSubtype);
|
|
}
|
|
|
|
public ValueTask<bool> IsEncodingOfAsync(
|
|
ExpandedNodeId encodingId,
|
|
ExpandedNodeId datatypeId,
|
|
CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(true);
|
|
}
|
|
|
|
public ValueTask<bool> IsEncodingForAsync(
|
|
NodeId expectedTypeId,
|
|
ExtensionObject value,
|
|
CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(true);
|
|
}
|
|
|
|
public ValueTask<bool> IsEncodingForAsync(NodeId expectedTypeId, Variant value, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<bool>(true);
|
|
}
|
|
|
|
public ValueTask<NodeId> FindDataTypeIdAsync(ExpandedNodeId encodingId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<NodeId>(DataTypeIds.BaseDataType);
|
|
}
|
|
|
|
public ValueTask<NodeId> FindDataTypeIdAsync(NodeId encodingId, CancellationToken ct = default)
|
|
{
|
|
CallCount++;
|
|
return new ValueTask<NodeId>(DataTypeIds.BaseDataType);
|
|
}
|
|
}
|
|
}
|
|
}
|