Skip to content

Fix Npm version parsing for version with build numbers.#528

Closed
morended wants to merge 2 commits intomainfrom
morended/Npm_version_parsing
Closed

Fix Npm version parsing for version with build numbers.#528
morended wants to merge 2 commits intomainfrom
morended/Npm_version_parsing

Conversation

@morended
Copy link
Contributor

Fix parsing for Npm packages with non-standard version numbers.
Retrieve the Npm package latest version from "dist-tag", or fallback to the most recent by time.
Example @adguard/dnr-rulesets@4.0.20260218200111

@michelleqyun
Copy link
Contributor

Could there be scenario where the build number contains letters or dashes (e.g. 2026-03-05-abc)?

public virtual JsonElement? GetVersionElement(JsonDocument contentJSON, string version)
{
string typeName = GetType().Name;
throw new NotImplementedException($"{typeName} does not implement GetVersions.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be "does not implement GetVersionElement".

}

/// <inheritdoc />
public virtual JsonElement? GetVersionElement(JsonDocument contentJSON, string version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method defined twice?

Copy link
Contributor Author

@morended morended Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the new method accepts string versions; I kept the original one for Version types for backwards compatibility

}

[Fact]
public void GetLatestVersionElement_WithNullJsonDocument_ReturnsNull()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add another test for testing getting the latest version from the dist tag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"GetLatestVersionElement_WithValidJsonDocument_ReturnsLatestVersionElement" is to test that scenario

JsonElement? versionElement = _projectManager.Object.GetVersionElement(contentJSON, "4.0.20260218200111");

Assert.NotNull(versionElement);
Assert.Equal("4.0.20260218200111", versionElement?.GetProperty("version").GetString());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.0.20260218200111 is the version number? Do we also want to test the package name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. [InlineData("pkg:npm/%40adguard/dnr-rulesets", "4.0.20260218200111")] This is the package in test

PackageURL purl = new("pkg:npm/%40adguard/dnr-rulesets");
string? content = await _projectManager.Object.GetMetadataAsync(purl, useCache: false);
JsonDocument contentJSON = JsonDocument.Parse(content);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Additional line space is not needed here.

@morended
Copy link
Contributor Author

morended commented Mar 5, 2026

Could there be scenario where the build number contains letters or dashes (e.g. 2026-03-05-abc)?

@morended morended closed this Mar 5, 2026
@morended
Copy link
Contributor Author

morended commented Mar 5, 2026

Could there be scenario where the build number contains letters or dashes (e.g. 2026-03-05-abc)?

I am not sure. Now that the we are accepting version as a string. The current logic should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants