Skip to content

Parameter Serialization

Serialization means translating data structures or object state into a format that can be transmitted and reconstructed later. OpenAPI 3.0 supports arrays and objects in operation parameters (path, query, header, and cookie) and lets you specify how these parameters should be serialized. The serialization method is defined by the style and explode keywords:

  • style defines how multiple values are delimited. Possible styles depend on the parameter location – path, query, header or cookie.
  • explode (true/false) specifies whether arrays and objects should generate separate parameters for each array item or object property.

OpenAPI serialization rules are based on a subset of URI template patterns defined by RFC 6570. Tool implementers can use existing URI template libraries to handle the serialization, as explained below.

Path Parameters

Path parameters support the following style values:

  • simple – (default) comma-separated values. Corresponds to the {param_name} URI template.
  • label – dot-prefixed values, also known as label expansion. Corresponds to the {.param_name} URI template.
  • matrix – semicolon-prefixed values, also known as path-style expansion. Corresponds to the {;param_name} URI template.

The default serialization method is style: simple and explode: false. Given the path /users/{id}, the path parameter id is serialized as follows:

styleexplodeURI templatePrimitive value id = 5Array id = [3, 4, 5]Object id = {“role”: “admin”, “firstName”: “Alex”}
simple *false */users/{id}/users/5/users/3,4,5/users/role,admin,firstName,Alex
simpletrue/users/{id*}/users/5/users/3,4,5/users/role=admin,firstName=Alex
labelfalse/users/{.id}/users/.5/users/.3,4,5/users/.role,admin,firstName,Alex
labeltrue/users/{.id*}/users/.5/users/.3.4.5/users/.role=admin.firstName=Alex
matrixfalse/users/{;id}/users/;id=5/users/;id=3,4,5/users/;id=role,admin,firstName,Alex
matrixtrue/users/{;id*}/users/;id=5/users/;id=3;id=4;id=5/users/;role=admin;firstName=Alex
  • Default serialization method

The label and matrix styles are sometimes used with partial path parameters, such as /users{id}, because the parameter values get prefixed.

Query Parameters

Query parameters support the following style values:

  • form – (default) ampersand-separated values, also known as form-style query expansion. Corresponds to the {?param_name} URI template.
  • spaceDelimited – space-separated array values. Same as collectionFormat: ssv in OpenAPI 2.0. Has effect only for non-exploded arrays (explode: false), that is, the space separates the array values if the array is a single parameter, as in arr=a b c.
  • pipeDelimited – pipeline-separated array values. Same as collectionFormat: pipes in OpenAPI 2.0. Has effect only for non-exploded arrays (explode: false), that is, the pipe separates the array values if the array is a single parameter, as in arr=a|b|c.
  • deepObject – simple non-nested objects are serialized as paramName[prop1]=value1&paramName[prop2]=value2&.... The behavior for nested objects and arrays is undefined.

The default serialization method is style: form and explode: true. This corresponds to collectionFormat: multi from OpenAPI 2.0. Given the path /users with a query parameter id, the query string is serialized as follows:

styleexplodeURI templatePrimitive value id = 5Array id = [3, 4, 5]Object id = {“role”: “admin”, “firstName”: “Alex”}
form *true */users{?id*}/users?id=5/users?id=3&id=4&id=5/users?role=admin&firstName=Alex
formfalse/users{?id}/users?id=5/users?id=3,4,5/users?id=role,admin,firstName,Alex
spaceDelimitedtrue/users{?id*}n/a/users?id=3&id=4&id=5n/a
spaceDelimitedfalsen/an/a/users?id=3%204%205n/a
pipeDelimitedtrue/users{?id*}n/a/users?id=3&id=4&id=5n/a
pipeDelimitedfalsen/an/a/users?id=3|4|5n/a
deepObjecttruen/an/an/a/users?id[role]=admin&id[firstName]=Alex

* Default serialization method

Additionally, the allowReserved keyword specifies whether the reserved characters :/?#[]@!$&'()*+,;= in parameter values are allowed to be sent as they are, or should be percent-encoded. By default, allowReserved is false, and reserved characters are percent-encoded. For example, / is encoded as %2F (or %2f), so that the parameter value quotes/h2g2.txt will be sent as quotes%2Fh2g2.txt.

Header Parameters

Header parameters always use the simple style, that is, comma-separated values. This corresponds to the {param_name} URI template. An optional explode keyword controls the object serialization. Given the request header named X-MyHeader, the header value is serialized as follows:

styleexplodeURI templatePrimitive value X-MyHeader = 5Array X-MyHeader = [3, 4, 5]Object X-MyHeader = {“role”: “admin”, “firstName”: “Alex”}
simple *false *{id}X-MyHeader: 5X-MyHeader: 3,4,5X-MyHeader: role,admin,firstName,Alex
simpletrue{id*}X-MyHeader: 5X-MyHeader: 3,4,5X-MyHeader: role=admin,firstName=Alex
  • Default serialization method

Cookie parameters always use the form style. An optional explode keyword controls the array and object serialization. Given the cookie named id, the cookie value is serialized as follows:

styleexplodeURI templatePrimitive value id = 5Array id = [3, 4, 5]Object id = {“role”: “admin”, “firstName”: “Alex”}
form *true *Cookie: id=5
formfalseid={id}Cookie: id=5Cookie: id=3,4,5Cookie: id=role,admin,firstName,Alex
  • Default serialization method

Serialization and RFC 6570

OpenAPI serialization rules are based on a subset of URI templates defined by RFC 6570. Tool implementers can use existing URI template libraries to handle the serialization. You will need to construct the URI template based on the path and parameter definitions. The following table shows how OpenAPI keywords are mapped to the URI Template modifiers.

KeywordURI Template Modifier
style: simplenone
style: label. prefix
style: matrix; prefix
style: form? or & prefix (depending on the parameter position in the query string)
style: pipeDelimited? or & prefix (depending on the parameter position in the query string) – but using pipes |instead of commas, to join the array values
style: spaceDelimited? or & prefix (depending on the parameter position in the query string) – but using spaces instead of commas , to join the array values
explode: falsenone
explode: true* suffix
allowReserved: falsenone
allowReserved: true+ prefix

For example, consider the path /users{id} with a query parameter metadata, defined like so:

1
paths:
2
# /users;id=3;id=4?metadata=true
3
/users{id}:
4
get:
5
parameters:
6
- in: path
7
name: id
8
required: true
9
schema:
10
type: array
11
items:
12
type: integer
13
minItems: 1
14
style: matrix
15
explode: true
16
- in: query
17
name: metadata
18
schema:
19
type: boolean
20
# Using the default serialization for query parameters:
21
# style=form, explode=false, allowReserved=false
22
responses:
23
'200':
24
description: A list of users

The path parameter id uses the matrix style with the explode modifier, which corresponds to the {;id*} template. The query parameter metadata uses the default form style, which corresponds to the {?metadata} template. The complete URI template would look like:

1
/users{;id*}{?metadata}

A client application can then use an URI template library to generate the request URL based on this template and specific parameter values.

Other Serialization Methods

style and explode cover the most common serialization methods, but not all. For more complex scenarios (for example, a JSON-formatted object in the query string), you can use the content keyword and specify the media type that defines the serialization format. For more information, see schema vs content.

Did not find what you were looking for? Ask the community
Found a mistake? Let us know