Skip to content

Commit

Permalink
(NuGet#9) Alter query emitted when doing a count query
Browse files Browse the repository at this point in the history
In a previous commit, changes were made to allow the execution of a
count query, to get information back about the number of packages that
meet a specific query.  During testing, it was found that this query,
which included usage of the skip and top options, wasn't working as
expected when hitting a Nexus Repository v2 feed.  Instead of returning
the number of packages, it would instead return the number that was
included in the top parameter, which was hard-coded to 1.

This then caused a problem when using Chocolatey GUI, since Chocolatey
GUI needs to know the total number of packages for a query, so that it
can set up the necessary paging within the application to see all the
results.

This commit alters the V2FeedQueryBuilder class to have a completely
separate format string for the count query, rather than trying to use
the existing search format string.  This means that there is clear
segregation between the format strings is use, and we can then easily
remove the problematic skip/top options.  It should be noted that other
repository servers did not have a problem with these extra options, it
was only found that Nexus did.  However, this change also makes the
count query the same as the one that was emitted by Chocolatey CLI 1.x,
so it makes sense to move forward with this change.
  • Loading branch information
gep13 committed Apr 21, 2023
1 parent 8c9d227 commit 3a89020
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/NuGet.Core/NuGet.Protocol/LegacyFeed/V2FeedQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public class V2FeedQueryBuilder
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private const string SearchEndpointFormat = "/Search(){0}?{1}{2}searchTerm='{3}'&targetFramework='{4}'&includePrerelease={5}&$skip={6}&$top={7}&" + SemVerLevel;
private const string CountQueryString = "/$count";
private const string SearchEndpointFormat = "/Search()?{0}{1}searchTerm='{2}'&targetFramework='{3}'&includePrerelease={4}&$skip={5}&$top={6}&" + SemVerLevel;
private const string CountEndpointFormat = "/Search()/$count?{0}{1}searchTerm='{2}'&targetFramework='{3}'&includePrerelease={4}&" + SemVerLevel;
private const string ExactFilterFormat = "tolower(Id)%20eq%20'{0}'";
private const string ByIdOnlyFormat = "substringof('{0}',tolower(Id))";
private const string ByTagOnlyFormat = "substringof('{0}',Tags)";
Expand Down Expand Up @@ -124,27 +124,38 @@ public string BuildSearchUri(

var filter = BuildFilter(searchTerm, filters, includePropertyClauses: false);

var uri = string.Format(
CultureInfo.InvariantCulture,
SearchEndpointFormat,

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
isCount ? CountQueryString : string.Empty,
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

filter != null ? filter + QueryDelimiter : string.Empty,
orderBy != null ? orderBy + QueryDelimiter : string.Empty,
UriUtility.UrlEncodeOdataParameter(searchTerm),
UriUtility.UrlEncodeOdataParameter(shortFormTargetFramework),
filters.IncludePrerelease.ToString(CultureInfo.CurrentCulture).ToLowerInvariant(),
skip,
take);
if (isCount)
{
return string.Format(
CultureInfo.InvariantCulture,
CountEndpointFormat,
filter != null ? filter + QueryDelimiter : string.Empty,
orderBy != null ? orderBy + QueryDelimiter : string.Empty,
UriUtility.UrlEncodeOdataParameter(searchTerm),
UriUtility.UrlEncodeOdataParameter(shortFormTargetFramework),
filters.IncludePrerelease.ToString(CultureInfo.CurrentCulture).ToLowerInvariant());
}
else
{
return string.Format(
CultureInfo.InvariantCulture,
SearchEndpointFormat,
filter != null ? filter + QueryDelimiter : string.Empty,
orderBy != null ? orderBy + QueryDelimiter : string.Empty,
UriUtility.UrlEncodeOdataParameter(searchTerm),
UriUtility.UrlEncodeOdataParameter(shortFormTargetFramework),
filters.IncludePrerelease.ToString(CultureInfo.CurrentCulture).ToLowerInvariant(),
skip,
take);
}

return uri;
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}

public string BuildFindPackagesByIdUri(string id)
Expand Down

0 comments on commit 3a89020

Please sign in to comment.