Microsoft.XmlSerializer.Generator hangs

Open
#98,447 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start by building the simple library project with Microsoft.XmlSerializer.Generator enabled, using the provided MeasureListType reproduction and its XML text surrogate. Compare behavior with the generator disabled and vary the number of surrogate properties or IXmlSerializable implementations. Done means compilation no longer hangs and the generated serializer does not throw at runtime when the concerned properties are absent from the raw XML.

Written by the indexing model from the issue text.

Description

area-Serialization
Description

First of all, the classes generated for https://aixm.aero/document/aixm-51-xml-schema-xsd are not fully supported. For example, array of doubles in XML text content, e.g. <doubleList>123.45 678.9 12.3 4.5</doubleList>.

[XmlText]
public double[] Text { get; set; }

The above throws an InvalidOperationException at runtime. So I use a surrogate as follow.

 [XmlText]
+[JsonIgnore]
+public string TextAsString {
+  get => string.Join(' ', Text.Select(d => d.ToString()));
+  set => Text = value.Split(' ').Select(double.Parse).ToArray();
+}
+[XmlIgnore]
 public double[] Text { get; set; }

(It works without using the source generator.)
Then, Microsoft.XmlSerializer.Generator hangs at compile-time when I apply this workaround to more than 2 such properties. It also hangs if I implement IXmlSerializable on the classes instead of applying the surrogates. The CPU fan keeps spinning, so it is not a dead lock, but potential infinite loop?
Ideally, it should support deserializing numeric arrays encoded in XML text content.

Reproduction Steps
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.opengis.net/gml/3.2")]
[System.Xml.Serialization.XmlRootAttribute("valueList", Namespace="http://www.opengis.net/gml/3.2", IsNullable=false)]
public partial class MeasureListType {
        
    private string uomField;
        
    private double[] textField;
        
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string uom {
        get {
            return this.uomField;
        }
        set {
            this.uomField = value;
        }
    }
        
    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    [JsonIgnore]
    public string TextAsString
    {
        get => string.Join(' ', Text);
        set => Text = value.Split(' ').Select(s => double.Parse(s)).ToArray();
    }

    [XmlIgnore]
    public double[] Text {
        get {
            return this.textField;
        }
        set {
            this.textField = value;
        }
    }
}

Build a simple library project with the tool enabled.

Expected behavior

It should generate a serializer that at least won't throw during run-time when the properties concerned do not appear in the raw XML.

Actual behavior

The source generator hangs during compile-time.

Regression?

No response

Known Workarounds

Disable the XML serializer generator tool.

Configuration
  • .NET 8
  • Windows 11 22631.3085
  • x64
  • not specific to the configuration
  • not specific to any browser
Other information

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
Avg merge
2d 19h
Merged PRs (30d)
589

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.