Skip to content

Commit 3918ab1

Browse files
author
Doug Davis
authored
Merge pull request #353 from duglin/primerExtensions
Add some rationale for our extensions decision
2 parents 3fd5bc0 + ba8ad6f commit 3918ab1

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

json-format.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ specification does not explicitly map each attribute, but
4343
provides a generic mapping model that applies to all current and future
4444
CloudEvents attributes, including extensions.
4545

46+
For clarity, extension attributes are serialized using the same rules as
47+
specification defined attributes. This includes their syntax and placement
48+
within the JSON object. In particular, extensions are placed as top-level
49+
JSON properties. Extensions themselves are free to have nested properties,
50+
however the root of the extension MUST be serialized as a top-level JSON
51+
property. There were many reason for this design decision and they are covered
52+
in more detail in the [Primer](primer.md#json-extensions).
53+
4654
### 2.1. Base Type System
4755

4856
The core [CloudEvents specification][CE] defines a minimal abstract type
@@ -110,7 +118,7 @@ The following table shows exemplary mappings:
110118
| data | Binary | "Q2xvdWRFdmVudHM="
111119
| data | Map | { "objA" : "vA", "objB", "vB" }
112120

113-
## 2.5. JSONSchema Validation
121+
### 2.5. JSONSchema Validation
114122

115123
The CloudEvents [JSONSchema](http://json-schema.org) for the spec is located
116124
[here](spec.json) and contains the definitions for validating events in JSON.

primer.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,52 @@ consider moving it into the specification as a core attribute. This means
177177
that the extension mechanism/process can be used as a way to vet new
178178
attributes prior to formally adding them to the specification.
179179

180+
### JSON Extensions
181+
182+
As mentioned in the [Attributes](json-format.md#2-attributes) section of the
183+
[JSON Event Format for CloudEvents](json-format.md) specificatinon,
184+
CloudEvent extension attributes are serialized as siblings to the specification
185+
defined attributes - meaning, at the top-level of the JSON object. The
186+
authors of the specification spent a long time considering all options
187+
and decided that this was the best choice. Some of the rationale follows.
188+
189+
Since the specifications follow [semver](https://semver.org/), this
190+
means that new properties can be defined by future versions of the
191+
core specification without requiring a major version number change - as
192+
long as these properties are optional. In those cases, consider what an
193+
existing consumer would do with a new (unknown) top-level property. While
194+
it would be free to ignore it, since it is optional, in most cases it is
195+
believed that these properties would still want to be exposed to the
196+
application receiving those events. This would allow those applications to
197+
support these properties even if the infrastructure doesn't. This means
198+
that unknown top-level properties (regardless of who defined them - future
199+
versions of the spec or the event producer) are probably not going to be
200+
ignored. So, while some other specifications define a specific property
201+
underwhich extensions are placed (e.g. a top-level `extensions` property), the
202+
authors decided that having two different locations within an
203+
incoming event for unknown properties could lead to interoperability issues
204+
and confusion for developers.
205+
206+
Often extensions are used to test new potential properties of
207+
specifications prior to them being formally adopted. If there were an
208+
`extensions` type of property, in which this new property was serialized,
209+
then if that property were to ever be adopted by the core specification it
210+
would be promoted (from a serialization perspective) from the `extensions`
211+
property to be a top-level property. If we assume that this new property
212+
will be optional, then as it is adopted by the core specification it will
213+
be just a minor version increment, and all existing consumers should still
214+
continue to work. However, consumers will not know where this property
215+
will appear - in the `extensions` property or as a top-level property.
216+
This means they might need to look in both places. What if the property
217+
appears in both place but with different values? Will producers need to place
218+
it in both places since they could have old and new consumers? While
219+
it might be possible to define clear rules for how to solve each of the
220+
potential problems that arise, the authors decided that it would be better
221+
to simply avoid all of them in the first place by only having one location
222+
in the serialization for unkown, or even new, properties. It was also
223+
noted that the HTTP specification is now following a similar pattern by
224+
no longer suggesting that extension HTTP headers be prefixed with `X-`.
225+
180226
## Qualifying Protocols and Encodings
181227

182228
The explicit goal of the CloudEvents effort, as expressed in the specification,

spec.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ both appear within the same JSON object.
142142
### Extension Attributes
143143
CloudEvents producers MAY include additional context attributes in the event
144144
that might be used in ancillary actions related to the processing of the event.
145-
See [CloudEvent Attributes Extensions](primer.md#cloudevent-attribute-extensions)
145+
See
146+
[CloudEvent Attributes Extensions](primer.md#cloudevent-attribute-extensions)
146147
for additional information concerning the use and definition of extensions.
147148

148149
This specification places no restriction on the type or semantics of the

0 commit comments

Comments
 (0)