Skip to content

Commit b04a0ae

Browse files
authored
Merge pull request #433 from mikekistler/polymorphism
Add guidelines for polymorphism
2 parents 2f96c45 + 4559735 commit b04a0ae

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

azure/Guidelines.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ Please ensure that you add an anchor tag to any new guidelines that you add and
1111

1212
## History
1313

14+
<details>
15+
<summary>Expand change history</summary>
16+
1417
| Date | Notes |
1518
| ----------- | -------------------------------------------------------------- |
16-
| 2022-Sep-07 | Updated URL guidelines for DNS Done Right | |
19+
| 2023-Apr-07 | Update/clarify guidelines on polymorphism |
20+
| 2022-Sep-07 | Updated URL guidelines for DNS Done Right |
1721
| 2022-Jul-15 | Update guidance on long-running operations |
1822
| 2022-May-11 | Drop guidance on version discovery |
1923
| 2022-Mar-29 | Add guidelines about using durations |
@@ -24,6 +28,8 @@ Please ensure that you add an anchor tag to any new guidelines that you add and
2428
| 2020-Jul-31 | Added service advice for initial versions |
2529
| 2020-Mar-31 | 1st public release of the Azure REST API Guidelines |
2630

31+
</details>
32+
2733
## Introduction
2834

2935
These guidelines apply to Azure service teams implementing _data plane_ APIs. They offer prescriptive guidance that Azure service teams MUST follow ensuring that customers have a great experience by designing APIs meeting these goals:
@@ -492,6 +498,12 @@ This indicates to client libraries and customers that values of the enumeration
492498
<a name="json-document-extensible-enums"></a>
493499
:white_check_mark: **DO** document to customers that new values may appear in the future so that customers write their code today expecting these new values tomorrow.
494500

501+
<a name="json-return-extensible-enum-value"></a>
502+
:heavy_check_mark: **YOU MAY** return a value for an extensible enum that is not one of the values defined for the api-version specified in the request.
503+
504+
<a name="json-accept-extensible-enum-value"></a>
505+
:warning: **YOU SHOULD NOT** accept a value for an extensible enum that is not one of the values defined for the api-version specified in the request.
506+
495507
<a name="json-removing-enum-value-is-breaking"></a>
496508
:no_entry: **DO NOT** remove values from your enumeration list as this breaks customer code.
497509

@@ -503,9 +515,9 @@ This indicates to client libraries and customers that values of the enumeration
503515
If you can't avoid them, then follow the guideline below.
504516

505517
<a name="json-use-discriminator-for-polymorphism"></a>
506-
:white_check_mark: **DO** define a `kind` field indicating the kind of the resource and include any kind-specific fields in the body.
518+
:white_check_mark: **DO** define a discriminator field indicating the kind of the resource and include any kind-specific fields in the body.
507519

508-
Below is an example of JSON for a Rectangle and Circle:
520+
Below is an example of JSON for a Rectangle and Circle with a discriminator field named `kind`:
509521
**Rectangle**
510522
```json
511523
{
@@ -540,6 +552,22 @@ Below is an example of JSON for a Rectangle and Circle:
540552
```
541553
Both Rectangle and Circle have common fields: `kind`, `fillColor`, `lineColor`, and `subscription`. A Rectangle also has `x`, `y`, `width`, and `length` while a Circle has `x`, `y`, and `radius`. The `subscription` is a nested polymorphic type. A `free` subscription has no additional fields and a `paid` subscription has `expiration` and `invoice` fields.
542554

555+
The [Azure Naming Guidelines](./ConsiderationsForServiceDesign.md#common-names) recommend that the discriminator field be named `kind`.
556+
557+
<a name="json-polymorphism-kind-extensible"></a>
558+
:ballot_box_with_check: **YOU SHOULD** define the discriminator field of a polymorphic type to be an extensible enum.
559+
560+
<a name="json-polymorphism-kind-immutable"></a>
561+
:warning: **YOU SHOULD NOT** allow an update (patch) to change the discriminator field of a polymorphic type.
562+
563+
<a name="json-polymorphism-versioning"></a>
564+
:warning: **YOU SHOULD NOT** return properties of a polymorphic type that are not defined for the api-version specified in the request.
565+
566+
<a name="json-polymorphism-arrays"></a>
567+
:warning: **YOU SHOULD NOT** have a property of an updatable resource whose value is an array of polymorphic objects.
568+
569+
Updating an array property with JSON merge-patch is not version-resilient if the array contains polymorphic types.
570+
543571
## Common API Patterns
544572

545573
<a name="actions"></a>

0 commit comments

Comments
 (0)