Skip to content

Commit e7324a7

Browse files
committed
Provide guidance on null in XML.
There really isn't a native `null` type in XML, as both elements and attributes that are empty have an empty string value. We also need to leave the behavior implementation-defined for compatibility. However, the `xsi:nil` attribute is the closest thing to a `null` element. Attributes are harder, and the best I can come up with is letting `null` behave the same as an omitted attribute for the purpose of serialization.
1 parent 630cc2a commit e7324a7

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/oas.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,21 @@ The `namespace` field is intended to match the syntax of [XML namespaces](https:
31443144
* Versions 3.1.0, 3.0.3, and earlier of this specification erroneously used the term "absolute URI" instead of "non-relative URI" ("non-relative IRI" as of OAS v3.2.0), so authors using namespaces that include a fragment should check tooling support carefully.
31453145
* XML allows but discourages relative IRI-references, while this specification outright forbids them.
31463146

3147+
##### Handling `null` Values
3148+
3149+
XML does not, by default, have a concept equivalent to `null`, and to preserve compatibility with version 3.1.1 and earlier of this specification, the behavior of serializing `null` values is implementation-defined.
3150+
3151+
However, implementations SHOULD handle `null` values as follows:
3152+
3153+
* For elements, produce an empty element with an `xsi:nil="true"` attribute.
3154+
* For attributes, omit the attribute.
3155+
3156+
Note that for attributes, this makes either a `null` value or a missing property serialize to an omitted attribute.
3157+
As the Schema Object validates the in-memory representation, this allows handling the combination of `null` and a required property.
3158+
However, because there is no distinct way to represent `null` as an attribute, it is RECOMMENDED to make attribute properties optional rather than use `null`.
3159+
3160+
To ensure correct round-trip behavior, when parsing an element that omits an attribute, implementations SHOULD set the corresponding property to `null` if the schema allows for that value (e.g. `type: ["number", "null"]`), and omit the property otherwise (e.g.`type: "number"`).
3161+
31473162
##### XML Object Examples
31483163

31493164
The Schema Objects are followed by an example XML representation produced for the schema shown.
@@ -3564,6 +3579,56 @@ The in-memory instance data structure for the above example would be:
35643579
]
35653580
```
35663581

3582+
###### XML With `null` Values
3583+
3584+
Recall that the schema validates the in-memory data, not the XML document itself.
3585+
The properties of the `"metadata"` element are omitted for brevity as it is here to show how the `null` value is represented.
3586+
3587+
```yaml
3588+
product:
3589+
type: object
3590+
required:
3591+
- count
3592+
- description
3593+
- related
3594+
properties:
3595+
count:
3596+
type:
3597+
- number
3598+
- "null"
3599+
xml:
3600+
nodeType: attribute
3601+
rating:
3602+
type: string
3603+
xml:
3604+
nodeType: attribute
3605+
description:
3606+
type: string
3607+
related:
3608+
type:
3609+
- object
3610+
- "null"
3611+
```
3612+
3613+
```xml
3614+
<product>
3615+
<description>Thing</description>
3616+
<related xsi:nil="true" />
3617+
</product>
3618+
```
3619+
3620+
The above XML example corresponds to the following in-memory instance:
3621+
3622+
```json
3623+
{
3624+
"product": {
3625+
"count": null,
3626+
"description": "Thing",
3627+
"related": null
3628+
}
3629+
}
3630+
```
3631+
35673632
#### Security Scheme Object
35683633

35693634
Defines a security scheme that can be used by the operations.

0 commit comments

Comments
 (0)