Skip to content

Invoke-RestMethod -FollowRelLink should return a single array of objects #5526

Closed

Description

@dddugan

Invoke-RestMethod with the -FollowRelLink parameter correctly retrieves additional pages of results from REST services. However, the return is an array of pages rather than one array of all results.

Steps to reproduce

PS /> $apikey = 'MYAPIKEYHERE'
PS /> $headers = @{'Accept'='application/json';'Content-Type'='application/json';'Authorization'="SSWS $apikey"}
PS />
PS /> $users = Invoke-RestMethod -Uri 'https://MYORGNAME.okta.com/api/v1/users/?filter=status eq "ACTIVE"&limit=10' -Method GET -Headers $headers -FollowRelLink -MaximumFollowRelLink 3
PS />
PS /> $users.Count
3
PS /> $users.Item(0).Count
10
PS /> $users.Item(1).Count
10
PS /> $users.Item(2).Count
10
PS />

Expected behavior

Invoke-RestMethod should return a single array of objects instead of an array of arrays (pages).

PS /> $users.Count
30

Actual behavior

Invoke-RestMethod returns an array of arrays (pages).

Environment data

PS /> $PSVersionTable                                                                                                   
Name                           Value
----                           -----
PSVersion                      6.0.0-rc
PSEdition                      Core
GitCommitId                    v6.0.0-rc
OS                             Linux 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


PS />

Activity

  1. dddugan commented on Nov 22, 2017

    @dddugan
    Author

    The specific API I'm using returns multiple Link headers, which may or may not be related.
    #5265

  2. markekraus commented on Nov 22, 2017

    @markekraus
    Contributor

    I disagree. If we do this we lose parity with the JSON objects being returned from the APIs.

    Page 1

    [
      {
        "Value": "One",
        "Id": 1
      },
      {
        "Value": "Two",
        "Id": 2
      }
    ]

    Page 2:

    [
      {
        "Value": "Three",
        "Id": 3
      },
      {
        "Value": "Four",
        "Id": 4
      }
    ]

    That is clearly 2 JSON arrays of JSON objects, not a single array of objects.

  3. added
    Issue-Discussion
    on Nov 22, 2017
  4. markekraus commented on Nov 22, 2017

    @markekraus
    Contributor

    #5265 is unrelated to the output.

  5. markekraus commented on Nov 22, 2017

    @markekraus
    Contributor

    I will also add that unwrapping the jagged array is easily done with Foreach-Object:

    $uri = 'https://api.github.com/repos/powershell/powershell/issues'
    Invoke-RestMethod -FollowRelLink -MaximumFollowRelLink 2 -uri $Uri | 
        Measure-Object

    Count : 2

    $uri = 'https://api.github.com/repos/powershell/powershell/issues'
    Invoke-RestMethod -FollowRelLink -MaximumFollowRelLink 2 -uri $Uri | 
        Foreach-Object { $_ } |
        Measure-Object

    Count: 60.

  6. dddugan commented on Nov 22, 2017

    @dddugan
    Author

    Thanks, Mark Kraus (@markekraus). You're absolutely right that the current behavior more closely matches the actual return. However, from my (outside) perspective FollowRelLink was added as a convenience so the caller doesn't need to think about paging and multiple calls. With that spirit in mind it seems like we shouldn't require them to post-process results in any way either.

  7. markekraus commented on Nov 22, 2017

    @markekraus
    Contributor

    dddugan It's certainly a difference of opinion on how it should work. On the one hand, we need to maintain parity with the objects as they are returned from the API and on the other is convenience. I cannot condone making the default behavior break parity with APIs. As someone who works heavily with APIs, I cannot stand it when something makes assumptions about how the objects should be provided to me, especially when that assumption breaks with what the API has documented. The API says it will return an array of objects per page, I would expect and array of arrays from FollowReLlink. My vehicle for obtaining these objects from the API may change, and I don't want to adjust my logic to fit with what ever API breaking format that vehicle has decided to impose on me.

    We could maybe add a switch to unwrap arrays, but, I'm not real excited about doing so. I think it is best to just return the objects as they are and let the user decide how they want to process them.

    FollowRelLink was added as a convenience so the caller doesn't need to think about paging and multiple calls.

    And they don't. In 5.1 for this operation to work you needed

    1. fall back to Invoke-WebRequest,
    2. Check for errors,
    3. Inspect Content-type header,
    4. convert the Content to an object,
    5. handle serialization errors,
    6. inspect the headers for Link,
    7. regex parse the single Link header,
    8. and finally repeat the process with the next link URI.

    In 6.0.0 irm -FollowRelLink manages all of that for you.

  8. jmiller76 commented on Nov 22, 2017

    @jmiller76

    Mark Kraus (@markekraus) I second that. It is easy to merge the multiple pages in script, but not trivial to unwind it if merged in cmdlet.

    Unless there is a particular efficiency gain from append them in the cmdlet that is.

    Does the project have any specific single responsibility type guidance for things like this?

  9. markekraus commented on Nov 22, 2017

    @markekraus
    Contributor

    Does the project have any specific single responsibility type guidance for things like this?

    Good question. I don't know about any in the project specifically, There is always the "The PowerShell Best Practices and Style Guide" (One relvant to this thread)

  10. dddugan commented on Nov 28, 2017

    @dddugan
    Author

    Fair enough. Given:

    1. The current behavior more closely matches the objects as returned from the API.
    2. The caller collapsing a jagged array is vastly simpler than unwinding it.

    I see the logic in status quo. It just wasn't the behavior I originally expected, which prompted me to raise the issue prior to GA.

    Any further discussion before we close this?

  11. markekraus commented on Nov 28, 2017

    @markekraus
    Contributor

    dddugan nothing from my end. I think this might make sense to note this in the documentation. If that wasn't what you expected, it's likely others will possible have the same experience.

  12. added
    Resolution-Answered
    on Dec 1, 2017
  13. silentdth commented on Sep 7, 2020

    @silentdth

    Just a note here - in PS 7.10 preview 6 the FollowRelLink doesn't work at all. You only get the first page.

  14. vexx32 commented on Sep 7, 2020

    @vexx32
    Collaborator

    silentdth you should probably file a new issue for that so it can be triaged appropriately, assuming there isn't an open issue for it yet. 🙂

  15. silentdth commented on Sep 7, 2020

    @silentdth

    Will do Rain Sallow (/u/ta11ow) (@vexx32) . For now I will use the DIY version for V5 of Powershell so I can get my job done ,then i'll come back and log it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-AnsweredWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions