File: 2019-12-24-Overlays.md

package info (click to toggle)
openapi-specification 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,604 kB
  • sloc: javascript: 425; sh: 189; makefile: 4
file content (232 lines) | stat: -rw-r--r-- 10,075 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Overlays

## Metadata

|Tag |Value |
|---- | ---------------- |
|Proposal |[2019-12-24-Overlays](https://github.com/OAI/OpenAPI-Specification/tree/main/proposals/2019-12-24-Overlays.md)|
|Authors|[Darrel Miller](https://github.com/darrelmiller)|
|Status |Proposal|
|Issues |[1442](https://github.com/OAI/OpenAPI-Specification/issues/1442) [1722](https://github.com/OAI/OpenAPI-Specification/issues/1722)|

## Change Log

|Date |Responsible Party |Description |
|---- | ---------------- | ---------- |
| 24th December 2019 | Darrel Miller | Initial draft |
| 2nd January 2019 | Darrel Miller | Update to wording around removing items from arrays.  Added section on backward compatibility. Clarified process around applying a set of updates. Started to add supported scenarios.|
| 29th July 2020 | Darrel Miller | Updated to be explicit about update operations |

## Introduction

In recent months we have been discussing various use cases for overlays and various solutions.  The following proposal takes a somewhat more radical approach to the problem.  It is a more ambitious proposal than the others we have seen before but the additional complexity does allow for supporting many of the scenarios that have been discussed to date.

#### <a name="overlayDocument"></a>Overlay Document

An overlay document contains a list of [Update Objects](#overlayUpdates) that are to be applied to the target document.  Each [Update Object](#updateObject) has a `target` property and a `value` property.  The `target` property is a [JMESPath](http://jmespath.org/specification.html) query that identifies what part of the target document is to be updated and the `value` property contains an object with the properties to be overlaid.


#### <a name="overlayObject"></a>Overlay Object

This is the root object of the [OpenAPI Overlay document](#oasDocument).

##### Fixed Fields

Field Name | Type | Description
---|:---:|---
<a name="overlayVersion"></a>overlay | `string` | Version of the Overlay specification that this document conforms to. 
<a name="overlayInfo"></a>info | [[Info Object](#overlayInfoObject)] | Identifying information about the overlay.
<a name="overlayExtends"></a>extends | `url` | URL to an OpenAPI document this overlay applies to. 
<a name="overlayUpdates"></a>updates | [[Update Object](#updateObject)] | A list of update objects to be applied to the target document.

The list of update objects MUST be applied in sequential order to ensure a consistent outcome.  Updates are applied to the result of the previous updates. This enables objects to be deleted in one update and then re-created in a subsequent update.

The `extends` property can be used to indicate that the Overlay was designed to update a specific OpenAPI description.  This is an optional property.  Where no `extends` is provided it is the responsibility of tooling to apply the Overlay documents to the appropriate OpenAPI description.

#### <a name="overlayInfoObject"></a>Info Object

This object contains identifying information about the [OpenAPI Overlay document](#oasDocument).

##### Fixed Fields

Field Name | Type | Description
---|:---:|---
<a name="overlayTitle"></a>title | `string` | A human readable description of the purpose of the overlay.
<a name="overlayVersion"></a>version | `string` | A version identifier for indicating changes to an overlay document.

#### <a name="updateObject"></a>Update Object

This object represents one or more changes to be applied to the target document at the location defined by the target JMESPath.

##### Fixed Fields

Field Name | Type | Description
---|:---:|---
<a name="updateTarget"></a>target | `string` | A JMESPath expression referencing the target objects in the target document.
<a name="updateAdd"></a>add | [Any](#addObject) | An object to be added as a child of the object(s) referenced by the target. Property has no impact if `remove` property is `true`.
<a name="updateMerge"></a>merge | [Any](#mergeObject) | An object with the properties and values to be merged with the object(s) referenced by the target.  Property has no impact if `remove` property is `true`.
<a name="updateRemove"></a>remove | `boolean` | A boolean value that indicates that the target object is to be removed from the the map or array it is contained in. The default value is false.  

The properties of the merge object MUST be compatible with the target object referenced by the JMESPath key.  When the Overlay document is applied, the properties in the merge object replace properties in the target object with the same name and new properties are appended to the target object.

##### Structured Overlays Example

When updating properties throughout the target document it may be more efficient to create a single `Update Object` that mirrors the structure of the target document. e.g.

```yaml
overlay: 1.0.0
info:
  title: Structured Overlay
  version: 1.0.0
updates:
- target: "@"
  merge:
    info:
      x-overlay-applied: structured-overlay
    paths:
      "/":
        summary: "The root resource"
        get:
          summary: "Retrieve the root resource"
          x-rate-limit: 100
      "/pets":
        get:
          summary: "Retrieve a list of pets"
          x-rate-limit: 100
    components:
    tags:
```

##### Targeted Overlays

Alternatively, where only a small number of updates need to be applied to a large document, each [Update Object](#updateObject) can be more targeted.

```yaml
overlay: 1.0.0
info:
  title: Targeted Overlays
  version: 1.0.0
updates:
- target: paths."/foo".get
  merge:
    description: This is the new description
- target: paths."/bar".get
  merge:
    description: This is the updated description
- target: paths."/bar"
  merge:
      post:
          description: This is an updated description of a child object
          x-safe: false
```

##### Wildcard Overlays Examples

One significant advantage of using the JMESPath syntax that it allows referencing multiple nodes in the target document.  This would allow a single update object to be applied to multiple target objects using wildcards.

```yaml
overlay: 1.0.0
info:
  title: Update many objects at once
  version: 1.0.0
updates:
- target: paths.*.get
  merge:
    x-safe: true
- target: paths.*.get.parameters[?name=='filter' && in=='query']
  merge:
    schema:
      $ref: "/components/schemas/filterSchema"
```

##### Array Modification Examples

Due to the fact that we can now reference specific elements of the parameter array, it allows adding parameters. Parameters can be deleted using the `remove` property.  Use of indexes to remove array items should be avoided where possible as indexes will change when items are removed.

```yaml
overlay: 1.0.0
info:
  title: Add an array element
  version: 1.0.0
updates:
- target: paths.*.get.parameters
  add:
    name: newParam
    in: query
```

```yaml
overlay: 1.0.0
info:
  title: Remove a array element
  version: 1.0.0
updates:
- target: paths[*].get.parameters[? name == 'dummy']
  remove: true
```

##### Traits Examples

By annotating an OpenAPI description using extension such as `x-oai-traits` an author of OpenAPI description can identify where overlay updates should be applied.

```yaml
openapi: 3.1.0
info:
  title: Api with a paged collection
  version: 1.0.0
paths:
  /items:
    get:
      x-oai-traits: ["paged"]
      responses:
        200:
          description: OK
```

With the above OpenAPI description, following Overlay document will apply the necessary updates to describe how paging is implemented, where that trait has been applied.

```yaml
overlay: 1.0.0
info:
  title: Apply Traits
  version: 1.0.0
updates:
- target: $.paths[*].get[?contains(x-traits,'paged')]
  merge:
    parameters:
      - name: top
        in: query
      - name: skip
        in: query
```

This approach allows flipping control of where Overlays apply updates to the OpenAPI description itself.

## Proposal Summary

### Benefits

- This approach addresses the two distinct approaches of structured overlay vs targeted overlay which suits distinct but equally valid scenarios.
- Addresses the problem of modifying the parameters array and removes the need to replace the entire array when a small change is required.
- Allows sets of related overlays to be stored in a same file.
- Enables updating a set of objects based on a pattern. This might be an effective way of apply common behaviour across many operations in an API.

### Challenges

- Tooling will need a JMESPath implementation.
- Large overlays may be slow to process.
- Multiple complex pattern based overlays may cause overlapping updates causing confusing outcomes.

## Alternatives considered

JMESPath was chosen over JSONPath due to the fact that JMESPath has a [specification](http://jmespath.org/specification.html) and a set of test cases.  This will help to ensure compatibility between implementations.

## Backwards compatibility

Overlays will be described in a new specification that can be used alongside an OpenAPI Description, therefore there will be no compatibility issues for the initial release. Overlay documents can be used against OpenAPI v2 and v3 descriptions.

## Scenarios Considered

- Multi-language support.  An Overlay document for each language is used to target a specific OpenAPI description.  The Overlay document will likely use a duplicate structure to the original OpenAPI description and replace all `description` properties.
- Applying API wide standards.  An Overlay document contains update objects that describe standard headers, parameters, responses.  These documents would use JMESPath queries to target the appropriate objects in the OpenAPI description.  Tooling could be used to target the OpenAPI description rather than using extends.
- Add tool specific OpenAPI metadata. Overlay adds additional metadata such as SLA information, client codegen hints or middleware policies. Using Overlays to manage this data separately is valuable when there is a different audience for the data and/or there the information has different sensitivity levels.