mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-20 08:41:51 +08:00
62658d1a44
# Description Fix runtime schema resolution and provide a supported DI-free construction path for generated data types. - Resolve registered data types by local/index-form and namespace-URI identifiers without using BrowseName as a namespace mapping. - Preserve identifier kind in URI registry keys, so numeric zero and empty GUID/string/ByteString identifiers remain distinct in nonzero namespaces, including during replacement. - Reconstruct typed identifiers during factory URI-to-index fallback while retaining exact lookup precedence and unknown-namespace/remote-server guards. - Make the built-in JSON, XSD, and BSD generators public, stabilize `EncodeableFactoryDefinitionSource`, and document generated activator/factory usage. - Keep the stable-API assertion effective across the netstandard2.1/net8.0 polyfill boundary by inspecting attribute metadata without referencing the ambiguous CLR type. - Merge the latest `master` without dropping upstream changes. ## Related Issues - Fixes #4423 - Fixes #4424 ## Validation - `Opc.Ua.Core.Schema.Tests`: 157/157 on `net10.0` - `Opc.Ua.Core.Schema.Tests`: 157/157 on `net48` - `Opc.Ua.Core.Schema.Tests`: 157/157 with `CustomTestTarget=netstandard2.1` and its `net8.0` consumer - Managed `SchemaAotTests`: 4/4 - NativeAOT Release publish and filtered `SchemaAotTests`: 4/4 - All three review reproductions pass against rebuilt assemblies. ## 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. - [x] 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. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: b17fd166-fdef-4660-a9d5-97c0e69f1bd5
189 lines
8.0 KiB
C#
189 lines
8.0 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 Microsoft.Extensions.DependencyInjection;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Schema.Json;
|
|
|
|
namespace Opc.Ua.Schema.Tests
|
|
{
|
|
/// <summary>
|
|
/// Tests for the schema generation dependency injection registration.
|
|
/// </summary>
|
|
[TestFixture]
|
|
[Category("Schema")]
|
|
public class SchemaServiceCollectionExtensionsTests
|
|
{
|
|
[Test]
|
|
public void AddSchemaGenerationResolvesProviderAndResolvesRegisteredType()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddOpcUa().AddSchemaGeneration();
|
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
|
|
|
DataTypeDefinitionRegistry registry = serviceProvider.GetRequiredService<DataTypeDefinitionRegistry>();
|
|
UaTypeDescription type = SchemaTestData.Structure(
|
|
3001,
|
|
"SampleType",
|
|
SchemaTestData.Field("Id", SchemaTestData.BuiltIn(BuiltInType.Int32)));
|
|
registry.Add(type);
|
|
|
|
ISchemaProvider provider = serviceProvider.GetRequiredService<ISchemaProvider>();
|
|
bool resolved = provider.TryGetSchema(
|
|
type.TypeId,
|
|
UaSchemaFormat.JsonCompact,
|
|
UaSchemaScope.Type,
|
|
out IUaSchema? schema);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(resolved, Is.True);
|
|
Assert.That(schema, Is.Not.Null);
|
|
Assert.That(schema!.Format, Is.EqualTo(UaSchemaFormat.JsonCompact));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public void AddSchemaGenerationResolvesUriFormRegisteredNode()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddOpcUa().AddSchemaGeneration();
|
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
|
|
|
var namespaceUris = new NamespaceTable();
|
|
namespaceUris.Append(SchemaTestData.TestNamespace);
|
|
var node = new DataTypeNode
|
|
{
|
|
NodeId = new NodeId(3002, SchemaTestData.TestNamespaceIndex),
|
|
BrowseName = new QualifiedName("RegisteredNode", SchemaTestData.TestNamespaceIndex),
|
|
DataTypeDefinition = new ExtensionObject(new StructureDefinition
|
|
{
|
|
BaseDataType = DataTypeIds.Structure,
|
|
StructureType = StructureType.Structure,
|
|
Fields =
|
|
[
|
|
SchemaTestData.Field("Value", SchemaTestData.BuiltIn(BuiltInType.Int32))
|
|
]
|
|
})
|
|
};
|
|
DataTypeDefinitionRegistry registry =
|
|
serviceProvider.GetRequiredService<DataTypeDefinitionRegistry>();
|
|
|
|
bool added = registry.TryAddDataType(node, namespaceUris);
|
|
ISchemaProvider provider = serviceProvider.GetRequiredService<ISchemaProvider>();
|
|
bool resolved = provider.TryGetSchema(
|
|
NodeId.ToExpandedNodeId(node.NodeId, namespaceUris),
|
|
UaSchemaFormat.JsonCompact,
|
|
UaSchemaScope.Type,
|
|
out IUaSchema? schema);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(added, Is.True);
|
|
Assert.That(resolved, Is.True);
|
|
Assert.That(schema, Is.Not.Null);
|
|
});
|
|
}
|
|
|
|
[TestCaseSource(typeof(SchemaTestData), nameof(SchemaTestData.TypeIdentifierCases))]
|
|
public void RegisteredIdentifierResolvesWithoutUsingBrowseNameNamespace(NodeId nodeId)
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddOpcUa().AddSchemaGeneration();
|
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
|
var namespaceUris = new NamespaceTable();
|
|
namespaceUris.Append(SchemaTestData.TestNamespace);
|
|
namespaceUris.Append(SchemaTestData.OtherNamespace);
|
|
var node = new DataTypeNode
|
|
{
|
|
NodeId = nodeId,
|
|
BrowseName = new QualifiedName("RegisteredEnum", SchemaTestData.OtherNamespaceIndex),
|
|
DataTypeDefinition = new ExtensionObject(new EnumDefinition
|
|
{
|
|
Fields = [new EnumField { Name = "Running", Value = 2 }]
|
|
})
|
|
};
|
|
DataTypeDefinitionRegistry registry =
|
|
serviceProvider.GetRequiredService<DataTypeDefinitionRegistry>();
|
|
ISchemaProvider provider = serviceProvider.GetRequiredService<ISchemaProvider>();
|
|
|
|
Assert.That(registry.TryAddDataType(node, namespaceUris), Is.True);
|
|
Assert.That(
|
|
provider.TryGetSchema(
|
|
NodeId.ToExpandedNodeId(nodeId, namespaceUris),
|
|
UaSchemaFormat.JsonCompact,
|
|
UaSchemaScope.Type,
|
|
out IUaSchema? uriSchema),
|
|
Is.True);
|
|
Assert.That(
|
|
provider.TryGetSchema(
|
|
new ExpandedNodeId(nodeId),
|
|
UaSchemaFormat.JsonCompact,
|
|
UaSchemaScope.Type,
|
|
out IUaSchema? indexSchema),
|
|
Is.True);
|
|
Assert.That(uriSchema, Is.TypeOf<JsonSchemaDocument>());
|
|
var document = (JsonSchemaDocument)uriSchema!;
|
|
Assert.That(document.Root["title"]!.GetValue<string>(), Is.EqualTo("RegisteredEnum"));
|
|
Assert.That(uriSchema.ToSchemaString(), Does.Contain("Running"));
|
|
Assert.That(indexSchema!.ToSchemaString(), Is.EqualTo(uriSchema.ToSchemaString()));
|
|
}
|
|
|
|
[Test]
|
|
public void AddSchemaGenerationThrowsForNullBuilder()
|
|
{
|
|
Assert.That(
|
|
() => SchemaServiceCollectionExtensions.AddSchemaGeneration(null!),
|
|
Throws.ArgumentNullException.With.Property(nameof(ArgumentNullException.ParamName)).EqualTo("builder"));
|
|
}
|
|
|
|
[Test]
|
|
public void TryGetSchemaReturnsFalseForUnknownType()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddOpcUa().AddSchemaGeneration();
|
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
|
|
|
ISchemaProvider provider = serviceProvider.GetRequiredService<ISchemaProvider>();
|
|
bool resolved = provider.TryGetSchema(
|
|
new ExpandedNodeId(new NodeId(9999, 1)),
|
|
UaSchemaFormat.JsonCompact,
|
|
UaSchemaScope.Type,
|
|
out IUaSchema? schema);
|
|
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(resolved, Is.False);
|
|
Assert.That(schema, Is.Null);
|
|
});
|
|
}
|
|
}
|
|
}
|