Skip to content

Bug in XsdDataContractImporter when importing ComplexType #98240

Open
@zabulus

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions