Skip to content

Commit 061dcbd

Browse files
authored
Merge pull request #3840 from handrews/ser-types-304
Appendix on converting data types to strings (3.0.4)
2 parents 1d56e50 + aacbbc9 commit 061dcbd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

versions/3.0.4.md

+28
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ There are four possible parameter locations specified by the `in` field:
10491049

10501050
The rules for serialization of the parameter are specified in one of two ways.
10511051
Parameter Objects MUST include either a `content` field or a `schema` field, but not both.
1052+
See [Appendix B](#dataTypeConversion) for a discussion of converting values of various types to string representations.
10521053

10531054
###### Common Fixed Fields
10541055

@@ -1618,6 +1619,7 @@ An `encoding` attribute is introduced to give you control over the serialization
16181619
#### <a name="encodingObject"></a>Encoding Object
16191620

16201621
A single encoding definition applied to a single schema property.
1622+
See [Appendix B](#dataTypeConversion) for a discussion of converting values of various types to string representations.
16211623

16221624
##### Fixed Fields
16231625
Field Name | Type | Description
@@ -3700,6 +3702,32 @@ Version | Date | Notes
37003702

37013703
## <a name="dataTypeConversion"></a>Appendix B: Data Type Conversion
37023704

3705+
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.
3706+
3707+
Schema Objects validate data based on the [JSON Schema data model](https://datatracker.ietf.org/doc/html/draft-wright-json-schema-00#section-4.2), which only recognizes four primitive data types: strings (which are [only broadly interoperable as UTF-8](https://datatracker.ietf.org/doc/html/rfc7159#section-8.1)), numbers, booleans, and `null`.
3708+
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.
3709+
3710+
The [Parameter Object](#parameterObject) and [Encoding Object](#encodingObject) offer features to control how to arrange values from array or object types.
3711+
They can also be used to control how strings are further encoded to avoid reserved or illegal characters.
3712+
However, there is no general-purpose specification for converting schema-validated non-UTF-8 primitive data types (or entire arrays or objects) to strings.
3713+
3714+
Two cases do offer standards-based guidance:
3715+
3716+
* [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)
3717+
* [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
3718+
3719+
Implementations of RFC6570 often have their own conventions for converting non-string values, but these are implementation-specific and not defined by the RFC itself.
3720+
This is one reason for the OpenAPI Specification to leave these conversions as implementation-defined: It allows using RFC6570 implementations regardless of how they choose to perform the conversions.
3721+
3722+
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 communicate how applications must pre-convert their data prior to schema validation.
3723+
The resulting strings would not require any further type conversion.
3724+
3725+
The `format` keyword can assist in serialization.
3726+
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.
3727+
However, care must be taken with `format` to ensure that the specific formats are supported by all relevant tools as unrecognized formats are ignored.
3728+
3729+
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.
3730+
37033731
## <a name="usingRFC6570Implementations"></a>Appendix C: Using RFC6570 Implementations
37043732

37053733
Serialization is defined in terms of RFC6570 URI Templates in two scenarios:

0 commit comments

Comments
 (0)