如何通过NuGet API查询包是否unlisted/deprecated及获取相关原因参考?
Great question! The NuGet API absolutely has support for checking if a package version is unlisted or deprecated, and in the case of deprecated packages, you can pull in additional details like the reason for deprecation or recommended alternatives. Here's how it works:
1. Checking if a Package Version is Unlisted
To check if a specific package version is unlisted, you can use the NuGet Registration API (V3 endpoint). The key field to look for is listed — a boolean value where false means the version is unlisted.
For example, to fetch metadata for all versions of a package (like Newtonsoft.Json), send a GET request to:
GET https://api.nuget.org/v3/registration5-semver1/newtonsoft.json/index.json
Each version entry in the response will include the listed property. If you only care about a single version, you can target the specific version's endpoint directly:
GET https://api.nuget.org/v3/registration5-semver1/newtonsoft.json/12.0.1.json
Note: Unlisted versions don't have a standardized field for a "reason" — this is because unlisting is typically a publisher's choice to hide a version without formal deprecation, so NuGet doesn't enforce capturing a reason for it.
2. Checking if a Package Version is Deprecated
Deprecated packages have dedicated metadata in the API response, stored in the deprecation object. This object includes all the details you're looking for:
Deprecation Details Available:
reason: A predefined enum value (e.g.,Legacy,CriticalBugs,Other) that categorizes why the package was deprecatedmessage: A custom text message from the publisher explaining the deprecation in more detailalternatePackage: An object pointing to a recommended replacement package, including its ID and version range
Example Deprecation Response Snippet
Here's what a deprecated version's metadata might look like (formatted for readability):
{
"version": "12.0.1",
"listed": true,
"deprecation": {
"reason": "CriticalBugs",
"message": "This version contains a critical serialization bug that affects nested objects. Please upgrade to v13.0.0 or later.",
"alternatePackage": {
"id": "Newtonsoft.Json",
"range": "[13.0.0,)"
}
}
}
3. Using NuGet.Client for Easier Access
If you're working in a .NET project, you can use the official NuGet.Client library to avoid manually handling HTTP requests. This library provides strongly-typed resources to fetch package metadata:
- Use
PackageMetadataResourceto retrieve version metadata - Access the
PackageVersionMetadata.Listedproperty to check unlisted status - Access the
PackageVersionMetadata.Deprecationproperty to get deprecation details (if present)
For example, a quick snippet using the library:
var packageSource = new PackageSource("https://api.nuget.org/v3/index.json"); var sourceRepository = Repository.Factory.GetCoreV3(packageSource); var metadataResource = await sourceRepository.GetResourceAsync<PackageMetadataResource>(); var metadata = await metadataResource.GetMetadataAsync( "Newtonsoft.Json", true, false, new SourceCacheContext(), NullLogger.Instance, CancellationToken.None); foreach (var versionMetadata in metadata) { Console.WriteLine($"Version: {versionMetadata.Version}"); Console.WriteLine($"Unlisted: {!versionMetadata.Listed}"); if (versionMetadata.Deprecation != null) { Console.WriteLine($"Deprecated Reason: {versionMetadata.Deprecation.Reason}"); Console.WriteLine($"Deprecation Message: {versionMetadata.Deprecation.Message}"); if (versionMetadata.Deprecation.AlternatePackage != null) { Console.WriteLine($"Recommended Alternative: {versionMetadata.Deprecation.AlternatePackage.Id} ({versionMetadata.Deprecation.AlternatePackage.Range})"); } } }
内容的提问来源于stack exchange,提问作者rychlmoj

