Skip to content

Conversation

Copy link

Copilot AI commented Dec 30, 2025

PR #577 broke three tests by attempting to add XmlText properties in derived classes when the base class already has one—a constraint the C# XmlSerializer enforces strictly.

Changes

ModelBuilder.cs

  • Generate enum types for simpleContent restrictions with enumeration facets
  • Store enum type in ClassModel.TextValueType for derived classes

ClassModel.cs

  • Generate EnumValue adapter property for derived classes with simpleContent enum restrictions
  • Property is marked with [XmlIgnore] to prevent serialization conflicts
  • Provides strongly-typed access to enum values with automatic conversion to/from string
  • Document XmlSerializer limitation and adapter property pattern

XmlTests.cs

  • Update TestSimpleContentEnum to verify enum generation and EnumValue adapter property functionality
  • Test validates round-trip conversion between string Value and enum EnumValue

Generated Code

// Base class with string Value and [XmlText]
public partial class CodeType {
    [XmlText]
    public string Value { get; set; }
}

// Derived class with EnumValue adapter property
public partial class TransConfirmationCodeType : CodeType {
    [XmlIgnore]
    public TransConfirmationCodeTypeEnum? EnumValue {
        get {
            if (Enum.TryParse(typeof(TransConfirmationCodeTypeEnum), Value, true, out var result))
                return (TransConfirmationCodeTypeEnum)result;
            return null;
        }
        set {
            Value = value?.ToString();
        }
    }
}

// Enum type for strongly-typed access
public enum TransConfirmationCodeTypeEnum {
    Always, Never, OnError
}

Usage

// Set using strongly-typed enum
var instance = new TransConfirmationCodeType();
instance.EnumValue = TransConfirmationCodeTypeEnum.Always;
// Value is automatically set to "Always"

// Read as strongly-typed enum
instance.Value = "Never";
var enumValue = instance.EnumValue; // Returns TransConfirmationCodeTypeEnum.Never

// Handle invalid values gracefully
instance.Value = "Invalid";
var invalid = instance.EnumValue; // Returns null

This approach generates the requested enum types and provides convenient strongly-typed access while staying within XmlSerializer's inheritance constraints.

Original prompt

Continue work on #577 to fix #561


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@codecov
Copy link

codecov bot commented Dec 30, 2025

Codecov Report

❌ Patch coverage is 95.37037% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.36%. Comparing base (d974d57) to head (3c93a82).

Files with missing lines Patch % Lines
XmlSchemaClassGenerator/ModelBuilder.cs 87.80% 1 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #578      +/-   ##
==========================================
+ Coverage   94.33%   94.36%   +0.03%     
==========================================
  Files          32       32              
  Lines        3230     3338     +108     
  Branches      515      526      +11     
==========================================
+ Hits         3047     3150     +103     
- Misses        122      123       +1     
- Partials       61       65       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI and others added 3 commits December 30, 2025 13:20
Fixed the issue where generating enums for simpleContent complex types with restrictions broke existing tests. The C# XmlSerializer has limitations that prevent derived classes from adding new XmlText properties when the base class already has one.

Solution: Generate the enum type for simpleContent restrictions, but don't add a new Value property in the derived class. Users can manually convert between the inherited string Value and the enum type.

- Updated ClassModel.cs to not generate Value property in derived classes with simpleContent enum restrictions
- Updated test to verify enum generation without expecting a strongly-typed Value property
- All originally failing tests now pass (TestSimple, TestEmptyKeyProviderGeneratorPrefix, AirspaceServicesTest1)

Co-authored-by: mganss <[email protected]>
- Moved comment explaining enum generation to proper location outside if block
- Added null safety checks for qualifiedName in CreateSimpleContentEnumModel
- Improved code clarity and safety

Co-authored-by: mganss <[email protected]>
Copilot AI changed the title [WIP] Continue work on issue #577 to fix issue #561 Fix enum generation for simpleContent restrictions to work with XmlSerializer constraints Dec 30, 2025
Copilot AI requested a review from mganss December 30, 2025 13:40
@mganss
Copy link
Owner

mganss commented Dec 30, 2025

@copilot There should be an adapter property as suggested in #562 like this:

[System.Xml.Serialization.XmlIgnore]
public TransConfirmationCodeTypeEnum? EnumValue
{
    get
    {
        if (Enum.TryParse(typeof(TransConfirmationCodeTypeEnum), Value, true, out var result))
        {
            return (TransConfirmationCodeTypeEnum)result;
        }
        return null;
    }
    set
    {
        Value = value?.ToString(); // keep Value updated
    }
}

Added an adapter property as suggested in #562 that provides strongly-typed access to enum values while keeping the string Value property for XML serialization. The EnumValue property:
- Is marked with [XmlIgnore] to prevent serialization
- Returns nullable enum type
- Uses Enum.TryParse in getter to convert from string Value
- Updates Value in setter by calling ToString() on the enum

Example generated code:
```csharp
[XmlIgnore]
public TransConfirmationCodeTypeEnum? EnumValue {
    get {
        if (Enum.TryParse(typeof(TransConfirmationCodeTypeEnum), Value, true, out var result))
            return (TransConfirmationCodeTypeEnum)result;
        return null;
    }
    set {
        Value = value?.ToString();
    }
}
```

Co-authored-by: mganss <[email protected]>
Copy link
Author

Copilot AI commented Dec 30, 2025

@copilot There should be an adapter property as suggested in #562 like this:

[System.Xml.Serialization.XmlIgnore]

...

Added the EnumValue adapter property as suggested. The property:

  • Uses Enum.TryParse with case-insensitive parsing in the getter
  • Returns nullable enum type for safe handling of invalid values
  • Sets Value in the setter by calling ToString() on the enum
  • Handles null values correctly in the setter

Commit: 3c93a82

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.w3.org
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/XmlSchemaClassGenerator/XmlSchemaClassGenerator/XmlSchemaClassGenerator.Tests/bin/Debug/net8.0/XmlSchemaClassGenerator.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/XmlSchemaClassGenerator/XmlSchemaClassGenerator/XmlSchemaClassGenerator.Tests/bin/Debug/net8.0/XmlSchemaClassGenerator.Tests.deps.json /home/REDACTED/work/XmlSchemaClassGenerator/XmlSchemaClassGenerator/XmlSchemaClassGenerator.Tests/bin/Debug/net8.0/testhost.dll --port 34409 --endpoint 127.0.0.1:034409 --role client --parentprocessid 4749 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Fix enum generation for simpleContent restrictions to work with XmlSerializer constraints Add EnumValue adapter property for simpleContent enum restrictions Dec 30, 2025
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.

Enum generated correctly only for simple types, not generated at all for complex types

2 participants