Skip to content

Commit b3dd05f

Browse files
authored
Merge pull request #4612 from handrews/xml-null-312
v3.1.2: Provide guidance on null in XML.
2 parents 0e5c817 + 21d0a85 commit b3dd05f

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/oas.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,12 +3455,29 @@ See examples for expected behavior.
34553455

34563456
This object MAY be extended with [Specification Extensions](#specification-extensions).
34573457

3458+
##### Namespace Limitations
3459+
34583460
The `namespace` field is intended to match the syntax of [XML namespaces](https://www.w3.org/TR/xml-names11/), although there are a few caveats:
34593461

34603462
* Versions 3.1.0, 3.0.3, and earlier of this specification erroneously used the term "absolute URI" instead of "non-relative URI", so authors using namespaces that include a fragment should check tooling support carefully.
34613463
* XML allows but discourages relative URI-references, while this specification outright forbids them.
34623464
* XML 1.1 allows IRIs ([RFC3987](https://datatracker.ietf.org/doc/html/rfc3987)) as namespaces, and specifies that namespaces are compared without any encoding or decoding, which means that IRIs encoded to meet this specification's URI syntax requirement cannot be compared to IRIs as-is.
34633465

3466+
##### Handling `null` Values
3467+
3468+
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.
3469+
3470+
However, implementations SHOULD handle `null` values as follows:
3471+
3472+
* For elements, produce an empty element with an `xsi:nil="true"` attribute.
3473+
* For attributes, omit the attribute.
3474+
3475+
Note that for attributes, this makes either a `null` value or a missing property serialize to an omitted attribute.
3476+
As the Schema Object validates the in-memory representation, this allows handling the combination of `null` and a required property.
3477+
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`.
3478+
3479+
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"`).
3480+
34643481
##### XML Object Examples
34653482

34663483
Each of the following examples represent the value of the `properties` keyword in a [Schema Object](#schema-object) that is omitted for brevity.
@@ -3803,6 +3820,85 @@ animals:
38033820
</aliens>
38043821
```
38053822

3823+
###### XML With `null` Values
3824+
3825+
Recall that the schema validates the in-memory data, not the XML document itself.
3826+
The properties of the `"metadata"` element are omitted for brevity as it is here to show how the `null` value is represented.
3827+
3828+
```json
3829+
{
3830+
"product": {
3831+
"type": "object",
3832+
"required": ["count", "description", "related"],
3833+
"properties": {
3834+
"count": {
3835+
"type": ["number", "null"],
3836+
"xml": {
3837+
"attribute": true
3838+
}
3839+
},
3840+
"rating": {
3841+
"type": "string",
3842+
"xml": {
3843+
"attribute": true
3844+
}
3845+
},
3846+
"description": {
3847+
"type": "string"
3848+
},
3849+
"related": {
3850+
"type": ["object", "null"]
3851+
}
3852+
}
3853+
}
3854+
}
3855+
```
3856+
3857+
```yaml
3858+
product:
3859+
type: object
3860+
required:
3861+
- count
3862+
- description
3863+
- related
3864+
properties:
3865+
count:
3866+
type:
3867+
- number
3868+
- "null"
3869+
xml:
3870+
attribute: true
3871+
rating:
3872+
type: string
3873+
xml:
3874+
attribute: true
3875+
description:
3876+
type: string
3877+
related:
3878+
type:
3879+
- object
3880+
- "null"
3881+
```
3882+
3883+
```xml
3884+
<product>
3885+
<description>Thing</description>
3886+
<related xsi:nil="true" />
3887+
</product>
3888+
```
3889+
3890+
The above XML example corresponds to the following in-memory instance:
3891+
3892+
```json
3893+
{
3894+
"product": {
3895+
"count": null,
3896+
"description": "Thing",
3897+
"related": null
3898+
}
3899+
}
3900+
```
3901+
38063902
#### Security Scheme Object
38073903

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

0 commit comments

Comments
 (0)