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

Batch request Content-ID reference #560

Open
twastvedt opened this issue Nov 30, 2018 · 3 comments
Open

Batch request Content-ID reference #560

twastvedt opened this issue Nov 30, 2018 · 3 comments
Assignees

Comments

@twastvedt
Copy link

I believe there is an error in the formation of URIs which reference Content-IDs from earlier batch operations. I'm not sure if this is a problem with Simple.OData or Microsoft.Data.OData. Here's the code and resulting content of one of my batch requests:

var batch = new ODataBatch(client, true);
var quote = new
{
	name = "Quote name",
	customerid_contact = new { contactid = crmContact.contactid },
	opportunityid = new { opportunityid = opportunityGUID },
};

batch += async c => await c.For<CRM.quote>()
	.Set(quote)
	.InsertEntryAsync(false);

batch += c => c.For<CRM.quotedetail>()
	.Set(new
	{
		ispriceoverridden = true,
		priceperunit = QuoteValue ?? 0,
		productid = crmProduct,
		quantity = 1,
		quoteid = quote,
		uomid = new CRM.uom { uomid = crmProduct.defaultuomid.uomid }
	})
	.InsertEntryAsync(false);

await batch.ExecuteAsync();

Content (added formatting to the json for clarity):


POST https://endpoint.crm.dynamics.com/api/data/v9.0/$batch HTTP/1.1
Authorization: Bearer 
Content-Type: multipart/mixed; boundary=batch_ff13d4e0-303f-4fdb-9a47-8f75f1d5b3be
Host: endpoint.crm.dynamics.com
Cookie: ApplicationGatewayAffinity=
Content-Length: 1893
Expect: 100-continue

--batch_ff13d4e0-303f-4fdb-9a47-8f75f1d5b3be
Content-Type: multipart/mixed; boundary=changeset_bfcd03d6-62c9-411c-afd4-9a2262e08ed5

--changeset_bfcd03d6-62c9-411c-afd4-9a2262e08ed5
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST https://endpoint.crm.dynamics.com/api/data/v9.0/quotes HTTP/1.1
Content-ID: 1
Prefer: return=minimal
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{
    "@odata.type":"#Microsoft.Dynamics.CRM.quote",
    "name":"Quote name",
    "[email protected]":"https://endpoint.crm.dynamics.com/api/data/v9.0/contacts(3dadff3e-ba2e-e811-814b-e0071b72a7d1)",
    "[email protected]":"https://endpoint.crm.dynamics.com/api/data/v9.0/opportunities(52d71b27-04b4-e711-810e-e0071b711c21)"
}
--changeset_bfcd03d6-62c9-411c-afd4-9a2262e08ed5
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

POST https://endpoint.crm.dynamics.com/api/data/v9.0/quotedetails HTTP/1.1
Content-ID: 2
Prefer: return=minimal
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{
    "@odata.type":"#Microsoft.Dynamics.CRM.quotedetail",
    "ispriceoverridden":true,
    "priceperunit":59132.0000,
    "quantity":1,
    "[email protected]":"https://endpoint.crm.dynamics.com/api/data/v9.0/products(b60acac5-8bcd-e811-a95c-000d3a3ac3f8)",
    "[email protected]":"https://endpoint.crm.dynamics.com/api/data/v9.0/$1",
    "[email protected]":"https://endpoint.crm.dynamics.com/api/data/v9.0/uoms(820ec296-d20e-4f22-a6c9-92089defb90e)"
}
--changeset_bfcd03d6-62c9-411c-afd4-9a2262e08ed5--
--batch_ff13d4e0-303f-4fdb-9a47-8f75f1d5b3be--

According to this answer, and a successful manual test in Postman, the content-id references are absolute and so the bold line above should be:

Interestingly, this is how the URL is formatted in the wiki.

@object
Copy link
Member

object commented Dec 2, 2018

@twastvedt thank you for the observation. We don't control details of writing batch operation payload, internally the library calls either Microsoft.Data.OData (OData V1-3) or Microsoft.OData.Core (OData V4). This is the code that writes OData V4 payload:
https://github.com/object/Simple.OData.Client/blob/master/src/Simple.OData.Client.V4.Adapter/RequestWriter.cs

So I am not sure whether it's possible to change the format of the value you highlighted.

@object object self-assigned this Dec 2, 2018
@twastvedt
Copy link
Author

OK, thanks for the info. When I get a chance I'll try to trace the writing of a batch request and figure out where that URL is getting created.

jods4 added a commit to jods4/Simple.OData.Client that referenced this issue May 22, 2023
@jods4
Copy link

jods4 commented May 22, 2023

I encountered this bug and look in the code a bit.
The cause of this bug is not in MS.Data.OData but in this package. There's a line that builds an absolute URI from $1.

PR with a suggested fix: #903.

Would love a patched release, it's currently impossible to reference entities in changeset.

Note
Bonus chatter, if you attempt to use change-id references.
It only works if the referenced POST does not have Prefer: return=representation.
In Simple.OData.Client, this means you must call InsertEntryAsync(resultRequired: false), otherwise your request will fail.

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