-
Notifications
You must be signed in to change notification settings - Fork 2.7k
add guidance for transitioning from a structural collection to a navigation collection #551
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
Open
corranrogue9
wants to merge
22
commits into
vNext
Choose a base branch
from
corranrogue9/structuralcollections
base: vNext
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8841d61
Update collections.md
corranrogue9 e7f42d8
Update collections.md
corranrogue9 3781028
Update collections.md
corranrogue9 6c2789b
Update collections.md
corranrogue9 e7edd54
Update collections.md
corranrogue9 9b32887
Update collections.md
corranrogue9 dd9fd4e
Update collections.md
corranrogue9 2a0091f
Update collections.md
corranrogue9 7823796
Update collections.md
corranrogue9 a59f820
Update collections.md
corranrogue9 9137756
Update collections.md
corranrogue9 d878b73
Update collections.md
corranrogue9 0e22ae8
Update collections.md
corranrogue9 303804f
Update graph/articles/collections.md
corranrogue9 fa9198a
Update collections.md
corranrogue9 560756b
Update collections.md
corranrogue9 bb24f13
Update collections.md
corranrogue9 95063f9
Update collections.md
corranrogue9 cdcb0e3
Update graph/articles/collections.md
corranrogue9 4d8039d
Update graph/articles/collections.md
corranrogue9 114fe12
Update collections.md
corranrogue9 f0fdbd5
Update collections.md
corranrogue9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,3 +316,158 @@ Content-Type: application/json | |
"value": [] | ||
} | ||
``` | ||
|
||
## 11. Collections of structural types (complex types or primitive types) | ||
|
||
Entity types are generally preferred for collections since complex types within a collection cannot be individually referenced. Collections of complex types, including any nested properties, must be updated as a single unit, entirely replacing the existing contents. Even if your API is read-only today, modeling it as a collection of entities gives you more flexibility in referencing individual members now and in the future. | ||
Sometimes, structural collection properties are added to a type and then scenarios are discovered later that require a collection of entity types. | ||
Take the following model with an entity type `application` that has a collection of `keyCredential`s: | ||
|
||
```xml | ||
<EntityType Name="application"> | ||
<Key> | ||
<PropertyRef Name="id" /> | ||
</Key> | ||
<Property Name="id" Type="Edm.String" Nullable="false" /> | ||
<Property Name="keyCredentials" Type="Collection(self.keyCredential)" /> | ||
... | ||
</EntityType> | ||
|
||
<ComplexType Name="keyCredential"> | ||
<Property Name="keyId" Type="Edm.Guid" /> | ||
<Property Name="endDateTime" Type="Edm.DateTimeOffset" /> | ||
... | ||
</ComplexType> | ||
``` | ||
and a scenario arises that requires, for example, to remove individual `keyCredential`s from the collection. | ||
There are two options forward: | ||
|
||
### 11.1 Side-by-side collection properties (for any collection of structural types) | ||
|
||
The model can be updated to have two collections side-by-side, deprecating the existing one: | ||
```diff | ||
<EntityType Name="application"> | ||
<Key> | ||
<PropertyRef Name="id" /> | ||
</Key> | ||
<Property Name="id" Type="Edm.String" Nullable="false" /> | ||
<Property Name="keyCredentials" Type="Collection(self.keyCredential)"> | ||
+ <Annotation Term="Org.OData.Core.V1.Revisions"> | ||
+ <Collection> | ||
+ <Record> | ||
+ <PropertyValue Property = "Date" Date="2020-08-20"/> | ||
+ <PropertyValue Property = "Version" String="2020-08/KeyCredentials"/> | ||
+ <PropertyValue Property = "Kind" EnumMember="Org.OData.Core.V1.RevisionKind/Deprecated"/> | ||
+ <PropertyValue Property = "Description" String="keyCredentials has been deprecated. Please use keyCredentials_v2 instead."/> | ||
+ <PropertyValue Property = "RemovalDate" Date="2022-08-20"/> | ||
+ </Record> | ||
+ </Collection> | ||
+ </Annotation> | ||
+ </Property> | ||
+ <NavigationProperty Name="keyCredentials_v2" Type="Collection(self.keyCredential_v2)" ContainsTarget="true" /> | ||
</EntityType> | ||
|
||
<ComplexType Name="keyCredential"> | ||
<Property Name="keyId" Type="Edm.Guid" /> | ||
<Property Name="endDateTime" Type="Edm.DateTimeOffset" /> | ||
</ComplexType> | ||
|
||
+<EntityType Name="keyCredential_v2"> | ||
+ <Key> | ||
+ <PropertyRef Name="keyId" /> | ||
+ </Key> | ||
+ <Property Name="keyId" Type="Edm.Guid" /> | ||
+ <Property Name="endDateTime" Type="Edm.DateTimeOffset" /> | ||
+</EntityType> | ||
``` | ||
Clients will now be able to refer to individual `keyCredential`s using `keyId` as a key, and they can now remove those `keyCredential`s using `DELETE` requests: | ||
```http | ||
DELETE /applications/{applicationId}/keyCredentials_v2/{some_keyId} | ||
``` | ||
```http | ||
HTTP/1.1 204 No Content | ||
``` | ||
While both properties exist on graph, the expectation is that `keyCredentials` and `keyCredentials_v2` are treated as two "views" into the same data. | ||
To meet this expectation, workloads must: | ||
corranrogue9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1. Keep the properties consistent between `keyCredential` and `keyCredential_v2`. | ||
Any changes to one type must be reflected in the other type. | ||
2. Reject requests that update both collections at the same time. | ||
A request that adds an item to `keyCredentials_v2` while replacing the content of `keyCredentials` must rejected with a `400`, for example: | ||
```http | ||
PATCH /applications/{applicationId} | ||
{ | ||
"keyCredentials": [ | ||
{ | ||
"keyId": "10000000-0000-0000-0000-000000000000", | ||
"endDateTime": "2012-12-03T07:16:23Z" | ||
} | ||
], | ||
"keyCredentials_v2@delta": [ | ||
{ | ||
"keyId": "20000000-0000-0000-0000-000000000000", | ||
"endDateTime": "2012-12-03T07:16:23Z" | ||
} | ||
] | ||
} | ||
``` | ||
```http | ||
HTTP/1.1 400 Bad Request | ||
{ | ||
"error": { | ||
"code": "badRequest", | ||
"message": "'keyCredentials' and 'keyCredentials_v2' cannot be updated in the same request.", | ||
} | ||
``` | ||
|
||
### 11.2 Redefine as Entity Type (for collections of complex types) | ||
|
||
The model can be updated to simply switch the complex type for an entity type: | ||
```diff | ||
<EntityType Name="application"> | ||
<Key> | ||
<PropertyRef Name="id" /> | ||
</Key> | ||
<Property Name="id" Type="Edm.String" Nullable="false" /> | ||
- <Property Name="keyCredentials" Type="Collection(self.keyCredential)" /> | ||
+ <NavigationProperty Name="keyCredentials" Type="Collection(self.keyCredential)" ContainsTarget="true" /> | ||
</EntityType> | ||
|
||
- <ComplexType Name="keyCredential"> | ||
+ <EntityType Name="keyCredential"> | ||
+ <Key> | ||
+ <PropertyRef Name="keyId" /> | ||
+ </Key> | ||
<Property Name="keyId" Type="Edm.Guid" /> | ||
<Property Name="endDateTime" Type="Edm.DateTimeOffset" /> | ||
-</ComplexType> | ||
+</EntityType> | ||
``` | ||
To maintain backwards compatibility **and** compliance with the OData standard, there are several semantic changes that the workload must address: | ||
1. Existing clients would have been able to `$select` the `keyCredentials` property. | ||
Now that `keyCredentials` is a navigation property, the [OData standard](https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#_Toc31361040) specifies that its navigation link be returned when it is `$selected`: | ||
|
||
> If the select item is a navigation property, then the corresponding navigation link is represented in the response. | ||
|
||
Because the previous behavior for `$select=keyCredentials` was to include the collection in the response, and because the standard dictates that the navigation link be included in the response, the new behavior is to include both: | ||
|
||
```http | ||
GET /applications/{applicationId}?$select=keyCredentials | ||
``` | ||
```http | ||
200 OK | ||
{ | ||
"id": "{applicationId}", | ||
"keyCredentials": [ | ||
{ | ||
"keyId": "30000000-0000-0000-0000-000000000000", | ||
"endDateTime": "2012-12-03T07:16:23Z", | ||
... | ||
}, | ||
... | ||
], | ||
"[email protected]": "/applications('{applicationId}')/keyCredentials" | ||
} | ||
``` | ||
|
||
2. The default behavior for structural collections is to include them in the response payload for their containing entity. If this was the behavior of `application` before, it must be preserved by **auto-expanding** the `keyCredentials` property now that it is a navigation property (because the default behavior for navigation properties is to **not** expand them). | ||
3. Structural collections can be updated using a `PATCH` request to the containing entity to replace the entire contents of the collection. If the service supported such updates to the structural collection, then updates to the new navigation property must preserve this behavior. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.