Skip to content

Commit 577874d

Browse files
authored
Merge pull request #551 from microsoft/corranrogue9/structuralcollections
add guidance for transitioning from a structural collection to a navigation collection
2 parents 4af08cf + f0fdbd5 commit 577874d

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

graph/articles/collections.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,158 @@ Content-Type: application/json
316316
"value": []
317317
}
318318
```
319+
320+
## 11. Collections of structural types (complex types or primitive types)
321+
322+
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.
323+
Sometimes, structural collection properties are added to a type and then scenarios are discovered later that require a collection of entity types.
324+
Take the following model with an entity type `application` that has a collection of `keyCredential`s:
325+
326+
```xml
327+
<EntityType Name="application">
328+
<Key>
329+
<PropertyRef Name="id" />
330+
</Key>
331+
<Property Name="id" Type="Edm.String" Nullable="false" />
332+
<Property Name="keyCredentials" Type="Collection(self.keyCredential)" />
333+
...
334+
</EntityType>
335+
336+
<ComplexType Name="keyCredential">
337+
<Property Name="keyId" Type="Edm.Guid" />
338+
<Property Name="endDateTime" Type="Edm.DateTimeOffset" />
339+
...
340+
</ComplexType>
341+
```
342+
and a scenario arises that requires, for example, to remove individual `keyCredential`s from the collection.
343+
There are two options forward:
344+
345+
### 11.1 Side-by-side collection properties (for any collection of structural types)
346+
347+
The model can be updated to have two collections side-by-side, deprecating the existing one:
348+
```diff
349+
<EntityType Name="application">
350+
<Key>
351+
<PropertyRef Name="id" />
352+
</Key>
353+
<Property Name="id" Type="Edm.String" Nullable="false" />
354+
<Property Name="keyCredentials" Type="Collection(self.keyCredential)">
355+
+ <Annotation Term="Org.OData.Core.V1.Revisions">
356+
+ <Collection>
357+
+ <Record>
358+
+ <PropertyValue Property = "Date" Date="2020-08-20"/>
359+
+ <PropertyValue Property = "Version" String="2020-08/KeyCredentials"/>
360+
+ <PropertyValue Property = "Kind" EnumMember="Org.OData.Core.V1.RevisionKind/Deprecated"/>
361+
+ <PropertyValue Property = "Description" String="keyCredentials has been deprecated. Please use keyCredentials_v2 instead."/>
362+
+ <PropertyValue Property = "RemovalDate" Date="2022-08-20"/>
363+
+ </Record>
364+
+ </Collection>
365+
+ </Annotation>
366+
+ </Property>
367+
+ <NavigationProperty Name="keyCredentials_v2" Type="Collection(self.keyCredential_v2)" ContainsTarget="true" />
368+
</EntityType>
369+
370+
<ComplexType Name="keyCredential">
371+
<Property Name="keyId" Type="Edm.Guid" />
372+
<Property Name="endDateTime" Type="Edm.DateTimeOffset" />
373+
</ComplexType>
374+
375+
+<EntityType Name="keyCredential_v2">
376+
+ <Key>
377+
+ <PropertyRef Name="keyId" />
378+
+ </Key>
379+
+ <Property Name="keyId" Type="Edm.Guid" />
380+
+ <Property Name="endDateTime" Type="Edm.DateTimeOffset" />
381+
+</EntityType>
382+
```
383+
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:
384+
```http
385+
DELETE /applications/{applicationId}/keyCredentials_v2/{some_keyId}
386+
```
387+
```http
388+
HTTP/1.1 204 No Content
389+
```
390+
While both properties exist on graph, the expectation is that `keyCredentials` and `keyCredentials_v2` are treated as two "views" into the same data.
391+
To meet this expectation, workloads must:
392+
1. Keep the properties consistent between `keyCredential` and `keyCredential_v2`.
393+
Any changes to one type must be reflected in the other type.
394+
2. Reject requests that update both collections at the same time.
395+
A request that adds an item to `keyCredentials_v2` while replacing the content of `keyCredentials` must rejected with a `400`, for example:
396+
```http
397+
PATCH /applications/{applicationId}
398+
{
399+
"keyCredentials": [
400+
{
401+
"keyId": "10000000-0000-0000-0000-000000000000",
402+
"endDateTime": "2012-12-03T07:16:23Z"
403+
}
404+
],
405+
"keyCredentials_v2@delta": [
406+
{
407+
"keyId": "20000000-0000-0000-0000-000000000000",
408+
"endDateTime": "2012-12-03T07:16:23Z"
409+
}
410+
]
411+
}
412+
```
413+
```http
414+
HTTP/1.1 400 Bad Request
415+
{
416+
"error": {
417+
"code": "badRequest",
418+
"message": "'keyCredentials' and 'keyCredentials_v2' cannot be updated in the same request.",
419+
}
420+
```
421+
422+
### 11.2 Redefine as Entity Type (for collections of complex types)
423+
424+
The model can be updated to simply switch the complex type for an entity type:
425+
```diff
426+
<EntityType Name="application">
427+
<Key>
428+
<PropertyRef Name="id" />
429+
</Key>
430+
<Property Name="id" Type="Edm.String" Nullable="false" />
431+
- <Property Name="keyCredentials" Type="Collection(self.keyCredential)" />
432+
+ <NavigationProperty Name="keyCredentials" Type="Collection(self.keyCredential)" ContainsTarget="true" />
433+
</EntityType>
434+
435+
- <ComplexType Name="keyCredential">
436+
+ <EntityType Name="keyCredential">
437+
+ <Key>
438+
+ <PropertyRef Name="keyId" />
439+
+ </Key>
440+
<Property Name="keyId" Type="Edm.Guid" />
441+
<Property Name="endDateTime" Type="Edm.DateTimeOffset" />
442+
-</ComplexType>
443+
+</EntityType>
444+
```
445+
To maintain backwards compatibility **and** compliance with the OData standard, there are several semantic changes that the workload must address:
446+
1. Existing clients would have been able to `$select` the `keyCredentials` property.
447+
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`:
448+
449+
> If the select item is a navigation property, then the corresponding navigation link is represented in the response.
450+
451+
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:
452+
453+
```http
454+
GET /applications/{applicationId}?$select=keyCredentials
455+
```
456+
```http
457+
200 OK
458+
{
459+
"id": "{applicationId}",
460+
"keyCredentials": [
461+
{
462+
"keyId": "30000000-0000-0000-0000-000000000000",
463+
"endDateTime": "2012-12-03T07:16:23Z",
464+
...
465+
},
466+
...
467+
],
468+
"[email protected]": "/applications('{applicationId}')/keyCredentials"
469+
}
470+
```
471+
472+
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).
473+
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.

0 commit comments

Comments
 (0)