Skip to content

Commit

Permalink
Set TypeId for structured types at decode. (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinMoldovean committed Jan 29, 2021
1 parent af6a933 commit b363442
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
29 changes: 24 additions & 5 deletions Stack/Opc.Ua.Core/Types/Encoders/BinaryDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,13 @@ public ExtensionObject ReadExtensionObject(string fieldName)
}

/// <summary>
/// Reads an encodeable object from the stream.
/// Reads an encodeable object from the stream.
/// </summary>
public IEncodeable ReadEncodeable(string fieldName, System.Type systemType)
/// <param name="fieldName">The encodeable object field name</param>
/// <param name="systemType">The system type of the encopdeable object to be read</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instance that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> object that was read from the stream.</returns>
public IEncodeable ReadEncodeable(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null)
{
if (systemType == null) throw new ArgumentNullException(nameof(systemType));

Expand All @@ -788,6 +792,17 @@ public IEncodeable ReadEncodeable(string fieldName, System.Type systemType)
Utils.Format("Cannot decode type '{0}'.", systemType.FullName));
}

if (encodeableTypeId != null)
{
// set type identifier for custom complex data types before decode.
IComplexTypeInstance complexTypeInstance = encodeable as IComplexTypeInstance;

if (complexTypeInstance != null)
{
complexTypeInstance.TypeId = encodeableTypeId;
}
}

// check the nesting level for avoiding a stack overflow.
if (m_nestingLevel > m_context.MaxEncodingNestingLevels)
{
Expand Down Expand Up @@ -1365,9 +1380,13 @@ public ExtensionObjectCollection ReadExtensionObjectArray(string fieldName)
}

/// <summary>
/// Reads an encodeable object array from the stream.
/// Reads an encodeable array from the stream.
/// </summary>
public Array ReadEncodeableArray(string fieldName, System.Type systemType)
/// <param name="fieldName">The encodeable array field name</param>
/// <param name="systemType">The system type of the encopdeable objects to be read object</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instances that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> array that was read from the stream.</returns>
public Array ReadEncodeableArray(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null)
{
int length = ReadArrayLength();

Expand All @@ -1380,7 +1399,7 @@ public Array ReadEncodeableArray(string fieldName, System.Type systemType)

for (int ii = 0; ii < length; ii++)
{
values.SetValue(ReadEncodeable(null, systemType), ii);
values.SetValue(ReadEncodeable(null, systemType, encodeableTypeId), ii);
}

return values;
Expand Down
16 changes: 12 additions & 4 deletions Stack/Opc.Ua.Core/Types/Encoders/IDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ public interface IDecoder
ExtensionObject ReadExtensionObject(string fieldName);

/// <summary>
/// Reads an encodeable object from the stream.
/// Reads an encodeable object from the stream.
/// </summary>
IEncodeable ReadEncodeable(string fieldName, System.Type systemType);
/// <param name="fieldName">The encodeable object field name</param>
/// <param name="systemType">The system type of the encopdeable object to be read</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instance that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> object that was read from the stream.</returns>
IEncodeable ReadEncodeable(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null);

/// <summary>
/// Reads an enumerated value from the stream.
Expand Down Expand Up @@ -308,9 +312,13 @@ public interface IDecoder
ExtensionObjectCollection ReadExtensionObjectArray(string fieldName);

/// <summary>
/// Reads an encodeable object array from the stream.
/// Reads an encodeable array from the stream.
/// </summary>
Array ReadEncodeableArray(string fieldName, System.Type systemType);
/// <param name="fieldName">The encodeable array field name</param>
/// <param name="systemType">The system type of the encopdeable objects to be read object</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instances that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> array that was read from the stream.</returns>
Array ReadEncodeableArray(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null);

/// <summary>
/// Reads an enumerated value array from the stream.
Expand Down
29 changes: 23 additions & 6 deletions Stack/Opc.Ua.Core/Types/Encoders/JsonDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,9 +1608,11 @@ public ExtensionObject ReadExtensionObject(string fieldName)
/// <summary>
/// Reads an encodeable object from the stream.
/// </summary>
public IEncodeable ReadEncodeable(
string fieldName,
System.Type systemType)
/// <param name="fieldName">The encodeable object field name</param>
/// <param name="systemType">The system type of the encopdeable object to be read</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instance that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> object that was read from the stream.</returns>
public IEncodeable ReadEncodeable(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null)
{
if (systemType == null)
{
Expand All @@ -1631,6 +1633,17 @@ public ExtensionObject ReadExtensionObject(string fieldName)
throw new ServiceResultException(StatusCodes.BadDecodingError, Utils.Format("Type does not support IEncodeable interface: '{0}'", systemType.FullName));
}

if (encodeableTypeId != null)
{
// set type identifier for custom complex data types before decode.
IComplexTypeInstance complexTypeInstance = value as IComplexTypeInstance;

if (complexTypeInstance != null)
{
complexTypeInstance.TypeId = encodeableTypeId;
}
}

// check the nesting level for avoiding a stack overflow.
if (m_nestingLevel > m_context.MaxEncodingNestingLevels)
{
Expand Down Expand Up @@ -2458,9 +2471,13 @@ public ExtensionObjectCollection ReadExtensionObjectArray(string fieldName)
}

/// <summary>
/// Reads an encodeable object array from the stream.
/// Reads an encodeable array from the stream.
/// </summary>
public Array ReadEncodeableArray(string fieldName, System.Type systemType)
/// <param name="fieldName">The encodeable array field name</param>
/// <param name="systemType">The system type of the encopdeable objects to be read object</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instances that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> array that was read from the stream.</returns>
public Array ReadEncodeableArray(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null)
{
if (systemType == null)
{
Expand All @@ -2481,7 +2498,7 @@ public Array ReadEncodeableArray(string fieldName, System.Type systemType)
try
{
m_stack.Push(token[ii]);
var element = ReadEncodeable(null, systemType);
var element = ReadEncodeable(null, systemType, encodeableTypeId);
values.SetValue(element, ii);
}
finally
Expand Down
29 changes: 23 additions & 6 deletions Stack/Opc.Ua.Core/Types/Encoders/XmlDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,9 +1456,11 @@ public ExtensionObject ReadExtensionObject(string fieldName)
/// <summary>
/// Reads an encodeable object from the stream.
/// </summary>
public IEncodeable ReadEncodeable(
string fieldName,
System.Type systemType)
/// <param name="fieldName">The encodeable object field name</param>
/// <param name="systemType">The system type of the encopdeable object to be read</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instance that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> object that was read from the stream.</returns>
public IEncodeable ReadEncodeable(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null)
{
if (systemType == null) throw new ArgumentNullException(nameof(systemType));

Expand All @@ -1471,6 +1473,17 @@ public ExtensionObject ReadExtensionObject(string fieldName)
Utils.Format("Type does not support IEncodeable interface: '{0}'", systemType.FullName));
}

if (encodeableTypeId != null)
{
// set type identifier for custom complex data types before decode.
IComplexTypeInstance complexTypeInstance = value as IComplexTypeInstance;

if (complexTypeInstance != null)
{
complexTypeInstance.TypeId = encodeableTypeId;
}
}

// check the nesting level for avoiding a stack overflow.
if (m_nestingLevel > m_context.MaxEncodingNestingLevels)
{
Expand Down Expand Up @@ -2497,9 +2510,13 @@ public ExtensionObjectCollection ReadExtensionObjectArray(string fieldName)
}

/// <summary>
/// Reads an encodeable object array from the stream.
/// Reads an encodeable array from the stream.
/// </summary>
public Array ReadEncodeableArray(string fieldName, System.Type systemType)
/// <param name="fieldName">The encodeable array field name</param>
/// <param name="systemType">The system type of the encopdeable objects to be read object</param>
/// <param name="encodeableTypeId">The TypeId for the <see cref="IEncodeable"/> instances that will be read.</param>
/// <returns>An <see cref="IEncodeable"/> array that was read from the stream.</returns>
public Array ReadEncodeableArray(string fieldName, System.Type systemType, ExpandedNodeId encodeableTypeId = null)
{
if (systemType == null) throw new ArgumentNullException(nameof(systemType));

Expand All @@ -2514,7 +2531,7 @@ public Array ReadEncodeableArray(string fieldName, System.Type systemType)

while (MoveToElement(xmlName.Name))
{
encodeables.Add(ReadEncodeable(xmlName.Name, systemType));
encodeables.Add(ReadEncodeable(xmlName.Name, systemType, encodeableTypeId));
}

// check the length.
Expand Down

0 comments on commit b363442

Please sign in to comment.