豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 26cdcd1

Browse files
committed
Migrate AutoComplete
1 parent de3b92c commit 26cdcd1

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Text.Json.Serialization;
5+
6+
namespace NuGet.Protocol.Model
7+
{
8+
internal sealed class AutoCompleteModel
9+
{
10+
[JsonPropertyName("data")]
11+
public string[]? Data { get; set; }
12+
}
13+
}

src/NuGet.Core/NuGet.Protocol/Resources/AutoCompleteResourceV3.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
using System.Globalization;
99
using System.Linq;
1010
using System.Net;
11+
using System.Text.Json;
1112
using System.Threading;
1213
using System.Threading.Tasks;
13-
using Newtonsoft.Json.Linq;
1414
using NuGet.Protocol.Core.Types;
15+
using NuGet.Protocol.Model;
16+
using NuGet.Protocol.Utility;
1517
using NuGet.Versioning;
1618

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

5759
var queryUri = queryUrl.Uri;
58-
var results = await _client.GetJObjectAsync(
60+
AutoCompleteModel results = await _client.ProcessStreamAsync(
5961
new HttpSourceRequest(queryUri, logger),
62+
async stream =>
63+
{
64+
if (stream == null)
65+
{
66+
return null;
67+
}
68+
69+
return await JsonSerializer.DeserializeAsync(stream, JsonContext.Default.AutoCompleteModel, token);
70+
},
6071
logger,
6172
token);
73+
6274
token.ThrowIfCancellationRequested();
63-
if (results == null)
64-
{
65-
return Enumerable.Empty<string>();
66-
}
67-
var data = results.Value<JArray>("data");
68-
if (data == null)
69-
{
70-
return Enumerable.Empty<string>();
71-
}
7275

73-
// Resolve all the objects
74-
var outputs = new List<string>();
75-
foreach (var result in data)
76+
if (results?.Data == null)
7677
{
77-
if (result != null)
78-
{
79-
outputs.Add(result.ToString());
80-
}
78+
return Enumerable.Empty<string>();
8179
}
8280

83-
return outputs.Where(item => item.StartsWith(packageIdPrefix, StringComparison.OrdinalIgnoreCase));
81+
return results.Data
82+
.Where(item => item != null && item.StartsWith(packageIdPrefix, StringComparison.OrdinalIgnoreCase));
8483
}
8584

8685
public override async Task<IEnumerable<NuGetVersion>> VersionStartsWith(

src/NuGet.Core/NuGet.Protocol/Utility/JsonContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ namespace NuGet.Protocol.Utility
1717
[JsonSerializable(typeof(HttpFileSystemBasedFindPackageByIdResource.FlatContainerVersionList))]
1818
[JsonSerializable(typeof(IReadOnlyList<V3VulnerabilityIndexEntry>), TypeInfoPropertyName = "VulnerabilityIndex")]
1919
[JsonSerializable(typeof(CaseInsensitiveDictionary<IReadOnlyList<PackageVulnerabilityInfo>>), TypeInfoPropertyName = "VulnerabilityPage")]
20+
<<<<<<< Updated upstream
21+
=======
22+
[JsonSerializable(typeof(AutoCompleteModel))]
23+
[JsonSerializable(typeof(ServiceIndexModel))]
24+
>>>>>>> Stashed changes
2025
internal partial class JsonContext : JsonSerializerContext
2126
{
2227
}

0 commit comments

Comments
 (0)