Skip to content

Bug in XsdDataContractImporter when importing ComplexType #98240

Closed

Description

@zabulus

Description

We are currently in process of porting from .net4.8 to net7.0. Utility is used to customize generation of data contracts from .xsd files. Particular complex type in xsd is a dictionary of enum-key bool-value records.
Error message we encounter:

System.Runtime.Serialization.InvalidDataContractException: Array type 'ArrayOfKeyValueOfDayOfWeekbooleanRDHGY3MA' in namespace 'http://schemas.microsoft.com/2003/10/Serialization/Arrays' cannot be imported. It is an invalid dictionary type since element 'KeyValueOfDayOfWeekbooleanRDHGY3MA' references a type from a different namespace 'http://schemas.datacontract.org/2004/07/System.Runtime.Serialization'. Either fix the schema or remove the IsDictionary annotation. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer.
   at System.Runtime.Serialization.SchemaImporter.ThrowTypeCannotBeImportedException(String message)
   at System.Runtime.Serialization.SchemaImporter.ImportCollection(XmlQualifiedName typeName, XmlSchemaSequence rootSequence, XmlSchemaObjectCollection attributes, XmlSchemaAnnotation annotation, Boolean isReference)
   at System.Runtime.Serialization.SchemaImporter.ImportType(XmlQualifiedName typeName, XmlSchemaParticle rootParticle, XmlSchemaObjectCollection attributes, XmlSchemaAnyAttribute anyAttribute, XmlQualifiedName baseTypeName, XmlSchemaAnnotation annotation)
   at System.Runtime.Serialization.SchemaImporter.ImportType(XmlSchemaType type, XmlQualifiedName typeName, Boolean isAnonymous)

Error message is somewhat misleading, but gives a clue that an exception thrown from this line:

ThrowArrayTypeCannotBeImportedException(typeName.Name, typeName.Namespace, SR.Format(SR.InvalidKeyValueTypeNamespace, element.Name, keyValueContract.Namespace));

Basically it looks like that namespaces are mismatched, but our xsd doesn't have any reference of 'http://schemas.datacontract.org/2004/07/System.Runtime.Serialization'.
during research and comparison of work of net4.8 execution path, I've figured out that problem is mismatch of namespaces resulting in following two lines:
new CollectionDataContract(Globals.TypeOfSchemaDefinedType, CollectionKind.Array) used in ImportCollection method gives contract namespace 'null', as in net4.8.
but
new ClassDataContract(Globals.TypeOfSchemaDefinedType) used in ImportClass method gives namespace 'http://schemas.datacontract.org/2004/07/System.Runtime.Serialization' mentioned in exception message and differs from net4.8 behavior.

Reproduction Steps

xsd:

            <xs:complexType name="ArrayOfKeyValueOfDayOfWeekbooleanRDHGY3MA">
                <xs:annotation>
                    <xs:appinfo>
                        <IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary>
                    </xs:appinfo>
                </xs:annotation>
                <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="KeyValueOfDayOfWeekbooleanRDHGY3MA">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/System" name="Key"
                                            type="q1:DayOfWeek"/>
                                <xs:element name="Value" type="xs:boolean"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>

C#

                XsdDataContractImporter importer = new XsdDataContractImporter();
                importer.Options = new ImportOptions();
                importer.Options.ImportXmlType = true;
                importer.Options.CodeProvider = codeProvider;
                importer.Options.ReferencedCollectionTypes.Add(typeof(List<>));
                importer.Options.ReferencedTypes.Add(typeof(Tuple<,>));
                importer.Import(xsds);

If you need copy-paste-and-run sample, pls give me a notice, as it is a part of 1mb complex xsd file, and narrow down only this complex type would take some effort.

Expected behavior

No exception occurs

Actual behavior

Exception occurs

System.Runtime.Serialization.InvalidDataContractException: Array type 'ArrayOfKeyValueOfDayOfWeekbooleanRDHGY3MA' in namespace 'http://schemas.microsoft.com/2003/10/Serialization/Arrays' cannot be imported. It is an invalid dictionary type since element 'KeyValueOfDayOfWeekbooleanRDHGY3MA' references a type from a different namespace 'http://schemas.datacontract.org/2004/07/System.Runtime.Serialization'. Either fix the schema or remove the IsDictionary annotation. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer.
   at System.Runtime.Serialization.SchemaImporter.ThrowTypeCannotBeImportedException(String message)
   at System.Runtime.Serialization.SchemaImporter.ImportCollection(XmlQualifiedName typeName, XmlSchemaSequence rootSequence, XmlSchemaObjectCollection attributes, XmlSchemaAnnotation annotation, Boolean isReference)
   at System.Runtime.Serialization.SchemaImporter.ImportType(XmlQualifiedName typeName, XmlSchemaParticle rootParticle, XmlSchemaObjectCollection attributes, XmlSchemaAnyAttribute anyAttribute, XmlQualifiedName baseTypeName, XmlSchemaAnnotation annotation)
   at System.Runtime.Serialization.SchemaImporter.ImportType(XmlSchemaType type, XmlQualifiedName typeName, Boolean isAnonymous)

Regression?

No response

Known Workarounds

No response

Configuration

.net7.0 running on windows

    <PackageReference Include="System.Web.Services.Description" Version="8.0.0" />
    <PackageReference Include="System.Runtime.Serialization.Schema" Version="8.0.0" />

Other information

No response

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Feb 9, 2024
  2. removed
    untriagedNew issue has not been triaged by the area owner
    on Feb 28, 2024
  3. added this to the 9.0.0 milestone on Feb 28, 2024
  4. modified the milestones: 9.0.0, 10.0.0 on Dec 9, 2024
  5. modified the milestones: 10.0.0, 11.0.0 on Sep 22, 2025
  6. added
    in-prThere is an active PR which will close this issue when it is merged
    on Apr 3, 2026
  7. added a commit that references this issue on Jun 2, 2026
    91749c1
  8. locked and limited conversation to collaborators on Jul 3, 2026
  9. added a commit that references this issue on Jul 15, 2026
    168231c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-Serializationin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions