Skip to content

Commit d2435ec

Browse files
authored
add enum tip
1 parent 55ffb89 commit d2435ec

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

graph/GuidelinesGraph.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Table of contents
1515
- [Pros and cons](#pros-and-cons)
1616
- [Behavior modeling](#behavior-modeling)
1717
- [Error handling](#error-handling)
18+
- [Enums](#enums)
1819
- [API contract and non-backward compatible changes](#api-contract-and-non-backward-compatible-changes)
1920
- [Versioning and deprecation](#versioning-and-deprecation)
2021
- [Recommended API design patterns](#recommended-api-design-patterns)
@@ -326,6 +327,78 @@ For a complete mapping of error codes to HTTP statuses, see
326327

327328
<a name="api-contract-and-non-backward-compatible-changes"></a>
328329

330+
### Enums
331+
332+
In OData, enum are nominal types representing a subset of the nominal type they use, and are especially useful in cases where certain properties have predefined, limited options.
333+
334+
```xml
335+
<EnumType Name="Color">
336+
<Member Name="Red" Value="0" />
337+
<Member Name="Green" Value="1" />
338+
<Member Name="Blue" Value="2" />
339+
</EnumType>
340+
```
341+
342+
#### Pros
343+
344+
- Our SDK generators will translate the enum to the best representation of the target programming language, resulting in a better developer experience and free client side validation
345+
346+
#### Cons
347+
348+
- Adding a new value requires to go through a (generally fast) API Review
349+
- If the enum is not [evolvable](./patterns/evolvable-enums.md), adding a new value is a breaking change and will generally not be allowed
350+
351+
#### Enum or Booleans
352+
353+
Enumerations are a good alternative to booleans when one of the two values (`true`, `false`) conveyes other possible values not yet conceived. Let's assume we have an `Error` type and a property to communicate how to display it:
354+
355+
```xml
356+
<ComplexType Name="Error">
357+
<Property Name="title" Type="Edm.String" />
358+
<Property Name="message" Type="Edm.String" />
359+
<Property Name="displayAsTip" Type="Edm.Boolean" />
360+
</ComplexType>
361+
```
362+
363+
The `false` value here merely communicates that the error shall not be displayed as a tip. What if, in the future, the error could be displayed as a `tip` or `alert`, and then in a more distant future, a `dialog` option is viable?
364+
365+
With the current model, the only way is to add more boolean properties to convey the new information:
366+
367+
```diff
368+
<ComplexType Name="Error">
369+
<Property Name="title" Type="Edm.String" />
370+
<Property Name="message" Type="Edm.String" />
371+
<Property Name="displayAsTip" Type="Edm.Boolean" />
372+
+ <Property Name="displayAsAlert" Type="Edm.Boolean" />
373+
+ <Property Name="displayAsDialog" Type="Edm.Boolean" />
374+
</ComplexType>
375+
```
376+
377+
Additionally speaking, the workload will now also have to validate the data structure and make sure that only one of the 3 values is `true`
378+
379+
By using an evolvable enum, instead, all we need to do is to add new members:
380+
381+
```diff
382+
<ComplexType Name="Error">
383+
<Property Name="title" Type="Edm.String" />
384+
<Property Name="message" Type="Edm.String" />
385+
+ <Property Name="displayMethod" Type="microsoft.graph.displayMethod" />
386+
- <Property Name="displayAsTip" Type="Edm.Boolean" />
387+
- <Property Name="displayAsAlert" Type="Edm.Boolean" />
388+
- <Property Name="displayAsDialog" Type="Edm.Boolean" />
389+
</ComplexType>
390+
```
391+
392+
```xml
393+
<EnumType Name="displayMethod">
394+
<Member Name="tip" Value="0" />
395+
<Member Name="unknownFutureValue" Value="1" />
396+
<Member Name="alert" Value="2" />
397+
<Member Name="dialog" Value="3" />
398+
</EnumType>
399+
```
400+
401+
329402
## API contract and non-backward compatible changes
330403

331404
The Microsoft Graph definition of breaking changes is based on the

graph/patterns/evolvable-enums.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Microsoft Graph API Design Pattern
44

55
*The evolvable enums pattern allows API producers to extend enumerated types with new members without breaking API consumers.*
66

7+
Note: You might be interested in reading the [Enum guidance](../GuidelinesGraph.md#enums) first
8+
79
## Problem
810

911
Frequently API producers want to add new members to an enum type after it is initially published. Some serialization libraries might fail when they encounter members in an enum type that were added after the serialization model was generated. In this documentation, we refer to any added enum members as unknown.

0 commit comments

Comments
 (0)