Skip to content

Report nested elements in C# XML text - #844

Merged
mristin merged 1 commit into
mainfrom
mristin/Report-nested-element-in-CSharp-XML-text
Oct 11, 2026
Merged

mristin merged 1 commit into
mainfrom
mristin/Report-nested-element-in-CSharp-XML-text

Conversation

@mristin

@mristin mristin commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

The C# de-serialization threw an InvalidOperationException instead of reporting an error when the content of a primitive started with an element, e.g., <someString><nested/></someString>. XmlReader.ReadContentAs* throws it at an element node, and our catch filters did not cover it. We now check for an element before the conversion in AsText, which all the primitives go through:

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!;
}

The XML-RPC readers of <boolean>, <double> and <string> had the same bug, and get the same check. Moreover, the key of a struct member was coalesced with ?? throw before the error was checked, so any error in a <name> surfaced as an InvalidOperationException. We now check the error first.

We add the live test data some_string_as_nested_element.xml for all the targets, and a C# XML-RPC test with a nested element in <boolean>, <double>, <string> and <name>.

The C# de-serialization threw an ``InvalidOperationException`` instead
of reporting an error when the content of a primitive started with
an element, *e.g.*, ``<someString><nested/></someString>``.
``XmlReader.ReadContentAs*`` throws it at an element node, and our
``catch`` filters did not cover it. We now check for an element before
the conversion in ``AsText``, which all the primitives go through:

    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!;
    }

The XML-RPC readers of ``<boolean>``, ``<double>`` and ``<string>`` had
the same bug, and get the same check. Moreover, the key of a struct
member was coalesced with ``?? throw`` before the error was checked, so
any error in a ``<name>`` surfaced as an ``InvalidOperationException``.
We now check the error first.

We add the live test data ``some_string_as_nested_element.xml`` for
all the targets, and a C# XML-RPC test with a nested element in
``<boolean>``, ``<double>``, ``<string>`` and ``<name>``.
@mristin
mristin merged commit f9d82d5 into main Oct 11, 2026
5 checks passed
@mristin
mristin deleted the mristin/Report-nested-element-in-CSharp-XML-text branch October 11, 2026 23:55
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 38185248312

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage remained the same at 90.141%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 50217
Covered Lines: 45266
Line Coverage: 90.14%
Coverage Strength: 2.7 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants