-
Notifications
You must be signed in to change notification settings - Fork 349
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
Add DependsOnId on Add Related Object API #3079
base: main
Are you sure you want to change the base?
Conversation
@DButoyez please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
...ests/Tests/Client/Microsoft.OData.Client.E2E.Tests/Common/Server/Default/DefaultDataModel.cs
Outdated
Show resolved
Hide resolved
...Tests/Tests/Client/Microsoft.OData.Client.E2E.Tests/Common/Server/Default/DefaultEdmModel.cs
Show resolved
Hide resolved
.../EndToEndTests/Tests/Client/Microsoft.OData.Client.E2E.Tests/Batch/Server/BanksController.cs
Outdated
Show resolved
Hide resolved
Assert.Equal(201, (response.First() as ChangeOperationResponse).StatusCode); | ||
Assert.Equal(201, (response.Last() as ChangeOperationResponse).StatusCode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert.Equal(201, (response.First() as ChangeOperationResponse).StatusCode); | |
Assert.Equal(201, (response.Last() as ChangeOperationResponse).StatusCode); | |
Assert.Equal(201, Assert.IsType<ChangeOperationResponse>(response.First()).StatusCode); | |
Assert.Equal(201, Assert.IsType<ChangeOperationResponse>(response.Last()).StatusCode); |
- safer - avoid
NullReferenceException
in your test - neater
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I addition, I suggest you add an assert statement above the two statements to verify that result
has at least two elements, otherwise .First()
can also be the .Last()
. We want to confirm that both POST requests for Bank and BankAccount happened
@@ -2634,7 +2634,8 @@ public virtual void AddRelatedObject(object source, string sourceProperty, objec | |||
var targetResource = new EntityDescriptor(this.model) | |||
{ | |||
Entity = target, | |||
State = EntityStates.Added | |||
State = EntityStates.Added, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified that the payload looks good:
{
"requests": [
{
"id": "1",
"atomicityGroup": "22d9a80c-30e8-4546-9ac8-1f2c4c5369fa",
"method": "POST",
"url": "http://localhost/odata/Banks",
"headers": {
"odata-version": "4.0",
"odata-maxversion": "4.0",
"content-type": "application/json;odata.metadata=minimal",
"accept": "application/json;odata.metadata=minimal",
"accept-charset": "UTF-8",
"user-agent": "Microsoft.OData.Client/8.0.1"
},
"body": {
"Id": 45,
"Location": "KE",
"Name": "Test Bank"
}
},
{
"id": "2",
"atomicityGroup": "22d9a80c-30e8-4546-9ac8-1f2c4c5369fa",
"dependsOn": [
"1"
],
"method": "POST",
"url": "http://localhost/odata/$1/BankAccounts",
"headers": {
"odata-version": "4.0",
"odata-maxversion": "4.0",
"content-type": "application/json;odata.metadata=minimal",
"accept": "application/json;odata.metadata=minimal",
"accept-charset": "UTF-8",
"user-agent": "Microsoft.OData.Client/8.0.1"
},
"body": {
"AccountNumber": "4567890",
"BankId": 45,
"Id": 890
}
}
]
}
} | ||
} | ||
|
||
class Container : DataServiceContext |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DataServiceContext
generated by OData Connected Service would contain methods with the following signatures:
public virtual void AddToBanks(Bank bank)
{
base.AddObject("Banks", bank);
}
public virtual void AddToBankAccounts(BankAccount bankAccount)
{
base.AddObject("BankAccounts", bankAccount);
}
The [more common]/alternative way that people accomplish this that doesn't involve calling AddObject
and AddRelatedObject
directly is the following:
var bank = new Bank { /***/ };
var bankAccount = new BankAccount { /***/ };
_context.AddToBanks(bank);
_context.AddToBankAccounts(bankAccount);
// THEN
_context.AddLink(source: bank, sourceProperty: "BankAccounts", target: bankAccount); // Where "sourceProperty" MUST be a collection-valued navigation property
// Or
_context.SetLink(source: bankAccount, sourceProperty: "Bank", target: bank); // Where "sourceProperty" MUST be a single-valued navigation property
You may want to add another test that does things this way to confirm that it works as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might not work as the change is mostly geared towards the AddRelatedObject for the AddLink and SetLink APIs we would need to also implement a fix for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DButoyez Did you try it out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be surprised to find that they work as expected. But you'd only know for certain if you tried it out practically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AddLink
method doesn't automatically populate the dependsOnID on the targert resource a similar change will have to be made:
I have replicated using the below code and I'm getting the dependsOnID exception:
Request Id reference [1] in Uri [$1/BankAccounts/$ref] is not found in effective depends-on-Ids [] of the request
public async Task JsonBatchSequencingSingeChangeSetTestAddLink()
{
// Create new BankAccounts object
var bank = new Bank
{
Id = 45,
Name = "Test Bank",
Location = "KE",
BankAccounts = new List<BankAccount>()
};
// Create new BankAccount object
var bankAccount = new BankAccount
{
Id = 890,
AccountNumber = "4567890",
BankId = bank.Id,
Bank = bank
};
_context.AddObject("Banks", bank);
_context.AddObject("BankAccounts", bankAccount);
// THEN
_context.AddLink(source: bank, sourceProperty: "BankAccounts", target: bankAccount);
// Save both entities in a single batch request using JSON
var response = await _context.SaveChangesAsync(SaveChangesOptions.BatchWithSingleChangeset | SaveChangesOptions.UseJsonBatch);
Assert.Equal(2, response.Count());
var bankResponse = response.First() as ChangeOperationResponse;
var bankAccountResponse = response.Last() as ChangeOperationResponse;
Assert.NotNull(bankResponse);
Assert.NotNull(bankAccountResponse);
Assert.Equal(201, bankResponse.StatusCode);
Assert.Equal(201, bankAccountResponse.StatusCode);
}
Issues
This pull request fixes ##3056
Description
This change request aims to add support for dependsOnID to the AddRelatedObject API in order to enable saving an object using SaveChangesOptions.BatchWithSingleChangeset | SaveChangesOptions.UseJsonBatch.
Checklist (Uncheck if it is not completed)
Additional work necessary
If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.