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

Ensure that namespaces are always null or non-empty #602

Merged
merged 1 commit into from
Dec 15, 2024
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
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);
}
}
}
Loading