Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object sent as part of additionalProperties for New-MgExternalConnection truncated #2352

Closed
waldekmastykarz opened this issue Oct 10, 2023 · 3 comments

Comments

@waldekmastykarz
Copy link

Describe the bug

Object sent as part of additionalProperties for New-MgExternalConnection truncated

To Reproduce

  1. Check out this script: https://github.com/waldekmastykarz/graph-connectors-samples/tree/powershell-resultlayout/samples/powershell-markdown
  2. Follow instructions from readme
  3. After initial setup, run .\Initialize-ExternalConnection.ps1

Expected behavior

New external connection created with a result template

Debug Output

DEBUG: [CmdletBeginProcessing]: - New-MgExternalConnection begin processing with parameterSet 'Create'.
DEBUG: [Authentication]: - AuthType: 'AppOnly', TokenCredentialType: 'ClientSecret', ContextScope: 'Process', AppName: 'Waldek Mastykarz (blog) - connector (PowerShell)'.
DEBUG: [Authentication]: - Scopes: [ExternalItem.ReadWrite.OwnedBy, ExternalConnection.ReadWrite.OwnedBy].

Confirm
Are you sure you want to perform this action?
Performing the operation "New-MgExternalConnection_Create" on target "Call remote 'POST /external/connections' operation".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
POST

Absolute Uri:
https://graph.microsoft.com/v1.0/external/connections

Headers:
FeatureFlag                   : 00000043
Cache-Control                 : no-store, no-cache
User-Agent                    : Mozilla/5.0,(Macintosh; Darwin 23.0.0 Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:42 PDT 2023; root:xnu-10002.1.13~1/RELEASE_X86_64; en-NL),PowerShell/7.3.7
Accept-Encoding               : gzip
Retry-Attempt                 : 1
SdkVersion                    : graph-powershell/2.6.1,
client-request-id             : e0e7542a-6c29-4be4-b65e-835788e0c4e

Body:
{
  "id": "waldekblogpowershell8",
  "description": "Tips and best practices for building applications on Microsoft 365 by Waldek Mastykarz - Microsoft 365 Cloud Developer Advocate",
  "name": "Waldek Mastykarz (blog); PowerShell",
  "activitySettings": {
    "urlToItemResolvers": [
      {
        "urlMatchInfo": {
          "baseUrls": [
            "https://blog.mastykarz.nl"
          ],
          "urlPattern": "/(?<slug>[^/]+)"
        },
        "itemId": "{slug}",
        "@odata.type": "#microsoft.graph.externalConnectors.itemIdResolver",
        "priority": 1
      }
    ]
  },
  "searchSettings": {
    "searchResultTemplates": [
      {
        "id": "waldekblogpwsh",
        "layout": {
          "additionalProperties": "@{type=AdaptiveCard; version=1.3; body=System.Object[]; $schema=http://adaptivecards.io/schemas/adaptive-card.json; $data=}"
        },
        "priority": 1
      }
    ]
  }
}


DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
Conflict

Headers:
Transfer-Encoding             : chunked
Vary                          : Accept-Encoding
Strict-Transport-Security     : max-age=31536000
request-id                    : 8443115d-7e60-4e94-82b8-e7120a028404
client-request-id             : e0e7542a-6c29-4be4-b65e-835788e0c4e5
x-ms-ags-diagnostic           : {"ServerInfo":{"DataCenter":"West Europe","Slice":"E","Ring":"5","ScaleUnit":"004","RoleInstance":"AM2PEPF0000BE44"}}
X-BackEndHttpStatus           : 409,409
X-CalculatedFETarget          : PR1P264CU006.internal.outlook.com
X-CalculatedBETarget          : PR3P192MB0864.EURP192.PROD.OUTLOOK.COM
X-FEServer                    : PR1P264CA0095,AM0PR05CA0085
X-BEServer                    : PAXP192MB1151
Date                          : Tue, 10 Oct 2023 14:36:16 GM

Body:
{
  "error": {
    "code": "NameAlreadyExists",
    "message": "The specified resource name already exists",
    "innerError": {
      "code": "ConnectionExists",
      "message": "A connection with the id 'waldekblogpowershell8' already exists. If you recently deleted a connection with the same id, please retry after some time or use a different connection id.",
      "date": "2023-10-10T14:36:16",
      "request-id": "8443115d-7e60-4e94-82b8-e7120a028404",
      "client-request-id": "e0e7542a-6c29-4be4-b65e-835788e0c4e5"
    }
  }
}


New-MgExternalConnection_Create: /Users/waldek/github/waldekmastykarz/graph-connectors-samples/samples/powershell-markdown/Initialize-ExternalConnection.ps1:5
Line |
   5 |  New-MgExternalConnection -BodyParameter $externalConnection.connectio …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The specified resource name already exists  Status: 409 (Conflict) ErrorCode: NameAlreadyExists Date: 2023-10-10T14:36:16
     | Headers: Transfer-Encoding             : chunked Vary                          : Accept-Encoding
     | Strict-Transport-Security     : max-age=31536000 request-id                    : 8443115d-7e60-4e94-82b8-e7120a028404
     | client-request-id             : e0e7542a-6c29-4be4-b65e-835788e0c4e5 x-ms-ags-diagnostic           :
     | {"ServerInfo":{"DataCenter":"West Europe","Slice":"E","Ring":"5","ScaleUnit":"004","RoleInstance":"AM2PEPF0000BE44"}}
     | X-BackEndHttpStatus           : 409,409 X-CalculatedFETarget          : PR1P264CU006.internal.outlook.com
     | X-CalculatedBETarget          : PR3P192MB0864.EURP192.PROD.OUTLOOK.COM X-FEServer                    :
     | PR1P264CA0095,AM0PR05CA0085 X-BEServer                    : PAXP192MB1151 Date                          : Tue, 10 Oct
     | 2023 14:36:16 GM
PS /Users/waldek/github/waldekmastykarz/graph-connectors-samples/samples/powershell-markdown>

Please note, that the error only shows the last call failing. Before, there's another request that's failing with a 503 request that's not logged in PowerShell, but which you can see if you use a local proxy like Fiddler.

Module Version
2.6.1

Environment Data

Name Value


PSVersion 7.3.7
PSEdition Core
GitCommitId 7.3.7
OS Darwin 23.0.0 Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:42 PDT 2023; root:xnu-10002.1.13~1/…
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Screenshots

image

See how the value of the additionalProperties property is 1) stored as a string and 2) truncated

Additional context

FWIW, the same approach works just fine with the Graph .NET SDK

@peombwa
Copy link
Member

peombwa commented Oct 10, 2023

Thanks for following up on this.

You will need to store the adaptiveCard as a Hashtable and directly assign it to the layout property of the searchResultTemplate object:

# Initialize to an empty hashtable to explicitly define the type as hashtable.
# This is needed to avoid the breaking change introduced in PowerShell 7.3 - https://github.com/PowerShell/PowerShell/issues/18524.
[hashtable]$adaptiveCard = @{}
$adaptiveCard += Get-Content -Path ".\resultLayout.json" -Raw | ConvertFrom-Json -AsHashtable # Use -AsHashtable to convert the JSON to a hashtable. The code generator exposes additionalProperties, complex types (with no properties) & open types as hashtable types - https://learn.microsoft.com/en-us/powershell/module/microsoft.graph.search/new-mgexternalconnection?view=graph-powershell-1.0#-additionalproperties.
...
searchSettings = @{
    searchResultTemplates = @(
        @{
            id = "waldekblogpwsh"
            priority = 1
            layout = $adaptiveCard # Assign the hashtable directly to the layout property.
        }
    )
}

The code generator, AutoREST.PowerShell, uses hashtable types for additionalProperties, complex types (with no properties) & open types. Truncation will occur when converting PSCustomObject to hashtable as shown by [hashtable]$adaptiveCard = Get-Content -Path $JsonFile -Raw | ConvertFrom-Json (without the exception):
image

The inconsistency between PSObject or Hashtable types is external to the SDK as it emanates from the code generator, AutoREST. The inconsistency is being tracked at #1659.

@sebastienlevert
Copy link

@peombwa should we think about offering a custom "ConvertFrom-Json" that handles these cases? It feels we're leaking some of our implementation / dependency details that are not necessary for developers to know about. Adding @CarolKigoonya to this thread.

@waldekmastykarz
Copy link
Author

Thank you @peombwa! I can confirm that the solution you proposed works.

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

No branches or pull requests

3 participants