Many REST APIs return different response *types* based on result count
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- api, backend-api-design
Research direction
Start by tracing the response handling represented by the packages.Items count switch, then inspect the packages, packageManifests, locales, installers, and versions API endpoints. Compare responses for zero, one, and multiple results. Done means each endpoint uses one consistent response shape, with behavior verified through the relevant API checks or tests.
Written by the indexing model from the issue text.
Description
Brief description of your issue
I just hit upon something in the REST api that I believe we should consider changing at the schema/interface level. Some of the APIs return different result types depending on the count of items returned, for example:
return packages.Items.Count switch
{
0 => new NoContentResult(),
1 => new ApiObjectResult(new ApiResponse<Package>(packages.Items.First(), packages.ContinuationToken)),
_ => new ApiObjectResult(new ApiResponse<List<Package>>(packages.Items.ToList(), packages.ContinuationToken)),
};
Either No Content (null), a single object, or an array. For example:
This returns an array: .../api/packages/Microsoft.PowerToys/versions
{"Data":[{...},{...},,…],"ContinuationToken":"…"}
This returns a single object: .../api/packages/Zwift.Zwift/versions
{"Data":{...}}
This returns 204 (null): .../api/packages/Foo.Bar/versions
If the query returns more than one result, I get an object where Data is an array (and there’s a continuationToken property), but if there’s only one result, then Data is an object. This is true for the packages, packageManifests, locales, installers and versions APIs.
IMO this is a bad practice and requires clients to try to parse the result object twice in a catch block to successfully deserialize it. Instead we should always consistently return the same object type, potentially with only one item in the array, and a null continuationToken.
Steps to reproduce
Call above APIs
Expected behavior
Response type should be consistent regardless of # of results
Actual behavior
Response type varies based on number of results
Environment
N/A
- Dominant language
- C#
- Stars
- 317
- Forks
- 79
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/winget-cli-restsource
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Issue-Feature
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Font Support OpenIssue-Feature
microsoft/winget-cli-restsource#310 · 1 comment · 1 assignee ·
-
Issue-Bug
Difficulty 4/5 3-5 days Newbie friendliness 25/100
microsoft/winget-cli-restsource#309 · 2 comments ·
All issues in microsoft/winget-cli-restsource
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·