MSGraph-SDK-Code-Generator icon indicating copy to clipboard operation
MSGraph-SDK-Code-Generator copied to clipboard

EntityCollectionReferencesRequest.cs.tt generates incorrect reference URL

Open DCourtel opened this issue 5 years ago • 6 comments

I try to assign a Win32LoBApp to a category using the GraphServiceClient library (3.20). I use this code for example:

var allApps = await graphClient.DeviceAppManagement.MobileApps.Request().Filter("isOf('microsoft.graph.win32LobApp')");
var allCategories = await graphClient.DeviceAppManagement.MobileAppCategories.Request().GetAsync();
await graphClient.DeviceAppManagement.MobileApps[allApps[0].id]
                    .Categories
                    .References
                    .Request()
                    .AddAsync(allCategories[2]);

The third line throws an exception (ServiceException) Error Message:

Code: BadRequest Message: Resource not found for the segment 'categories'.

Any help would be appreciated. AB#7094

DCourtel avatar Nov 22 '20 09:11 DCourtel

Can you share the document you used to help form the request? Can you capture and sanitize the request and response and share it with us? A requestId and timestamp would be helpful as well. Thank you.

MIchaelMainer avatar Dec 03 '20 21:12 MIchaelMainer

FYI - the Graph.Community library contains a Logging middleware that will capture the request/response:

https://github.com/microsoftgraph/msgraph-sdk-dotnet-contrib/blob/master/samples/ImmutableIds.cs

pschaeflein avatar Dec 04 '20 13:12 pschaeflein

Hi, I realize that I didn’t update the description after updating the code example. The error is thrown at the third line of code when adding the category to the App. This is the RequestId and Timestamp:

"innerError": {
      "date": "2020-12-06T17:34:10",
      "request-id": "01eed349-a9c3-4631-8acc-2ea77fb61718",
      "client-request-id": "01eed349-a9c3-4631-8acc-2ea77fb61718"
    }

Here a capture made with Fiddler: Fiddler capture Add Category to an App.txt

Thanks.

DCourtel avatar Dec 06 '20 18:12 DCourtel

We are generating the incorrect reference URL.

Actual

POST https://graph.microsoft.com/v1.0/deviceAppManagement/mobileApps/8691b9c4-da35-47fe-9177-17d0b6c441d0/categories/$ref
{
  "@odata.id": "https://graph.microsoft.com/v1.0/deviceAppManagement/categories/2ae868f2-5b00-4473-a116-570800b0d331"
}

Expected

POST https://graph.microsoft.com/v1.0/deviceAppManagement/mobileApps/8691b9c4-da35-47fe-9177-17d0b6c441d0/categories/$ref
{
  "@odata.id": "https://graph.microsoft.com/v1.0/deviceAppManagement/mobileAppCategories/2ae868f2-5b00-4473-a116-570800b0d331"
}

The issue is here: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/src/Microsoft.Graph/Generated/requests/MobileAppCategoriesCollectionReferencesRequest.cs#L62

Bug in EntityCollectionReferencesRequest.cs.tt. We are making an incorrect assumption on the canonical reference to an item.

MIchaelMainer avatar Dec 07 '20 19:12 MIchaelMainer

Workaround until this is fixed.

var allApps = await client.DeviceAppManagement.MobileApps.Request().Filter("isOf('microsoft.graph.win32LobApp')").GetAsync();
var allCategories = await client.DeviceAppManagement.MobileAppCategories.Request().GetAsync();
var hrm = client.DeviceAppManagement.MobileApps["mobileAppId"].Categories.References.Request().GetHttpRequestMessage();
hrm.Method = HttpMethod.Post;
hrm.Content = new StringContent($"{{\"@odata.id\": \"https://graph.microsoft.com/v1.0/deviceAppManagement/mobileAppCategories/{allCategories[2]}\"}}");
var response = await client.HttpProvider.SendAsync(hrm);

MIchaelMainer avatar Dec 07 '20 21:12 MIchaelMainer

Thanks Michael, I had to change the new StringContent line to this:

hrm.Content = new StringContent($"{{\"@odata.id\": \"https://graph.microsoft.com/v1.0/deviceAppManagement/mobileAppCategories/{allCategories[2].Id}\"}}",
                                  Encoding.Unicode, 
                                  "application/json");

To have it works.

I hope to see the definitive fix soon.

DCourtel avatar Dec 08 '20 08:12 DCourtel