Skip to content

Support attribute restrictions (such as fixed/default values) on complex types derived by restriction #103

Description

@mamift

Fixed Value Handling in LinqToXsd

LinqToXsd does have special code generation logic for fixed values on attributes and elements, but there is a nuance in how it interacts with complex type derivation by restriction (as seen in metalex_mcontainerTypeBug.xsd).


1. General Handling of Fixed Values for Attributes

When an attribute has fixed="value" in an XSD schema:

  1. Extraction (XsdToTypesConverter.cs):

    • In SetFixedDefaultValue(XmlSchemaAttribute, ClrPropertyInfo), attribute.FixedValue is read and stored on ClrPropertyInfo.FixedValue.
  2. Constant Field Emission (ClrPropertyInfo.CreateFixedDefaultValue):

    • LinqToXsd generates a static [DebuggerBrowsable(Never)] field holding the fixed value (e.g., <propertyName>FixedValue).
  3. Getter Behavior (ClrPropertyInfo.AddGetStatements / ReturnFixedValue):

    • For attributes with a fixed value, the getter skips reading from the underlying XML attribute and directly returns the fixed value constant (return <propertyName>FixedValue;), adhering to XSD semantics where fixed attributes always evaluate to that fixed value.
  4. Setter Validation (ClrPropertyInfo.AddFixedValueChecking):

    • The setter generates an equality check against the fixed constant. If the assigned value does not match the fixed value, it throws a LinqToXsdFixedValueException:
      if (typeFixedValue.Equals(value)) {
          // set attribute
      } else {
          throw new LinqToXsdFixedValueException(value, typeFixedValue);
      }

2. Why It Is Not Applied in metalex_mcontainerTypeBug.xsd

In metalex_mcontainerTypeBug.xsd:

  1. urType definition:

    • urType defines type through the typeopt attribute group without a fixed value (<xsd:attribute name="type" type="e:CMtype"/>).
    • LinqToXsd generates a normal, read/write type property on the C# urType class.
  2. absMcontainerType restriction:

    • absMcontainerType derives from urType using <xsd:restriction base="e:urType"> and includes typemcontainer, which sets fixed="mcontainer" on the type attribute.
    • However, in XsdToTypesConverter.cs (lines 306–310):
      if (ct.IsDerivedByRestriction())
      {
          //Do not handle restrictions on complex content?
          return;
      }
    • LinqToXsd deliberately skips generating child particle and attribute properties for complex types derived by restriction from another complex type. Instead, the generated C# class (absMcontainerType) simply inherits from urType (public partial class absMcontainerType : global::MetalEx_mcontainerBug.urType).
    • As a result, absMcontainerType inherits the unconstrained type property from urType rather than generating a specialized or overridden type property with the fixed value logic.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions