Skip to content

Commit

Permalink
WSDL for XmlSerializer: Fix invalid ArrayOfString schema
Browse files Browse the repository at this point in the history
Removed the special if-condition for "string", which does not make sense,
because it does not correspond to the actual XML that is generated
by the XmlSerializer. Afterwards, the fields _arrayToBuild and
_buildArrayTypes are not needed anymore, so I have removed them.

The test CheckStringArrayNameWsdl() can only be performed for the
DataContractSerializer now, because the complexType does not have an
element inside it anymore.

Fixes #996
  • Loading branch information
vidrenning committed Feb 1, 2024
1 parent 0f29018 commit ef223ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 85 deletions.
1 change: 0 additions & 1 deletion src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ public void CheckIActionResultInterfaceDataContract()
}

[DataTestMethod]
[DataRow(SoapSerializer.XmlSerializer)]
[DataRow(SoapSerializer.DataContractSerializer)]
public async Task CheckStringArrayNameWsdl(SoapSerializer soapSerializer)
{
Expand Down
102 changes: 18 additions & 84 deletions src/SoapCore/Meta/MetaBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ public class MetaBodyWriter : BodyWriter

private readonly Queue<Type> _enumToBuild;
private readonly Queue<TypeToBuild> _complexTypeToBuild;
private readonly Queue<Type> _arrayToBuild;

private readonly HashSet<string> _builtEnumTypes;
private readonly HashSet<string> _builtComplexTypes;
private readonly HashSet<string> _buildArrayTypes;
private readonly Dictionary<string, Dictionary<string, string>> _requestedDynamicTypes;

private bool _buildDateTimeOffset;
Expand All @@ -59,10 +57,8 @@ public MetaBodyWriter(ServiceDescription service, string baseUrl, XmlNamespaceMa

_enumToBuild = new Queue<Type>();
_complexTypeToBuild = new Queue<TypeToBuild>();
_arrayToBuild = new Queue<Type>();
_builtEnumTypes = new HashSet<string>();
_builtComplexTypes = new HashSet<string>();
_buildArrayTypes = new HashSet<string>();
_requestedDynamicTypes = new Dictionary<string, Dictionary<string, string>>();

BindingName = bindingName;
Expand Down Expand Up @@ -302,10 +298,6 @@ private void AddTypes(XmlDictionaryWriter writer)
writer.WriteAttributeString("elementFormDefault", "qualified");
writer.WriteAttributeString("targetNamespace", TargetNameSpace);

writer.WriteStartElement("import", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("namespace", Namespaces.ARRAYS_NS);
writer.WriteEndElement();

writer.WriteStartElement("import", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("namespace", Namespaces.SYSTEM_NS);
writer.WriteEndElement();
Expand Down Expand Up @@ -513,39 +505,6 @@ join m in toBuild.GetMembers() on n equals m.Name

writer.WriteEndElement(); // schema

while (_arrayToBuild.Count > 0)
{
var toBuild = _arrayToBuild.Dequeue();
var toBuildName = toBuild.GetSerializedTypeName();

if (!_buildArrayTypes.Contains(toBuildName))
{
writer.WriteStartElement("schema", Namespaces.XMLNS_XSD);
writer.WriteXmlnsAttribute("tns", Namespaces.ARRAYS_NS);
writer.WriteAttributeString("elementFormDefault", "qualified");
writer.WriteAttributeString("targetNamespace", Namespaces.ARRAYS_NS);

writer.WriteStartElement("complexType", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("name", toBuildName);

writer.WriteStartElement("sequence", Namespaces.XMLNS_XSD);
AddSchemaType(writer, toBuild.GetGenericType(), null, true);
writer.WriteEndElement(); // sequence

writer.WriteEndElement(); // complexType

writer.WriteStartElement("element", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("name", toBuildName);
writer.WriteAttributeString("nillable", "true");
writer.WriteAttributeString("type", "tns:" + toBuildName);
writer.WriteEndElement(); // element

writer.WriteEndElement(); // schema

_buildArrayTypes.Add(toBuildName);
}
}

if (_buildDateTimeOffset)
{
writer.WriteStartElement("schema", Namespaces.XMLNS_XSD);
Expand Down Expand Up @@ -1214,58 +1173,33 @@ private void AddSchemaType(XmlDictionaryWriter writer, TypeToBuild toBuild, stri
}
else if (typeof(IEnumerable).IsAssignableFrom(type))
{
if (type.GetGenericType().Name == "String")
if (string.IsNullOrEmpty(name))
{
if (string.IsNullOrEmpty(name))
{
name = typeName;
}

var ns = $"q{_namespaceCounter++}";
name = typeName;
}

writer.WriteXmlnsAttribute(ns, Namespaces.ARRAYS_NS);
writer.WriteAttributeString("name", name);
WriteQualification(writer, isUnqualified);
writer.WriteAttributeString("name", name);
WriteQualification(writer, isUnqualified);

if (!isArray)
{
writer.WriteAttributeString("nillable", "true");
}
if (!isArray)
{
writer.WriteAttributeString("nillable", "true");
}

writer.WriteAttributeString("type", $"{ns}:{newTypeToBuild.TypeName}");
if (isListWithoutWrapper)
{
newTypeToBuild = new TypeToBuild(newTypeToBuild.Type.GetGenericType());
}

_arrayToBuild.Enqueue(type);
if (newTypeToBuild.IsAnonumous)
{
AddSchemaComplexType(writer, newTypeToBuild);
}
else
{
if (string.IsNullOrEmpty(name))
{
name = typeName;
}

writer.WriteAttributeString("name", name);
WriteQualification(writer, isUnqualified);

if (!isArray)
{
writer.WriteAttributeString("nillable", "true");
}

if (isListWithoutWrapper)
{
newTypeToBuild = new TypeToBuild(newTypeToBuild.Type.GetGenericType());
}
writer.WriteAttributeString("type", "tns:" + newTypeToBuild.TypeName);

if (newTypeToBuild.IsAnonumous)
{
AddSchemaComplexType(writer, newTypeToBuild);
}
else
{
writer.WriteAttributeString("type", "tns:" + newTypeToBuild.TypeName);

_complexTypeToBuild.Enqueue(newTypeToBuild);
}
_complexTypeToBuild.Enqueue(newTypeToBuild);
}
}
else if (toBuild.IsAnonumous)
Expand Down

0 comments on commit ef223ff

Please sign in to comment.