-
Notifications
You must be signed in to change notification settings - Fork 748
[NSJ -> STJ]Migrate ServiceIndexResource deserialization #7297
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
base: dev-feature-nsj-stj-migration
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // 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.Collections.Generic; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace NuGet.Protocol.Model | ||
| { | ||
| internal sealed class ServiceIndexModel | ||
| { | ||
| [JsonPropertyName("version")] | ||
| public string? Version { get; set; } | ||
|
|
||
| [JsonPropertyName("resources")] | ||
| public IReadOnlyList<ServiceIndexEntryModel>? Resources { get; set; } | ||
| } | ||
|
|
||
| internal sealed class ServiceIndexEntryModel | ||
| { | ||
| [JsonPropertyName("@id")] | ||
| public string? Id { get; set; } | ||
|
|
||
| /// <summary>JSON string or array of strings.</summary> | ||
| [JsonPropertyName("@type")] | ||
| public JsonElement Type { get; set; } | ||
|
|
||
| /// <summary>Optional JSON string or array of strings.</summary> | ||
| [JsonPropertyName("clientVersion")] | ||
| [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] | ||
| public JsonElement ClientVersion { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,11 +6,14 @@ | |||||
| using System; | ||||||
| using System.Collections.Generic; | ||||||
| using System.Linq; | ||||||
| using System.Text.Json; | ||||||
| using Newtonsoft.Json.Linq; | ||||||
| using NuGet.Configuration; | ||||||
| using NuGet.Packaging; | ||||||
| using NuGet.Protocol.Core.Types; | ||||||
| using NuGet.Protocol.Events; | ||||||
| using NuGet.Protocol.Model; | ||||||
| using NuGet.Protocol.Utility; | ||||||
| using NuGet.Versioning; | ||||||
|
|
||||||
| namespace NuGet.Protocol | ||||||
|
|
@@ -21,6 +24,7 @@ namespace NuGet.Protocol | |||||
| public class ServiceIndexResourceV3 : INuGetResource | ||||||
| { | ||||||
| private readonly string _json; | ||||||
| private readonly ServiceIndexModel _model; | ||||||
| private readonly IDictionary<string, List<ServiceIndexEntry>> _index; | ||||||
| private readonly DateTime _requestTime; | ||||||
| private static readonly IReadOnlyList<ServiceIndexEntry> _emptyEntries = new List<ServiceIndexEntry>(); | ||||||
|
|
@@ -35,6 +39,13 @@ internal ServiceIndexResourceV3(JObject index, DateTime requestTime, PackageSour | |||||
|
|
||||||
| public ServiceIndexResourceV3(JObject index, DateTime requestTime) : this(index, requestTime, null) { } | ||||||
|
|
||||||
| internal ServiceIndexResourceV3(ServiceIndexModel model, DateTime requestTime, PackageSource packageSource) | ||||||
| { | ||||||
| _model = model; | ||||||
| _index = MakeLookup(model, packageSource); | ||||||
| _requestTime = requestTime; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Time the index was requested | ||||||
| /// </summary> | ||||||
|
|
@@ -56,10 +67,7 @@ public virtual IReadOnlyList<ServiceIndexEntry> Entries | |||||
|
|
||||||
| public virtual string Json | ||||||
| { | ||||||
| get | ||||||
| { | ||||||
| return _json; | ||||||
| } | ||||||
| get { return _json ?? JsonSerializer.Serialize(_model, JsonContext.Default.ServiceIndexModel); } | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern results in serialization occuring once and seems have similar characteristics as the old code.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
|
|
@@ -148,6 +156,87 @@ public virtual IReadOnlyList<Uri> GetServiceEntryUris(NuGetVersion clientVersion | |||||
| return GetServiceEntries(clientVersion, orderedTypes).Select(e => e.Uri).ToList(); | ||||||
| } | ||||||
|
|
||||||
| private static IDictionary<string, List<ServiceIndexEntry>> MakeLookup(ServiceIndexModel index, PackageSource packageSource) | ||||||
| { | ||||||
| var result = new Dictionary<string, List<ServiceIndexEntry>>(StringComparer.Ordinal); | ||||||
|
|
||||||
| if (index?.Resources is null) | ||||||
| { | ||||||
| return result; | ||||||
| } | ||||||
|
|
||||||
| foreach (var resource in index.Resources) | ||||||
| { | ||||||
| var id = resource.Id; | ||||||
| if (string.IsNullOrEmpty(id) || !Uri.TryCreate(id, UriKind.Absolute, out Uri uri)) | ||||||
| { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| if (packageSource != null && uri.Scheme == Uri.UriSchemeHttp && packageSource.IsHttps) | ||||||
| { | ||||||
| ProtocolDiagnostics.RaiseEvent(new ProtocolDiagnosticServiceIndexEntryEvent(source: packageSource.Source, httpsSourceHasHttpResource: true)); | ||||||
| } | ||||||
|
|
||||||
| var types = GetStringValues(resource.Type).ToArray(); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get an |
||||||
|
|
||||||
| var clientVersions = new List<SemanticVersion>(); | ||||||
| if (resource.ClientVersion.ValueKind == JsonValueKind.Undefined) | ||||||
| { | ||||||
| clientVersions.Add(_defaultVersion); | ||||||
| } | ||||||
| else | ||||||
| { | ||||||
| foreach (var versionString in GetStringValues(resource.ClientVersion)) | ||||||
| { | ||||||
| if (SemanticVersion.TryParse(versionString, out SemanticVersion semVer)) | ||||||
| { | ||||||
| clientVersions.Add(semVer); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| foreach (var type in types) | ||||||
| { | ||||||
| foreach (var clientVersion in clientVersions) | ||||||
| { | ||||||
| if (!result.TryGetValue(type, out List<ServiceIndexEntry> entries)) | ||||||
| { | ||||||
| entries = new List<ServiceIndexEntry>(); | ||||||
| result.Add(type, entries); | ||||||
| } | ||||||
|
|
||||||
| entries.Add(new ServiceIndexEntry(uri, type, clientVersion)); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| foreach (var type in result.Keys.ToArray()) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here. Generating an array for |
||||||
| { | ||||||
| result[type] = result[type].OrderByDescending(e => e.ClientVersion).ToList(); | ||||||
| } | ||||||
|
|
||||||
| return result; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should validate that the caller actually needs a materialized list. |
||||||
| } | ||||||
|
|
||||||
| private static IEnumerable<string> GetStringValues(JsonElement element) | ||||||
| { | ||||||
| if (element.ValueKind == JsonValueKind.Array) | ||||||
| { | ||||||
| foreach (var item in element.EnumerateArray()) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider writing an extension method that is |
||||||
| { | ||||||
| if (item.ValueKind == JsonValueKind.String) | ||||||
| { | ||||||
| yield return item.GetString(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| else if (element.ValueKind == JsonValueKind.String) | ||||||
| { | ||||||
| yield return element.GetString(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private static IDictionary<string, List<ServiceIndexEntry>> MakeLookup(JObject index, PackageSource packageSource) | ||||||
| { | ||||||
| var result = new Dictionary<string, List<ServiceIndexEntry>>(StringComparer.Ordinal); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
is null? Seems simpler and equivalent.