Skip to content

responses from functions with returntype collection are not being parsed as openApiDocs YML are not representing function return types correctly as OData message bodies #665

Closed
@markwahl-msft

Description

@markwahl-msft

For example Get-MgReportCredentialUsageSummary -Period "D30" -Verbose -Debug
sends the correct request according to https://docs.microsoft.com/en-us/graph/api/reportroot-getcredentialusagesummary?view=graph-rest-beta&tabs=http
The metadata indicates that function returns <ReturnType Type="Collection(graph.credentialUsageSummary)" Nullable="false" /> and the response is formatted according to OData 4,

Each message body is represented as a single JSON object. This object is either ... or it contains a name/value pair whose name MUST be value and whose value is the correct representation for ... a collection of entities, or a collection of objects that represent changes to a previous result.

However this cmdlet fails to process the response from Microsoft graph

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#Collection(credentialUsageSummary)",
    "value": [
        {
            "@odata.type": "#microsoft.graph.credentialUsageSummary",
            "id": "11933632-39ca-4e44-a97a-6bc86aafb5c2",

with an error of

DEBUG: CmdletException: InvalidCastException - Unable to cast object of type
'Microsoft.Graph.PowerShell.Runtime.Json.JsonObject' to type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonArray'. : at Microsoft.Graph.PowerShell.Runtime.Json.JsonArray.Parse(String text)
at Microsoft.Graph.PowerShell.Reports.<>c.<ReportsGetCredentialUsageSummary_Call>b__158_0(Task`1 body)

that causes the cmdlet to fail.

Get-MgReportCredentialUsageSummary_Get: Unable to cast object of type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonObject' to type 'Microsoft.Graph.PowerShell.Runtime.Json.JsonArray'.

I observed the same behavior with an identity governance cmdlet Invoke-MgFilterEntitlementManagementAccessPackage -On "'allowedRequestor'" also failing with the same error, and observed that the stack trace included a call to JsonArray.Parse in IdentityGovernance.cs

body => If( Microsoft.Graph.PowerShell.Runtime.Json.JsonArray.Parse(body.Result) 

I believe this may be due to an issue in the openapidocs/*.yml files. I observed the functions had a response pattern that looked like

        content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/microsoft.graph.accessPackage'

rather than

          content:
            application/json:
              schema:
                title: Collection of accessPackage
                type: object
                properties:
                  value:
                    type: array
                    items:
                      $ref: '#/components/schemas/microsoft.graph.accessPackage'
                  '@odata.nextLink':
                    type: string
                additionalProperties:
                  type: object

and similarly Reports.yml had

          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/microsoft.graph.credentialUsageSummary'

whereas I would have expected

          content:
            application/json:
              schema:
                title: Collection of credentialUsageSummary
                type: object
                properties:
                  value:
                    type: array
                    items:
                      $ref: '#/components/schemas/microsoft.graph.credentialUsageSummary'
                  '@odata.nextLink':
                    type: string
                additionalProperties:
                  type: object

AB#9483

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions