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

Fix issues handling golang packages with multi-part namespaces. #20

Merged
merged 2 commits into from
Mar 16, 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
10 changes: 8 additions & 2 deletions src/PackageUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ namespace PackageUrl
[Serializable]
public sealed class PackageURL
{
/// <summary>
/// The url encoding of /.
/// </summary>
private const string EncodedSlash = "%2F";

private static readonly Regex s_typePattern = new Regex("^[a-zA-Z][a-zA-Z0-9.+-]+$", RegexOptions.Compiled);

/// <summary>
Expand Down Expand Up @@ -135,7 +140,8 @@ public override string ToString()
purl.Append('/');
if (Namespace != null)
{
purl.Append(WebUtility.UrlEncode(Namespace));
string encodedNamespace = WebUtility.UrlEncode(Namespace).Replace(EncodedSlash, "/");
purl.Append(encodedNamespace);
purl.Append('/');
}
if (Name != null)
Expand Down Expand Up @@ -238,7 +244,7 @@ private void Parse(string purl)
int i;
for (i = 1; i < firstPartArray.Length - 2; ++i)
{
@namespace += firstPartArray[i] + ',';
@namespace += firstPartArray[i] + '/';
}
@namespace += firstPartArray[i];

Expand Down
12 changes: 12 additions & 0 deletions tests/TestAssets/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
"subpath": "googleapis/api/annotations",
"is_invalid": false
},
{
"description": "valid go purl with version, subpath, and multi-part namespace",
"purl": "pkg:GOLANG/github.com/gorilla/context@234fd47e07d1004f0aed9c#api/",
"canonical_purl": "pkg:golang/github.com/gorilla/context@234fd47e07d1004f0aed9c#api",
"type": "golang",
"namespace": "github.com/gorilla",
"name": "context",
"version": "234fd47e07d1004f0aed9c",
"qualifiers": null,
"subpath": "api",
"is_invalid": false
},
{
"description": "bitbucket namespace and name should be lowercased",
"purl": "pkg:bitbucket/birKenfeld/pyGments-main@244fd47e07d1014f0aed9c",
Expand Down