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

Reuse top-level prefix for all extension base types with explicit XML type declarations #1072

Merged
merged 8 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions Bonsai.Core.Tests/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Bonsai.Core.Tests
{
internal static class Constants
{
public const string XmlNamespace = "clr-namespace:Bonsai.Core.Tests;assembly=Bonsai.Core.Tests";
}
}
10 changes: 5 additions & 5 deletions Bonsai.Core.Tests/TypeMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ public void Serialize_Nameclash_DifferentNamespaces()
{
var workflow = new WorkflowBuilder();
var builder = new StringBuilder();
workflow.Workflow.Add(new CombinatorBuilder() { Combinator = new MappingCombinator() });
workflow.Workflow.Add(new CombinatorBuilder() { Combinator = new GenericMappingCombinator<int>() });
workflow.Workflow.Add(new CombinatorBuilder { Combinator = new MappingCombinator() });
workflow.Workflow.Add(new CombinatorBuilder { Combinator = new GenericMappingCombinator<int>() });
workflow.Workflow.Add(new AddBuilder { Operand = new WorkflowProperty<int>() });
#pragma warning disable CS0612 // Type or member is obsolete
workflow.Workflow.Add(new ExternalizedTimeSpan<int>());
#pragma warning restore CS0612 // Type or member is obsolete
workflow.Workflow.Add(new PropertySource<Bonsai.Reactive.WindowCount, int>());
workflow.Workflow.Add(new PropertySource<Reactive.WindowCount, int>());
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<Tuple<int, int>>() });
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<Tuple<Tuple<int, int, int>>>() });
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<Tuple<Tuple<int, int>, int>>() });
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<List<Bonsai.Core.Tests.MappingNamespace1.Vector2>>() });
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<List<Bonsai.Core.Tests.MappingNamespace2.Vector2>>() });
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<List<MappingNamespace1.Vector2>>() });
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<List<MappingNamespace2.Vector2>>() });
workflow.Workflow.Add(new InputMappingBuilder { TypeMapping = new TypeMapping<System.Diagnostics.Stopwatch>() });
using (var writer = XmlWriter.Create(builder, new XmlWriterSettings { Indent = true }))
{
Expand Down
69 changes: 69 additions & 0 deletions Bonsai.Core.Tests/WorkflowBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using Bonsai.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bonsai.Core.Tests
{
[TestClass]
public class WorkflowBuilderTests
{
[TestMethod]
public void Serialize_MultipleDerivedXmlTypes_UniqueBaseXmlTypeDeclaration()
{
var builder = new StringBuilder();
var workflow = new WorkflowBuilder();
var derivedClass = new DerivedNamespace.DerivedClassWithProperty();
derivedClass.BaseProperty = 10;
workflow.Workflow.Add(new CombinatorBuilder { Combinator = derivedClass });
workflow.Workflow.Add(new CombinatorBuilder { Combinator = new DerivedXmlTypeWithProperty() });

using (var writer = XmlWriter.Create(builder, new XmlWriterSettings { Indent = true }))
{
WorkflowBuilder.Serializer.Serialize(writer, workflow);
}

var xml = builder.ToString();
var baseNamespaceDeclarations = Regex.Matches(xml, Regex.Escape(BaseNamespace.BaseClassWithProperty.XmlNamespace));
Assert.AreEqual(1, baseNamespaceDeclarations.Count);
}
}

namespace BaseNamespace
{
[XmlType(Namespace = XmlNamespace)]
public class BaseClassWithProperty : Combinator
{
internal const string XmlNamespace = "clr-namespace:Bonsai.Core.Tests.BaseNamespace;assembly=Bonsai.Core.Tests";

public int BaseProperty { get; set; }

public override IObservable<TSource> Process<TSource>(IObservable<TSource> source)
{
throw new NotImplementedException();
}
}
}

namespace DerivedNamespace
{
public class DerivedClassWithProperty : IntermediateTypeWithProperty
{
public int NewProperty { get; set; }
}
}

[XmlType(Namespace = Constants.XmlNamespace)]
public class DerivedXmlTypeWithProperty : BaseNamespace.BaseClassWithProperty
{
public int NewProperty { get; set; }
}

public class IntermediateTypeWithProperty : BaseNamespace.BaseClassWithProperty
{
public int IntermediateProperty { get; set; }
}
}
2 changes: 1 addition & 1 deletion Bonsai.Core/Bonsai.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageTags>Bonsai Rx Reactive Extensions</PackageTags>
<TargetFrameworks>net462;netstandard2.0;net6.0</TargetFrameworks>
<RootNamespace>Bonsai</RootNamespace>
<VersionPrefix>2.7.1</VersionPrefix>
<VersionPrefix>2.7.2</VersionPrefix>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="Rx-Linq" Version="2.2.5" />
Expand Down
37 changes: 23 additions & 14 deletions Bonsai.Core/WorkflowBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ static IEnumerable<Type> GetDefaultSerializerTypes()
{
var builderType = typeof(ExpressionBuilder);
return builderType.Assembly.GetTypes().Where(type =>
!type.IsGenericType && !type.IsAbstract &&
type.Namespace == builderType.Namespace &&
Attribute.IsDefined(type, typeof(XmlTypeAttribute), false) &&
!Attribute.IsDefined(type, typeof(ObsoleteAttribute), false));
!type.IsGenericType &&
(type.Namespace == builderType.Namespace || type.Namespace == nameof(Bonsai)) &&
Attribute.IsDefined(type, typeof(XmlTypeAttribute), inherit: false) &&
!Attribute.IsDefined(type, typeof(ObsoleteAttribute), inherit: false));
}

static IEnumerable<Type> GetSerializerLegacyTypes()
Expand All @@ -253,9 +253,9 @@ static IEnumerable<Type> GetSerializerLegacyTypes()
return builderType.Assembly.GetTypes().Where(type =>
!type.IsGenericType && !type.IsAbstract &&
type.Namespace == builderType.Namespace &&
Attribute.IsDefined(type, typeof(XmlTypeAttribute), false) &&
Attribute.IsDefined(type, typeof(ObsoleteAttribute), false) &&
Attribute.IsDefined(type, typeof(ProxyTypeAttribute), false))
Attribute.IsDefined(type, typeof(XmlTypeAttribute), inherit: false) &&
Attribute.IsDefined(type, typeof(ObsoleteAttribute), inherit: false) &&
Attribute.IsDefined(type, typeof(ProxyTypeAttribute), inherit: false))
#pragma warning disable CS0612 // Type or member is obsolete
.Concat(new[]
{
Expand Down Expand Up @@ -339,8 +339,8 @@ static XmlSerializer GetXmlSerializerLegacy(HashSet<Type> serializerTypes)
var overrides = new XmlAttributeOverrides();
foreach (var type in serializerTypes)
{
var obsolete = Attribute.IsDefined(type, typeof(ObsoleteAttribute), false);
var xmlTypeDefined = Attribute.IsDefined(type, typeof(XmlTypeAttribute), false);
var obsolete = Attribute.IsDefined(type, typeof(ObsoleteAttribute), inherit: false);
var xmlTypeDefined = Attribute.IsDefined(type, typeof(XmlTypeAttribute), inherit: false);
if (xmlTypeDefined && !obsolete) continue;

var attributes = new XmlAttributes();
Expand Down Expand Up @@ -378,7 +378,7 @@ static XmlSerializer GetXmlSerializer(HashSet<Type> types, out Dictionary<string
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
foreach (var type in serializerTypes)
{
var xmlTypeDefined = Attribute.IsDefined(type, typeof(XmlTypeAttribute), false);
var xmlTypeDefined = Attribute.IsDefined(type, typeof(XmlTypeAttribute), inherit: false);
var attributes = new XmlAttributes();
attributes.XmlType = xmlTypeDefined
? (XmlTypeAttribute)Attribute.GetCustomAttribute(type, typeof(XmlTypeAttribute))
Expand Down Expand Up @@ -452,6 +452,15 @@ static void AddExtensionType(HashSet<Type> types, Type type)
{
types.Add(xmlInclude[i].Type);
}

while (type.BaseType != null && !SerializerExtraTypes.Contains(type.BaseType))
{
type = type.BaseType;
if (Attribute.IsDefined(type, typeof(XmlTypeAttribute), inherit: false))
{
AddExtensionType(types, type);
}
}
}
}

Expand Down Expand Up @@ -640,9 +649,9 @@ static Dictionary<string, Type> GetDefaultXmlTypeForwarding()
var assemblyName = builderType.Assembly.GetName().Name;
foreach (var type in builderType.Assembly.GetTypes().Where(type =>
type.IsGenericType && !type.IsAbstract &&
Attribute.IsDefined(type, typeof(XmlTypeAttribute), false) &&
(!Attribute.IsDefined(type, typeof(ObsoleteAttribute), false) ||
Attribute.IsDefined(type, typeof(ProxyTypeAttribute), false))))
Attribute.IsDefined(type, typeof(XmlTypeAttribute), inherit: false) &&
(!Attribute.IsDefined(type, typeof(ObsoleteAttribute), inherit: false) ||
Attribute.IsDefined(type, typeof(ProxyTypeAttribute), inherit: false))))
{
var xmlTypeAttribute = (XmlTypeAttribute)Attribute.GetCustomAttribute(type, typeof(XmlTypeAttribute));
if (string.IsNullOrEmpty(xmlTypeAttribute.TypeName)) continue;
Expand All @@ -659,7 +668,7 @@ static Dictionary<Type, Type> GetDefaultXmlProxyTypes()
var typeMap = new Dictionary<Type, Type>();
var assemblyName = builderType.Assembly.GetName().Name;
foreach (var type in builderType.Assembly.GetTypes().Where(type =>
Attribute.IsDefined(type, typeof(ProxyTypeAttribute), false)))
Attribute.IsDefined(type, typeof(ProxyTypeAttribute), inherit: false)))
{
var proxyTypeAttribute = (ProxyTypeAttribute)Attribute.GetCustomAttribute(type, typeof(ProxyTypeAttribute));
if (proxyTypeAttribute.Destination == null) continue;
Expand Down