From 287cdc30aa95aca10d4b1f080f2679ad6e67c12b Mon Sep 17 00:00:00 2001 From: glopesdev Date: Sat, 28 Dec 2024 14:59:05 +0000 Subject: [PATCH] Refactor package type as a single value property --- .../Formatters/SearchFilterFormatter.cs | 14 +++++++------- .../LocalPackageSearchResource.cs | 18 +++++++----------- .../PublicAPI/net472/PublicAPI.Shipped.txt | 2 -- .../PublicAPI/net472/PublicAPI.Unshipped.txt | 2 ++ .../netcoreapp5.0/PublicAPI.Shipped.txt | 2 -- .../netcoreapp5.0/PublicAPI.Unshipped.txt | 2 ++ .../netstandard2.0/PublicAPI.Shipped.txt | 2 -- .../netstandard2.0/PublicAPI.Unshipped.txt | 2 ++ .../Resources/PackageSearchResourceV3.cs | 8 ++------ .../Resources/RawSearchResourceV3.cs | 8 ++------ src/NuGet.Core/NuGet.Protocol/SearchFilter.cs | 4 ++-- .../Formatters/SearchFilterFormatterTests.cs | 4 ++-- .../LocalPackageSearchResourceTests.cs | 2 +- .../PackageSearchResourceV3Tests.cs | 2 +- 14 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/NuGet.Clients/NuGet.VisualStudio.Internal.Contracts/Formatters/SearchFilterFormatter.cs b/src/NuGet.Clients/NuGet.VisualStudio.Internal.Contracts/Formatters/SearchFilterFormatter.cs index 3d82efd68f0..1efb0759718 100644 --- a/src/NuGet.Clients/NuGet.VisualStudio.Internal.Contracts/Formatters/SearchFilterFormatter.cs +++ b/src/NuGet.Clients/NuGet.VisualStudio.Internal.Contracts/Formatters/SearchFilterFormatter.cs @@ -12,7 +12,7 @@ internal sealed class SearchFilterFormatter : NuGetMessagePackFormatter? supportedFrameworks = null; - IEnumerable? packageTypes = null; + string? packageType = null; int propertyCount = reader.ReadMapHeader(); for (int propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++) @@ -43,8 +43,8 @@ private SearchFilterFormatter() case IncludeDelistedPropertyName: includeDelisted = reader.ReadBoolean(); break; - case PackageTypesPropertyName: - packageTypes = options.Resolver.GetFormatter>()!.Deserialize(ref reader, options); + case PackageTypePropertyName: + packageType = reader.ReadString(); break; case FilterPropertyName: filterType = options.Resolver.GetFormatter()!.Deserialize(ref reader, options); @@ -65,7 +65,7 @@ private SearchFilterFormatter() { SupportedFrameworks = supportedFrameworks, OrderBy = searchOrderBy, - PackageTypes = packageTypes, + PackageType = packageType, IncludeDelisted = includeDelisted, }; } @@ -77,8 +77,8 @@ protected override void SerializeCore(ref MessagePackWriter writer, SearchFilter writer.Write(value.IncludePrerelease); writer.Write(IncludeDelistedPropertyName); writer.Write(value.IncludeDelisted); - writer.Write(PackageTypesPropertyName); - options.Resolver.GetFormatter>()!.Serialize(ref writer, value.PackageTypes, options); + writer.Write(PackageTypePropertyName); + writer.Write(value.PackageType); writer.Write(FilterPropertyName); options.Resolver.GetFormatter()!.Serialize(ref writer, value.Filter, options); writer.Write(OrderByPropertyName); diff --git a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/LocalPackageSearchResource.cs b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/LocalPackageSearchResource.cs index 5d63a3fbf5a..9affb76129d 100644 --- a/src/NuGet.Core/NuGet.Protocol/LocalRepositories/LocalPackageSearchResource.cs +++ b/src/NuGet.Core/NuGet.Protocol/LocalRepositories/LocalPackageSearchResource.cs @@ -60,18 +60,14 @@ public async override Task> SearchAsync( } // Filter on package types - if (filters?.PackageTypes != null - && filters.PackageTypes.Any()) + if (!string.IsNullOrEmpty(filters.PackageType)) { - foreach (var packageTypeName in filters.PackageTypes) - { - query = query - .Where(package => package.Nuspec - .GetPackageTypes() - .Any(packageType => StringComparer.OrdinalIgnoreCase.Equals( - packageType.Name, - packageTypeName))); - } + query = query + .Where(package => package.Nuspec + .GetPackageTypes() + .Any(packageType => StringComparer.OrdinalIgnoreCase.Equals( + packageType.Name, + filters.PackageType))); } // Collapse to the highest version per id, if necessary diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt index 6cfec1efc98..b08024dc31d 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy? NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void -~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable -~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void ~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt index 7dc5c58110b..2cc260b10e0 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string +~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt index 4f3ea636015..0f1feedcb40 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy? NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void -~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable -~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void ~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt index 7dc5c58110b..2cc260b10e0 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string +~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt index 4f3ea636015..0f1feedcb40 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy? NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void -~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable -~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void ~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 7dc5c58110b..2cc260b10e0 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string +~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void diff --git a/src/NuGet.Core/NuGet.Protocol/Resources/PackageSearchResourceV3.cs b/src/NuGet.Core/NuGet.Protocol/Resources/PackageSearchResourceV3.cs index 0ba8d5974dc..9adf8fbfcd5 100644 --- a/src/NuGet.Core/NuGet.Protocol/Resources/PackageSearchResourceV3.cs +++ b/src/NuGet.Core/NuGet.Protocol/Resources/PackageSearchResourceV3.cs @@ -137,13 +137,9 @@ private async Task SearchPage( queryString += "&" + frameworks; } - if (filters.PackageTypes != null - && filters.PackageTypes.Any()) + if (!string.IsNullOrEmpty(filters.PackageType)) { - var types = string.Join("&", - filters.PackageTypes.Select( - s => "packageType=" + s)); - queryString += "&" + types; + queryString += "&packageType=" + filters.PackageType; } queryString += "&semVerLevel=2.0.0"; diff --git a/src/NuGet.Core/NuGet.Protocol/Resources/RawSearchResourceV3.cs b/src/NuGet.Core/NuGet.Protocol/Resources/RawSearchResourceV3.cs index 6c98f5ffd73..e52a1a2b9fe 100644 --- a/src/NuGet.Core/NuGet.Protocol/Resources/RawSearchResourceV3.cs +++ b/src/NuGet.Core/NuGet.Protocol/Resources/RawSearchResourceV3.cs @@ -67,13 +67,9 @@ public virtual async Task SearchPage(string searchTerm, SearchFilter fi queryString += "&" + frameworks; } - if (filters.PackageTypes != null - && filters.PackageTypes.Any()) + if (!string.IsNullOrEmpty(filters.PackageType)) { - var types = string.Join("&", - filters.PackageTypes.Select( - s => "packageTypeFilter=" + s)); - queryString += "&" + types; + queryString += "&packageType=" + filters.PackageType; } queryString += "&semVerLevel=2.0.0"; diff --git a/src/NuGet.Core/NuGet.Protocol/SearchFilter.cs b/src/NuGet.Core/NuGet.Protocol/SearchFilter.cs index a574c942d59..2d91ba0d2fb 100644 --- a/src/NuGet.Core/NuGet.Protocol/SearchFilter.cs +++ b/src/NuGet.Core/NuGet.Protocol/SearchFilter.cs @@ -55,9 +55,9 @@ public SearchFilter(bool includePrerelease, SearchFilterType? filter) public bool IncludeDelisted { get; set; } = false; /// - /// Restrict the search to certain package types. + /// Restrict the search to a specific package type. /// - public IEnumerable PackageTypes { get; set; } = Enumerable.Empty(); + public string PackageType { get; set; } = null; /// /// The optional filter type. Absense of this value indicates that all versions should be returned. diff --git a/test/NuGet.Clients.Tests/NuGet.VisualStudio.Internal.Contracts.Test/Formatters/SearchFilterFormatterTests.cs b/test/NuGet.Clients.Tests/NuGet.VisualStudio.Internal.Contracts.Test/Formatters/SearchFilterFormatterTests.cs index 4f6e79b7694..4d390fe8f3e 100644 --- a/test/NuGet.Clients.Tests/NuGet.VisualStudio.Internal.Contracts.Test/Formatters/SearchFilterFormatterTests.cs +++ b/test/NuGet.Clients.Tests/NuGet.VisualStudio.Internal.Contracts.Test/Formatters/SearchFilterFormatterTests.cs @@ -20,7 +20,7 @@ public void SerializeThenDeserialize_WithValidArguments_RoundTrips(SearchFilter Assert.Equal(expectedResult.IncludeDelisted, actualResult.IncludeDelisted); Assert.Equal(expectedResult.IncludePrerelease, actualResult.IncludePrerelease); Assert.Equal(expectedResult.OrderBy, actualResult.OrderBy); - Assert.Equal(expectedResult.PackageTypes, actualResult.PackageTypes); + Assert.Equal(expectedResult.PackageType, actualResult.PackageType); Assert.Equal(expectedResult.SupportedFrameworks, actualResult.SupportedFrameworks); } @@ -31,7 +31,7 @@ public void SerializeThenDeserialize_WithValidArguments_RoundTrips(SearchFilter { IncludeDelisted = true, OrderBy = SearchOrderBy.Id, - PackageTypes = new List() { "packageType1", "packageType2" }, + PackageType = "packageType", SupportedFrameworks = new List() { ".Net451", ".Net452" } } }, diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/LocalResourceTests/LocalPackageSearchResourceTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/LocalResourceTests/LocalPackageSearchResourceTests.cs index 180f0b1cede..9193c3cf116 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/LocalResourceTests/LocalPackageSearchResourceTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/LocalResourceTests/LocalPackageSearchResourceTests.cs @@ -253,7 +253,7 @@ public async Task LocalPackageSearchResource_MatchOnPackageTypeAsync() var filter = new SearchFilter(includePrerelease: true) { - PackageTypes = new[] { "test" } + PackageType = "test" }; // Act diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/PackageSearchResourceV3Tests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/PackageSearchResourceV3Tests.cs index 968c9020db3..dc1cb6a995d 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/PackageSearchResourceV3Tests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/PackageSearchResourceV3Tests.cs @@ -130,7 +130,7 @@ public async Task PackageSearchResourceV3_SearchPackageType() var searchFilter = new SearchFilter(includePrerelease: false) { - PackageTypes = new[] { "test" } + PackageType = "test" }; // Act