Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for serializing extension types #209

Merged
merged 1 commit into from
Mar 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/SoapCore/MetaWCFBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,16 @@ private void DiscoveryTypesByProperties(Type type, bool isRootType)
return;
}

foreach (var property in type.GetProperties().Where(prop => prop.CustomAttributes.All(attr => attr.AttributeType.Name != "IgnoreDataMemberAttribute") && !prop.PropertyType.IsPrimitive && !SysTypeDic.ContainsKey(prop.PropertyType.FullName)))
if (HasBaseType(type))
{
DiscoveryTypesByProperties(type.BaseType, false);
_complexTypeToBuild.Enqueue(type.BaseType);
}

foreach (var property in type.GetProperties().Where(prop =>
prop.DeclaringType == type
&& prop.CustomAttributes.All(attr => attr.AttributeType.Name != "IgnoreDataMemberAttribute")
&& !prop.PropertyType.IsPrimitive && !SysTypeDic.ContainsKey(prop.PropertyType.FullName)))
{
Type propertyType;
var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
Expand Down Expand Up @@ -517,6 +526,17 @@ private void WriteComplexType(XmlDictionaryWriter writer, Type type)
writer.WriteStartElement("xs:complexType");
writer.WriteAttributeString("name", toBuildName);
writer.WriteAttributeString("xmlns:ser", SERIALIZATION_NS);

var hasBaseType = HasBaseType(type);

if (hasBaseType)
{
writer.WriteStartElement("xs:complexContent");

writer.WriteStartElement("xs:extension");
writer.WriteAttributeString("base", $"tns:{type.BaseType.Name}");
}

writer.WriteStartElement("xs:sequence");

if (type.IsArray || typeof(IEnumerable).IsAssignableFrom(type))
Expand All @@ -527,6 +547,7 @@ private void WriteComplexType(XmlDictionaryWriter writer, Type type)
else
{
var properties = type.GetProperties().Where(prop =>
prop.DeclaringType == type &&
prop.CustomAttributes.All(attr => attr.AttributeType.Name != "IgnoreDataMemberAttribute"));

//TODO: base type properties
Expand All @@ -537,9 +558,16 @@ private void WriteComplexType(XmlDictionaryWriter writer, Type type)
}
}

writer.WriteEndElement(); // xs:complexType
writer.WriteEndElement(); // xs:sequence

if (hasBaseType)
{
writer.WriteEndElement(); // xs:extension
writer.WriteEndElement(); // xs:complexContent
}

writer.WriteEndElement(); // xs:complexType

_builtComplexTypes.Add(toBuildName);
}
}
Expand Down Expand Up @@ -855,5 +883,14 @@ private Type GetGenericType(Type collectionType)

return baseType.GetTypeInfo().GetGenericArguments().DefaultIfEmpty(typeof(object)).FirstOrDefault();
}

private bool HasBaseType(Type type)
{
var isArrayType = type.IsArray || typeof(IEnumerable).IsAssignableFrom(type);

var baseType = type.GetTypeInfo().BaseType;

return !isArrayType && !type.IsEnum && !type.IsPrimitive && !baseType.Name.Equals("Object");
}
}
}