You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's very unclear how numbers, booleans, and other non-UTF-8-string
values are converted to strings, particularly for the form media types.
This adds a brief appendix that acknowledges the lack of standardization,
and points to resources for the few cases that do have specifications.
It highlights concerns with relying on certain JSON Schema keywords
or values for serialization, and suggests defining schemas of
type string and requiring applications to perform the conversion
prior to schema validation as a way to control the results.
This also clarifies that schema validation occurs before serialization.
Copy file name to clipboardExpand all lines: versions/3.0.4.md
+27Lines changed: 27 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1042,6 +1042,7 @@ There are four possible parameter locations specified by the `in` field:
1042
1042
1043
1043
The rules for serialization of the parameter are specified in one of two ways.
1044
1044
Parameter Objects MUST include either a `content` field or a `schema` field, but not both.
1045
+
See [Appendix C](#dataTypeConversion) for a discussion of converting values of various types to string representations.
1045
1046
1046
1047
###### Common Fixed Fields
1047
1048
@@ -1607,6 +1608,7 @@ An `encoding` attribute is introduced to give you control over the serialization
1607
1608
#### <a name="encodingObject"></a>Encoding Object
1608
1609
1609
1610
A single encoding definition applied to a single schema property.
1611
+
See [Appendix C](#dataTypeConversion) for a discussion of converting values of various types to string representations.
1610
1612
1611
1613
##### Fixed Fields
1612
1614
Field Name | Type | Description
@@ -3505,3 +3507,28 @@ Version | Date | Notes
3505
3507
1.2 | 2014-03-14 | Initial release of the formal document.
3506
3508
1.1 | 2012-08-22 | Release of Swagger 1.1
3507
3509
1.0 | 2011-08-10 | First release of the Swagger Specification
3510
+
3511
+
## <a name="dataTypeConversion"></a>Appendix C: Data Type Conversion
3512
+
3513
+
Serializing typed data to plain text, which can occur in `text/plain` message bodies or `multipart` parts, as well as in the `application/x-www-form-urlencoded` format in either URL query strings or message bodies, involves significant implementation- or application-defined behavior.
3514
+
3515
+
Schema Objects validate data based on the [JSON Schema data model](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-4.2.1), which only recognizes four primitive data types: strings (which are UTF-8 except in [extremely limited circumstances](https://datatracker.ietf.org/doc/html/rfc8259#section-8.1)), numbers, booleans, and `null`.
3516
+
Notably, integers are not a distinct type from other numbers, with `type: integer` being a convenience defined mathematically, rather than based on the presence or absence of a decimal point in any string representation.
3517
+
3518
+
The Parameter and Encoding Objects offer features to control how to arrange values from array or object types.
3519
+
They can also be used to control how strings are further encoded to avoid reserved or illegal characters.
3520
+
However, there is no general-purpose specification for converting schema-validated non-UTF-8 primitive data types (or entire arrays or objects) to strings.
3521
+
3522
+
Two cases do offer standards-based guidance:
3523
+
3524
+
* [RFC3987 §3.1](https://datatracker.ietf.org/doc/html/rfc3987#section-3.1) provides guidance for converting non-Unicode strings to UTF-8, particularly in the context of URIs (and by extension, the form media types which use the same encoding rules)
3525
+
* [RFC6570 §2.3](https://www.rfc-editor.org/rfc/rfc6570#section-2.3) specifies which values, including but not limited to `null`, are considered _undefined_ and therefore treated specially in the expansion process when serializing based on that specification
3526
+
3527
+
To control the serialization of numbers, booleans, and `null` (or other values RFC6570 deems to be undefined) more precisely, schemas can be defined as `type: string` and constrained using `pattern`, `enum`, `format`, and other keywords to communicated how applications must pre-convert their data prior to schema validation.
3528
+
The resulting strings would not require any further type conversion.
3529
+
3530
+
The `format` keyword can assist in serialization.
3531
+
Some formats (such as `date-time` or `byte`) are unambiguous, while others (such as [`decimal`](https://spec.openapis.org/registry/format/decimal.html) in the [Format Registry](https://spec.openapis.org/registry/format/)) are less clear.
3532
+
However, care must be taken with `format` to ensure that the specific formats are supported by all relevant tools as unrecognized formats are ignored.
3533
+
3534
+
Requiring input as pre-formatted, schema-validated strings also improves round-trip interoperability as not all programming languages and environments support the same data types.
0 commit comments