豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
Draft
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
13 changes: 13 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/Model/AutoCompleteModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Text.Json.Serialization;

namespace NuGet.Protocol.Model
{
internal sealed class AutoCompleteModel
{
[JsonPropertyName("data")]
public string[]? Data { get; set; }
}
}
37 changes: 18 additions & 19 deletions src/NuGet.Core/NuGet.Protocol/Resources/AutoCompleteResourceV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Model;
using NuGet.Protocol.Utility;
using NuGet.Versioning;

namespace NuGet.Protocol
Expand Down Expand Up @@ -55,32 +57,29 @@ public override async Task<IEnumerable<string>> IdStartsWith(
Common.ILogger logger = log ?? Common.NullLogger.Instance;

var queryUri = queryUrl.Uri;
var results = await _client.GetJObjectAsync(
AutoCompleteModel results = await _client.ProcessStreamAsync(
Copy link
Copy Markdown

@richlander richlander Apr 17, 2026

Choose a reason for hiding this comment

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

I am surprised that AutoCompleteModel is always non-null. Is that correct?

Does this library not have nullable enabled?

new HttpSourceRequest(queryUri, logger),
async stream =>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know what the team code style is, if they link lamdas defined ahead of the method call or within it.

{
if (stream == null)
{
return null;
}

return await JsonSerializer.DeserializeAsync(stream, JsonContext.Default.AutoCompleteModel, token);
},
logger,
token);

token.ThrowIfCancellationRequested();
if (results == null)
{
return Enumerable.Empty<string>();
}
var data = results.Value<JArray>("data");
if (data == null)
{
return Enumerable.Empty<string>();
}

// Resolve all the objects
var outputs = new List<string>();
foreach (var result in data)
if (results?.Data == null)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You could adopt the null-coalescing syntax:

var returnValue = results?.Data.Where(item => item != null && item.StartsWith(packageIdPrefix, StringComparison.OrdinalIgnoreCase));

return returnValue ?? Enumerable.Empty<string>();

Or some variant of that.

{
if (result != null)
{
outputs.Add(result.ToString());
}
return Enumerable.Empty<string>();
}

return outputs.Where(item => item.StartsWith(packageIdPrefix, StringComparison.OrdinalIgnoreCase));
return results.Data
.Where(item => item != null && item.StartsWith(packageIdPrefix, StringComparison.OrdinalIgnoreCase));
}

public override async Task<IEnumerable<NuGetVersion>> VersionStartsWith(
Expand Down
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace NuGet.Protocol.Utility
[JsonSerializable(typeof(HttpFileSystemBasedFindPackageByIdResource.FlatContainerVersionList))]
[JsonSerializable(typeof(IReadOnlyList<V3VulnerabilityIndexEntry>), TypeInfoPropertyName = "VulnerabilityIndex")]
[JsonSerializable(typeof(CaseInsensitiveDictionary<IReadOnlyList<PackageVulnerabilityInfo>>), TypeInfoPropertyName = "VulnerabilityPage")]
[JsonSerializable(typeof(AutoCompleteModel))]
internal partial class JsonContext : JsonSerializerContext
{
}
Expand Down