Skip to content

Commit 0c76f10

Browse files
add Examples section to the Forms media-type page, and describe array modelling
This change accompanies an edit to the OAS for v3.3.
1 parent 2777fb4 commit 0c76f10

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

_includes/media-type-entry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ This page also applies to any unrecognized {{ page.default_for }} media type.
2828
{{ include.remarks }}
2929
{% endif %}
3030

31+
{% if include.examples %}
32+
## Examples
33+
34+
{{ include.examples }}
35+
{% endif %}

registries/_media-type/forms.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ references:
2424
anchor: encoding-by-name
2525
parent: Encoding Usage and Restrictions
2626
parentAnchor: encoding-usage-and-restrictions
27+
- section: Encoding By Position
28+
anchor: encoding-by-position
29+
parent: Encoding Usage and Restrictions
30+
parentAnchor: encoding-usage-and-restrictions
2731
- section: Encoding the `x-www-form-urlencoded` Media Type
2832
anchor: encoding-the-x-www-form-urlencoded-media-type
2933
parent: Encoding Object
@@ -36,6 +40,8 @@ references:
3640
anchor: appendix-c-using-rfc6570-based-serialization
3741
- section: "Appendix E: Percent-Encoding and Form Media Types"
3842
anchor: appendix-e-percent-encoding-and-form-media-types
43+
- section: Non-JSON Data
44+
anchor: non-json-data
3945
layout: default
4046
---
4147

@@ -45,11 +51,65 @@ Web-style form data consists of name-value pairs, with duplicate names allowed,
4551

4652
{% capture remarks %}
4753
Both form media types use the [Encoding Object](https://spec.openapis.org/oas/latest.html#encoding-object) to map object properties from schema-ready data structures to name-value pairs, with special rules for arrays causing each array value to be treated as a separate pair with the same name.
48-
While the ordering of pairs is significant in these formats, the OAS does not (as of v3.2) provide a way to control such ordering.
4954

5055
As of OAS v3.2, endpoint URL query strings can be modeled as a media type using `in: querystring` in the [Parameter Object](https://spec.openapis.org/oas/latest.html#parameter-object). The query string can also be modeled using multiple `in: query` Parameter Objects through mechanisms similar to the Encoding Object.
5156

5257
Note that URL-encoded forms have been defined by different standards organizations at different times, leading to inconsistencies regarding percent-encoding in later standards and implementations; this is addressed in detail in [Appendix E](https://spec.openapis.org/oas/latest.html#appendix-e-percent-encoding-and-form-media-types).
58+
59+
Since v3.3, the OAS provides for a way to preserve ordering, by treating the deserialized content as an array, rather than an object.
60+
{% endcapture %}
61+
62+
{% capture examples %}
63+
64+
Treating the data as an object, this data:
65+
66+
```json
67+
{
68+
"alpha": 1,
69+
"beta": 2,
70+
"gamma": [ 3, 4 ]
71+
}
72+
```
73+
74+
... serializes as `application/x-www-form-urlencoded` to: `alpha=1&beta=2&gamma=3&gamma=4`
75+
76+
and serializes as `multipart/form-data; boundary="4aKOX"` to:
77+
78+
```
79+
--4aKOX
80+
Content-Disposition: form-data; name="alpha"
81+
82+
1
83+
--4aKOX
84+
Content-Disposition: form-data; name="beta"
85+
86+
2
87+
--4aKOX
88+
Content-Disposition: form-data; name="gamma"
89+
90+
3
91+
--4aKOX
92+
Content-Disposition: form-data; name="gamma"
93+
94+
4
95+
--4aKOX--
96+
```
97+
98+
If preservation of value/part order is important, treat the data as an array, where each array item
99+
is an object consisting of the key/value pair:
100+
101+
```json
102+
[
103+
{ "alpha": 1 },
104+
{ "beta": 2 },
105+
{ "gamma": 3 }
106+
{ "gamma": 4 }
107+
}
108+
```
109+
110+
This distinction can be made clear to a deserializer by using `type: array` in the
111+
schema, using the process as described in [Non-JSON Data](https://spec.openapis.org/oas/latest#non-json-data).
112+
53113
{% endcapture %}
54114

55-
{% include media-type-entry.md summary=summary remarks=remarks %}
115+
{% include media-type-entry.md summary=summary remarks=remarks examples=examples %}

0 commit comments

Comments
 (0)