Skip to content

_to_nodeid() fails with int arguments > 255 #1831

Open
@DrMatthiasArnold

Description

@DrMatthiasArnold

Describe the bug

Calls to _to_node_id() fail with int arguments > 255.
A typical example is a call like
self.get_referenced_nodes()
where the type of the reference is provided in parameter refs as integer argument.
Meanwhile a lot of standard OPC UA references have integer IDs >> 255 (see example):

To Reproduce

The example shows a search for references of type HasDictionaryEntry, a standard reference type of OPC UA.
It fails because the integer id of the type is HasDictionaryEntry = 17597.

nodes = await self.get_referenced_nodes(refs = ua.ObjectIds.HasDictionaryEntry, direction = ua.BrowseDirection.Forward, nodeclassmask = ua.NodeClass.Object)

The root cause of the failure is, that the current implementation of the function _to_nodeid(), which is utilized when issuing the call in the example, only supports TwoByteNodeIds for input arguments of type int.

Expected behavior

The implementation of _to_nodeid() should check the numeric range of the input argument and return an appropriate nodeid object, e.g. as depicted in the following code snippet with additional range check:

def _to_nodeid(nodeid: Union["Node", ua.NodeId, str, int]) -> ua.NodeId:
    if isinstance(nodeid, int):
            if nodeid <= 255:
                return ua.TwoByteNodeId(nodeid)
            elif nodeid <= 65535:
                return ua.FourByteNodeId(nodeid)
            else:
                return ua.NumericNodeId(nodeid)
    if isinstance(nodeid, Node):
        return nodeid.nodeid
    if isinstance(nodeid, ua.NodeId):
        return nodeid
    if isinstance(nodeid, str):
        return ua.NodeId.from_string(nodeid)
    raise ua.UaError(f"Could not resolve '{nodeid}' to a type id")

Version

Python-Version: 3.13.3

opcua-asyncio Version: master branch, 1.1.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions