Skip to content

Commit

Permalink
Allowed null/Empty Namespaces for Custom/Non-system Assemblies
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo authored Sep 1, 2020
1 parent ffd3a89 commit 6fe3efd
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ readonly static Parser<string>

public static AssemblyPathParser Default { get; } = new AssemblyPathParser();

AssemblyPathParser() : base(
Parse.String("clr-namespace:")
.Then(Namespace.Accept)
.SelectMany(Parse.String(";assembly=")
.Accept, (ns, _) => ns)
.SelectMany(Assembly.Accept, (ns, assembly) => new AssemblyPath(ns, assembly))
) {}
AssemblyPathParser()
: base(Parse.String("clr-namespace:")
.Then(Namespace.Optional().Accept)
.SelectMany(Parse.String(";assembly=")
.Accept, (ns, _) => ns)
.SelectMany(Assembly.Accept, (ns, assembly)
=> new AssemblyPath(ns.GetOrDefault(), assembly))) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ protected override Partition Create(Assembly parameter)
=> _types.Get(parameter)
.Where(_specification)
.ToLookup(_key)
.Where(x => !string.IsNullOrWhiteSpace(x.Key))
.ToDictionary(x => x.Key, _format)
.ToDictionary(x => x.Key ?? string.Empty, _format)
.Get;

public ImmutableArray<TypeInfo>? Get(TypePartition parameter)
=> Get(parameter.Assembly)
.Invoke(parameter.Namespace)
?.Invoke(parameter.Name);
=> Get(parameter.Assembly)(parameter.Namespace ?? string.Empty)?.Invoke(parameter.Name);

Func<string, ImmutableArray<TypeInfo>?> Format(IGrouping<string, TypeInfo> grouping)
=> grouping.ToLookup(_formatter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public Classification(IFormattedContentSpecification specification, IIdentitySto
}

public TypeInfo Get(IFormatReader parameter)
=> FromAttributes(parameter) ??
(!parameter.IsSatisfiedBy(MemberIdentity.Default)
? _types.Get(_identities.Get(parameter.Name, parameter.Identifier))
: null);
=> FromAttributes(parameter) ?? (!parameter.IsSatisfiedBy(MemberIdentity.Default)
? _types.Get(_identities.Get(parameter.Name, parameter.Identifier))
: null);

TypeInfo FromAttributes(IFormatReader parameter)
=> _specification.IsSatisfiedBy(parameter)
Expand Down
3 changes: 1 addition & 2 deletions src/ExtendedXmlSerializer/ContentModel/Reflection/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public Types(IPartitionedTypeSpecification specification, IAssemblyTypePartition
}

protected override TypeInfo Create(IIdentity parameter)
=> _aliased.Get(parameter) ?? _candidates.Get(parameter)
.SingleOrDefault();
=> _aliased.Get(parameter) ?? _candidates.Get(parameter).SingleOrDefault();
}
}
24 changes: 24 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue431Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.Tests.ReportedIssues.Support;
using FluentAssertions;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue431Tests
{
[Fact]
public void Verify()
{
typeof(HelloWorld).Namespace.Should().BeNull();
var serializer = new ConfigurationContainer().Create().ForTesting();
var instance = new HelloWorld { Message = "Hello World!" };
serializer.Cycle(instance).Should().BeEquivalentTo(instance);
}
}
}

sealed class HelloWorld
{
public string Message { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using ExtendedXmlSerializer.ContentModel.Properties;
using ExtendedXmlSerializer.ContentModel.Reflection;
using ExtendedXmlSerializer.Core.Parsing;
using Sprache;
using FluentAssertions;
using Sprache;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -113,6 +113,16 @@ public void AssemblyPathParser()
Assert.Equal("ExtendedXmlSerializer.Tests", path.Path);
}

[Fact]
public void AssemblyPathParserWithNullNamespace()
{
const string input = "clr-namespace:;assembly=ExtendedXmlSerializer.Tests";
var path = ExtendedXmlSerializer.ContentModel.Reflection.AssemblyPathParser.Default.ToParser()
.Parse(input);
path.Namespace.Should().BeNull();
path.Path.Should().Be("ExtendedXmlSerializer.Tests");
}

[Fact]
public void GenericName()
{
Expand Down

0 comments on commit 6fe3efd

Please sign in to comment.