Files
romanett 9b034f8291 Enable nullable reference types centrally; migrate all unset and annotations-only projects (#4700)
# 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>
2026-10-08 08:48:13 +02:00

627 lines
23 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 System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using Opc.Ua.Schema.Model;
using Opc.Ua.Types;
namespace Opc.Ua.SourceGeneration
{
/// <summary>
/// Generates XML schema files from model designs.
/// </summary>
internal sealed class XmlSchemaGenerator : IGenerator
{
/// <summary>
/// Initializes a new instance of the <see cref="XmlSchemaGenerator"/> class.
/// </summary>
public XmlSchemaGenerator(IGeneratorContext context)
{
m_context = context ?? throw new ArgumentNullException(nameof(context));
}
/// <summary>
/// Validate schema output after generation.
/// </summary>
public bool ValidateOutput { get; set; }
/// <summary>
/// Generates the XML schema file for the supplied nodes.
/// </summary>
public IEnumerable<Resource> Emit()
{
string namespacePrefix = m_context.ModelDesign.TargetNamespace.Prefix;
string schemaFile = Path.Combine(m_context.OutputFolder, CoreUtils.Format(
"{0}.Types.xsd",
namespacePrefix));
WriteTemplate_XmlSchema(schemaFile);
if (ValidateOutput)
{
// Validate generated file
var validator = new Schema.Xml.XmlSchemaValidator2(
m_context.FileSystem,
[]);
validator.Validate(schemaFile);
}
return [schemaFile.AsTextFileResource(namespacePrefix)];
}
private void WriteTemplate_XmlSchema(string fileName)
{
using TextWriter writer = m_context.FileSystem.CreateTextWriter(fileName);
using var templateWriter = new TemplateWriter(writer);
var template = new Template(templateWriter, XmlSchemaTemplates.File);
if (!string.IsNullOrEmpty(m_context.ModelDesign.TargetNamespace.XmlNamespace))
{
template.AddReplacement(
Tokens.Namespace,
m_context.ModelDesign.TargetNamespace.XmlNamespace);
}
else
{
template.AddReplacement(
Tokens.Namespace,
m_context.ModelDesign.TargetNamespace.Value);
}
template.AddReplacement(Tokens.TargetVersion, m_context.ModelDesign.TargetVersion);
template.AddReplacement(Tokens.ModelUri, m_context.ModelDesign.TargetNamespace.Value);
template.AddReplacement(Tokens.TargetPublicationDate, XmlConvert.ToString(
m_context.ModelDesign.TargetPublicationDate ?? DateTime.MinValue,
XmlDateTimeSerializationMode.Utc));
template.AddReplacement(
Tokens.XmlnsS0ListOfNamespaces,
m_context.ModelDesign.Namespaces,
LoadTemplate_Imports);
template.AddReplacement(
Tokens.Imports,
m_context.ModelDesign.Namespaces,
LoadTemplate_Imports);
template.AddReplacement(
Tokens.BuiltInTypes,
XmlSchemaTemplates.BuiltInTypes,
[m_context.ModelDesign],
LoadTemplate_DataType,
WriteTemplate_DataType);
template.AddReplacement(
Tokens.ListOfTypes,
[.. m_context.ModelDesign.GetNodeDesigns()],
LoadTemplate_DataType,
WriteTemplate_DataType);
template.Render();
}
private TemplateString? LoadTemplate_Imports(ILoadContext context)
{
if (context.Target is not Namespace ns)
{
return null;
}
if (ns.Value == m_context.ModelDesign.TargetNamespace.Value)
{
return null;
}
string uri = ns.Value;
if (!string.IsNullOrEmpty(ns.XmlNamespace))
{
uri = ns.XmlNamespace;
}
if (context.Token == Tokens.XmlnsS0ListOfNamespaces)
{
if (ns.Value == Namespaces.OpcUa)
{
return null;
}
context.Out.WriteLine(
"xmlns:{0}=\"{1}\"",
m_context.ModelDesign.Namespaces.GetXmlNamespacePrefix(ns.Value),
uri);
return null;
}
context.Out.WriteLine("<xs:import namespace=\"{0}\" />", uri);
return null;
}
private TemplateString? LoadTemplate_DataType(ILoadContext context)
{
if (context.Target is IModelDesign design)
{
if (design.TargetNamespace.Value == Namespaces.OpcUa)
{
return context.TemplateString;
}
return null;
}
if (context.Target is not DataTypeDesign dataType)
{
return null;
}
#if TRUE
// don't write built-in types already in the template.
if (dataType.NumericId < 256 &&
dataType.SymbolicId.Namespace == Namespaces.OpcUa)
{
switch (dataType.NumericId)
{
case DataTypes.PermissionType:
case DataTypes.RolePermissionType:
case DataTypes.DataTypeDefinition:
case DataTypes.StructureDefinition:
case DataTypes.StructureField:
case DataTypes.StructureType:
case DataTypes.EnumDefinition:
case DataTypes.EnumField:
break;
default:
return null;
}
}
#endif
BasicDataType basicType = dataType.BasicDataType;
if (basicType == BasicDataType.Enumeration)
{
var baseType = dataType.BaseTypeNode as DataTypeDesign;
if (baseType?.SymbolicId == new XmlQualifiedName("OptionSet", Namespaces.OpcUa))
{
return XmlSchemaTemplates.DerivedType;
}
return XmlSchemaTemplates.EnumeratedType;
}
else if (basicType == BasicDataType.UserDefined)
{
if (dataType.BaseTypeNode!.SymbolicName.Name == "Union")
{
return XmlSchemaTemplates.Union;
}
else if (dataType.BaseTypeNode.SymbolicName.Name == "Structure" ||
WritesEncodingMaskBeforeBaseFields(dataType))
{
// A derived type that introduces optional fields writes
// the encoding mask before the base fields, which an
// xs:extension cannot express: it is flattened.
return XmlSchemaTemplates.ComplexType;
}
else
{
return XmlSchemaTemplates.DerivedType;
}
}
return XmlSchemaTemplates.SimpleType;
}
private bool WriteTemplate_DataType(IWriteContext context)
{
if (context.Target is IModelDesign design)
{
if (design.TargetNamespace.Value == Namespaces.OpcUa)
{
return context.Template.Render();
}
return false;
}
if (context.Target is not DataTypeDesign dataType)
{
return false;
}
var baseType = dataType.BaseTypeNode as DataTypeDesign;
if (baseType != null)
{
context.Template.AddReplacement(Tokens.BaseType, baseType.GetXmlDataType(
ValueRank.Scalar,
m_context.ModelDesign.TargetNamespace.Value,
m_context.ModelDesign.Namespaces));
}
context.Template.AddReplacement(
Tokens.TypeName,
dataType.SymbolicName.Name.AsXmlAttributeValue());
if (dataType.BasicDataType == BasicDataType.Enumeration && dataType.IsOptionSet)
{
context.Template.AddReplacement(Tokens.XsRestrictionBaseType,
baseType.GetXmlDataType(
ValueRank.Scalar,
m_context.ModelDesign.TargetNamespace.Value,
m_context.ModelDesign.Namespaces));
}
else
{
context.Template.AddReplacement(Tokens.XsRestrictionBaseType, "xs:string");
}
context.Template.AddReplacement(
Tokens.Documentation,
XmlSchemaTemplates.Documentation,
[dataType],
LoadTemplate_XmlDocumentation,
WriteTemplate_XmlDocumentation);
context.Template.AddReplacement(
Tokens.CollectionType,
XmlSchemaTemplates.CollectionType,
[dataType],
LoadTemplate_XmlCollectionType,
WriteTemplate_XmlCollectionType);
context.Template.AddReplacement(
Tokens.ListOfFields,
GetXmlTypeFields(dataType),
LoadTemplate_XmlTypeFields);
return context.Template.Render();
}
/// <summary>
/// The elements of a data type's content model. A structure that
/// writes the encoding mask (XmlEncoder emits it as the first child
/// element) starts with an EncodingMask element; a flattened derived
/// structure also lists the fields of its ancestors.
/// </summary>
private List<object> GetXmlTypeFields(DataTypeDesign dataType)
{
var fields = new List<object>();
if (dataType.BasicDataType != BasicDataType.UserDefined || dataType.IsUnion)
{
fields.AddRange(dataType.Fields ?? []);
return fields;
}
bool flattened = WritesEncodingMaskBeforeBaseFields(dataType);
if (HasOptionalFields(dataType) && !HasAncestorWithOptionalFields(dataType))
{
fields.Add(kEncodingMaskElement);
}
if (flattened)
{
var ancestors = new Stack<DataTypeDesign>();
for (var parent = dataType.BaseTypeNode as DataTypeDesign;
parent != null && parent.BasicDataType == BasicDataType.UserDefined;
parent = parent.BaseTypeNode as DataTypeDesign)
{
ancestors.Push(parent);
}
while (ancestors.Count > 0)
{
DataTypeDesign ancestor = ancestors.Pop();
foreach (Parameter field in ancestor.Fields ?? [])
{
if (!m_context.ModelDesign.IsExcluded(field))
{
fields.Add(field);
}
}
}
}
fields.AddRange(dataType.Fields ?? []);
return fields;
}
/// <summary>
/// True if the generated Encode writes the encoding mask ahead of
/// the fields of a base structure (DerivedClassWithOptionalFields
/// without an ancestor that has optional fields).
/// </summary>
private static bool WritesEncodingMaskBeforeBaseFields(DataTypeDesign dataType)
{
return dataType.BasicDataType == BasicDataType.UserDefined &&
!dataType.IsUnion &&
dataType.BaseTypeNode is DataTypeDesign baseType &&
baseType.BasicDataType == BasicDataType.UserDefined &&
HasOptionalFields(dataType) &&
!HasAncestorWithOptionalFields(dataType);
}
private static bool HasOptionalFields(DataTypeDesign dataType)
{
return dataType.Fields != null && dataType.Fields.Any(f => f.IsOptional);
}
private static bool HasAncestorWithOptionalFields(DataTypeDesign dataType)
{
for (var parent = dataType.BaseTypeNode as DataTypeDesign;
parent != null && parent.BasicDataType == BasicDataType.UserDefined;
parent = parent.BaseTypeNode as DataTypeDesign)
{
if (HasOptionalFields(parent))
{
return true;
}
}
return false;
}
/// <summary>
/// OPC 10000-6 5.3.6: the first element of a structure with optional
/// fields is the (mandatory) bit mask, typed xs:unsignedLong as in the
/// example of the clause; the 32 bit mask XmlEncoder writes is a
/// valid value of it.
/// </summary>
private const string kEncodingMaskElement =
"<xs:element name=\"EncodingMask\" type=\"xs:unsignedLong\" />";
private TemplateString? LoadTemplate_XmlTypeFields(ILoadContext context)
{
if (context.Target is string element)
{
context.Out.WriteLine(element);
return null;
}
if (context.Target is not Parameter field)
{
return null;
}
if (field.Parent is not DataTypeDesign dataType)
{
return null;
}
// The authored field name lands in XSD attributes, so it has to be
// escaped - a BrowseName may legally contain '&', '<' or a quote,
// which would otherwise make the emitted schema non-well-formed.
string fieldName = field.Name.AsXmlAttributeValue();
BasicDataType basicType = dataType.BasicDataType;
if (basicType == BasicDataType.Enumeration)
{
if (dataType.IsOptionSet)
{
return null;
}
if (field.IdentifierInName)
{
context.Out.WriteLine(
"<xs:enumeration value=\"{0}\" />",
fieldName);
return null;
}
context.Out.WriteLine(
"<xs:enumeration value=\"{0}_{1}\" />",
fieldName,
field.Identifier);
return null;
}
basicType = field.DataTypeNode!.BasicDataType;
if (basicType == BasicDataType.XmlElement &&
field.ValueRank == ValueRank.Scalar)
{
context.Out.WriteLine("<xs:element name=\"{0}\" minOccurs=\"0\" nillable=\"true\">", fieldName);
context.Out.WriteLine(" <xs:complexType>");
context.Out.WriteLine(" <xs:sequence>");
context.Out.WriteLine(" <xs:any minOccurs=\"0\" processContents=\"lax\" />");
context.Out.WriteLine(" </xs:sequence>");
context.Out.WriteLine(" </xs:complexType>");
context.Out.WriteLine("</xs:element>");
return null;
}
if (DataTypeGenerator.IsEncodedAsVariant(field))
{
// ScalarOrArray, Any, ... are written as a Variant.
context.Out.WriteLine(
"<xs:element name=\"{0}\" type=\"ua:Variant\" minOccurs=\"0\" nillable=\"true\" />",
fieldName);
return null;
}
if (field.ValueRank == ValueRank.OneOrMoreDimensions)
{
// "Multi-dimensional Array parameters are encoded using the
// Matrix type" (OPC 10000-6 5.3.4, 5.3.1.17): the field element
// itself is of type Matrix and holds the Dimensions and
// Elements, for every element type (built-in, enumeration,
// Variant, structure, subtyped structure) - no Matrix wrapper.
context.Out.WriteLine(
"<xs:element name=\"{0}\" type=\"ua:Matrix\" minOccurs=\"0\" nillable=\"true\" />",
fieldName);
return null;
}
if (field.ValueRank != ValueRank.Scalar)
{
string fieldDataType = field.DataTypeNode.GetXmlDataType(
field.ValueRank,
m_context.ModelDesign.TargetNamespace.Value,
m_context.ModelDesign.Namespaces);
if (basicType == BasicDataType.UserDefined && field.AllowSubTypes)
{
fieldDataType = "ua:ListOfExtensionObject";
}
context.Out.WriteLine(
"<xs:element name=\"{0}\" type=\"{1}\" minOccurs=\"0\" nillable=\"true\" />",
fieldName,
fieldDataType);
}
else
{
switch (basicType)
{
case BasicDataType.String:
case BasicDataType.ByteString:
case BasicDataType.DiagnosticInfo:
case BasicDataType.ExpandedNodeId:
case BasicDataType.LocalizedText:
case BasicDataType.NodeId:
case BasicDataType.QualifiedName:
case BasicDataType.Structure:
case BasicDataType.DataValue:
context.Out.WriteLine(
"<xs:element name=\"{0}\" type=\"{1}\" minOccurs=\"0\" nillable=\"true\" />",
fieldName,
field.DataTypeNode.GetXmlDataType(
field.ValueRank,
m_context.ModelDesign.TargetNamespace.Value,
m_context.ModelDesign.Namespaces));
break;
case BasicDataType.Guid:
case BasicDataType.StatusCode:
context.Out.WriteLine(
"<xs:element name=\"{0}\" type=\"{1}\" minOccurs=\"0\" />",
fieldName,
field.DataTypeNode.GetXmlDataType(
field.ValueRank,
m_context.ModelDesign.TargetNamespace.Value,
m_context.ModelDesign.Namespaces));
break;
case BasicDataType.UserDefined:
string fieldDataType = field.DataTypeNode.GetXmlDataType(
field.ValueRank,
m_context.ModelDesign.TargetNamespace.Value,
m_context.ModelDesign.Namespaces);
if (field.AllowSubTypes)
{
fieldDataType = "ua:ExtensionObject";
}
context.Out.WriteLine(
"<xs:element name=\"{0}\" type=\"{1}\" minOccurs=\"0\" nillable=\"true\" />",
fieldName,
fieldDataType);
break;
default:
context.Out.WriteLine("<xs:element name=\"{0}\" type=\"{1}\" minOccurs=\"0\" />",
fieldName,
field.DataTypeNode.GetXmlDataType(
field.ValueRank,
m_context.ModelDesign.TargetNamespace.Value,
m_context.ModelDesign.Namespaces));
break;
}
}
return null;
}
private TemplateString? LoadTemplate_XmlDocumentation(ILoadContext context)
{
if (context.Target is not DataTypeDesign dataType)
{
return null;
}
if (dataType.Description == null || dataType.Description.IsAutogenerated)
{
return null;
}
return context.TemplateString;
}
private bool WriteTemplate_XmlDocumentation(IWriteContext context)
{
if (context.Target is not DataTypeDesign dataType)
{
return false;
}
context.Template.AddReplacement(
Tokens.Description,
dataType.Description.Value.AsXmlText());
return context.Template.Render();
}
private TemplateString? LoadTemplate_XmlCollectionType(ILoadContext context)
{
if (context.Target is not DataTypeDesign dataType)
{
return null;
}
if (dataType.NoArraysAllowed)
{
return null;
}
return context.TemplateString;
}
private bool WriteTemplate_XmlCollectionType(IWriteContext context)
{
if (context.Target is not DataTypeDesign dataType)
{
return false;
}
context.Template.AddReplacement(
Tokens.TypeName,
dataType.SymbolicName.Name.AsXmlAttributeValue());
context.Template.AddReplacement(
Tokens.Nillable,
!dataType.BasicDataType.IsXmlNillable() ?
string.Empty : "nillable=\"true\" ");
return context.Template.Render();
}
private readonly IGeneratorContext m_context;
}
}