Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions aas_core_codegen/csharp/lib/_generate_xml_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def generate(namespace: csharp_common.NamespaceIdentifier) -> str:
{{
{I}error = null;

{I}// NOTE (mristin):
{I}// XmlReader.ReadContentAsString throws an InvalidOperationException if
{I}// it starts at an element, so we report a nested element beforehand.
{I}if (reader.NodeType == Xml.XmlNodeType.Element)
{I}{{
{II}error = new Reporting.Error(
{III}"Expected \\"0\\" or \\"1\\" as the text of a <boolean> element, " +
{III}$"but got a nested element {{reader.Name}}");
{II}return null;
{I}}}

{I}string text;
{I}try
{I}{{
Expand Down Expand Up @@ -126,6 +137,17 @@ def generate(namespace: csharp_common.NamespaceIdentifier) -> str:
{{
{I}error = null;

{I}// NOTE (mristin):
{I}// XmlReader.ReadContentAsString throws an InvalidOperationException if
{I}// it starts at an element, so we report a nested element beforehand.
{I}if (reader.NodeType == Xml.XmlNodeType.Element)
{I}{{
{II}error = new Reporting.Error(
{III}"Expected a number as the text of a <double> element, " +
{III}$"but got a nested element {{reader.Name}}");
{II}return null;
{I}}}

{I}double result;
{I}try
{I}{{
Expand Down Expand Up @@ -172,6 +194,17 @@ def generate(namespace: csharp_common.NamespaceIdentifier) -> str:
{{
{I}error = null;

{I}// NOTE (mristin):
{I}// XmlReader.ReadContentAsString throws an InvalidOperationException if
{I}// it starts at an element, so we report a nested element beforehand.
{I}if (reader.NodeType == Xml.XmlNodeType.Element)
{I}{{
{II}error = new Reporting.Error(
{III}"Expected text as the content of a <string> element, " +
{III}$"but got a nested element {{reader.Name}}");
{II}return null;
{I}}}

{I}try
{I}{{
{II}return reader.ReadContentAsString();
Expand Down Expand Up @@ -506,14 +539,16 @@ def generate(namespace: csharp_common.NamespaceIdentifier) -> str:
{I}}}
{I}else
{I}{{
{II}key = DeserializeStringTextFrom(reader, out error)
{III}?? throw new System.InvalidOperationException(
{IIII}"Unexpected key null when error null");
{II}string? maybeKey = DeserializeStringTextFrom(reader, out error);
{II}if (error != null)
{II}{{
{III}return null;
{II}}}

{II}key = maybeKey
{III}?? throw new System.InvalidOperationException(
{IIII}"Unexpected key null when error null");

{II}XmlCommon.ConsumeEndElementInNoNamespace(
{III}reader, "name", false, out error);
{II}if (error != null)
Expand Down
13 changes: 13 additions & 0 deletions aas_core_codegen/csharp/lib/_generate_xmlization.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,19 @@ def _generate_as_text_combinators(
{III}return default!;
{II}}}

{II}// NOTE (mristin):
{II}// XmlReader.ReadContentAs* throws an InvalidOperationException if it
{II}// starts at an element, so we have to report a nested element before
{II}// the conversion. A nested element after a text is not our concern
{II}// here, as the caller expects the end element afterwards.
{II}if (reader.NodeType == Xml.XmlNodeType.Element)
{II}{{
{III}error = new Reporting.Error(
{IIII}$"Expected an XML content representing {{typeof(T).Name}}, " +
{IIII}$"but got a nested element {{reader.Name}}");
{III}return default!;
{II}}}

{II}try
{II}{{
{III}return readContent(reader);
Expand Down
29 changes: 29 additions & 0 deletions aas_core_codegen/csharp/tests/_generate_test_xml_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,35 @@ def generate(namespace: csharp_common.NamespaceIdentifier) -> str:
Stripped(
f"""\
[Test]
public void Test_deserialize_rejects_a_nested_element_in_a_text()
{{
{I}string[] texts =
{I}{{
{II}"<value><boolean><nested/></boolean></value>",
{II}"<value><double><nested/></double></value>",
{II}"<value><string><nested/></string></value>",
{II}"<value><struct><member>"
{III}+ "<name><nested/></name>"
{III}+ "<value><string>something</string></value>"
{II}+ "</member></struct></value>"
{I}}};

{I}foreach (string text in texts)
{I}{{
{II}Nodes.JsonNode? result = DeserializeFromString(
{III}text, out Our.Reporting.Error? error);

{II}Assert.IsNull(result, text);
{II}Assert.IsNotNull(error, text);
{II}Assert.IsTrue(
{III}error!.Cause.Contains("but got a nested element nested"),
{III}$"Unexpected cause for {{text}}: {{error.Cause}}");
{I}}}
}}"""
),
Stripped(
f"""\
[Test]
public void Test_deserialize_rejects_repeated_member_key()
{{
{I}string text = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<something xmlns="https://dummy.com"><someBool>true</someBool><someInt>1234</someInt><someFloat>34.23</someFloat><someString><nested>hello world</nested></someString><someBytes>SGVsbG8gV29ybGQ=</someBytes></something>
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ out Reporting.Error? error
return default!;
}

// NOTE (mristin):
// XmlReader.ReadContentAs* throws an InvalidOperationException if it
// starts at an element, so we have to report a nested element before
// the conversion. A nested element after a text is not our concern
// here, as the caller expects the end element afterwards.
if (reader.NodeType == Xml.XmlNodeType.Element)
{
error = new Reporting.Error(
$"Expected an XML content representing {typeof(T).Name}, " +
$"but got a nested element {reader.Name}");
return default!;
}

try
{
return readContent(reader);
Expand Down
Loading
Loading