Skip to content

Commit

Permalink
Merge pull request #602 from ds5678/namespace-never-empty
Browse files Browse the repository at this point in the history
Ensure that namespaces are always null or non-empty
  • Loading branch information
Washi1337 authored Dec 15, 2024
2 parents e1f3a9c + 1c367c4 commit 56a7e1e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/AsmResolver.DotNet/TypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public TypeDefinition(Utf8String? ns, Utf8String? name, TypeAttributes attribute
public Utf8String? Namespace
{
get => _namespace.GetValue(this);
set => _namespace.SetValue(value);
set => _namespace.SetValue(Utf8String.IsNullOrEmpty(value) ? null : value);
// According to the specification, the namespace should always be null or non-empty.
}

string? ITypeDescriptor.Namespace => Namespace;
Expand Down
3 changes: 2 additions & 1 deletion src/AsmResolver.DotNet/TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public Utf8String? Name
public Utf8String? Namespace
{
get => _namespace.GetValue(this);
set => _namespace.SetValue(value);
set => _namespace.SetValue(Utf8String.IsNullOrEmpty(value) ? null : value);
// According to the specification, the namespace should always be null or non-empty.
}

string? ITypeDescriptor.Namespace => Namespace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public void MatchTopLevelTypeRefTypeRefDifferentNamespace()
Assert.NotEqual(reference1, reference2, _comparer);
}

[Fact]
public void MatchTopLevelTypeRefTypeRefWithEmptyNamespace()
{
var reference1 = new TypeReference(_someAssemblyReference, null, "SomeType");
var reference2 = new TypeReference(_someAssemblyReference, "", "SomeType");
Assert.Equal(reference1, reference2, _comparer);
}

[Fact]
public void MatchTopLevelTypeRefTypeDef()
{
Expand Down
14 changes: 14 additions & 0 deletions test/AsmResolver.DotNet.Tests/TypeDefinitionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -773,5 +773,19 @@ public void AddSameNestedTypeToDifferentTypesShouldThrow()
Assert.Throws<ArgumentException>(() => type2.NestedTypes.Add(nestedType));
}

[Fact]
public void NamespaceShouldBeNullIfEmptyStringGiven()
{
var type = new TypeDefinition(string.Empty, "SomeType", TypeAttributes.Public);
Assert.Null(type.Namespace);
}

[Fact]
public void NamespaceShouldBeNullIfEmptyStringSet()
{
var type = new TypeDefinition("SomeNamespace", "SomeType", TypeAttributes.Public);
type.Namespace = string.Empty;
Assert.Null(type.Namespace);
}
}
}
17 changes: 17 additions & 0 deletions test/AsmResolver.DotNet.Tests/TypeReferenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,22 @@ public void NonCorLibTypeToTypeSignatureShouldReturnTypeDefOrRef()

Assert.Equal(signature.Type, reference, Comparer);
}

[Fact]
public void NamespaceShouldBeNullIfEmptyStringGiven()
{
var module = new ModuleDefinition("SomeModule");
var type = new TypeReference(module, string.Empty, "SomeType");
Assert.Null(type.Namespace);
}

[Fact]
public void NamespaceShouldBeNullIfEmptyStringSet()
{
var module = new ModuleDefinition("SomeModule");
var type = new TypeReference(module, "SomeNamespace", "SomeType");
type.Namespace = string.Empty;
Assert.Null(type.Namespace);
}
}
}

0 comments on commit 56a7e1e

Please sign in to comment.