Skip to content

Commit 774f70c

Browse files
authored
Merge pull request #4748 from handrews/set-cookie
v3.2: Provide guidance for the `Set-Cookie` response header
2 parents 545cff1 + cb8baa3 commit 774f70c

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/oas.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,91 @@ components:
27762776
$ref: '#/components/mediaTypes/CollectionLinks'
27772777
```
27782778
2779+
##### Representing the `Set-Cookie` Header
2780+
2781+
The `Set-Cookie` header is noted in [[!RFC9110]] [Section 5.3](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.3) as an exception to the normal rules of headers with multiple values.
2782+
2783+
For most headers using the general syntax defined in RFC9110, the multiple-line and comma-separated single-line forms are interchangeable, meaning that this:
2784+
2785+
```http
2786+
Accept-Encoding: compress;q=0.5
2787+
Accept-Encoding: gzip;q=1.0
2788+
```
2789+
2790+
is interchangeable with the one-line form that works well with the OAS's `style: "simple"` option:
2791+
2792+
```http
2793+
Accept-Encoding: compress;q=0.5,gzip;q=1.0
2794+
```
2795+
2796+
The OAS models such multi-value headers using the one-line form as it matches the behavior of `style: "simple"`, and works well when using `content` as the values are completely separate from the header name, but it does not matter which form is used in an actual HTTP message.
2797+
2798+
As also noted in the RFC, `Set-Cookie` is an exception as it allows unquoted, non-escaped commas in its values, and can only use the one-value-per-line form.
2799+
For HTTP messages, this is purely a serialization concern, and no more of a problem than a message that uses the multi-line form of any other header.
2800+
2801+
However, because examples and values modeled with `content` do not incorporate the header name, for these fields `Set-Cookie` MUST be handled by placing each value on a separate line, without the header name or the `:` delimiter.
2802+
2803+
The following example shows two different ways to describe `Set-Cookie` headers that require cookies named `"lang"` and `"foo"`. The first uses `content` to preserve the necessary whitespace in the `"Expires"` cookie attribute, while the second shows the use of `style: "simple"` and forbids whitespace to ensure that values work with this serialization approach:
2804+
2805+
```yaml
2806+
components:
2807+
headers:
2808+
SetCookieWithExpires:
2809+
# Spaces within the Expires values prevent the use of `schema` and
2810+
# `style` as they would be percent-encoded, even with `allowReserved`.
2811+
content:
2812+
text/plain:
2813+
schema:
2814+
# Due to lack of support for multiline regular expressions
2815+
# in the `pattern` keyword, not much validation can be done.
2816+
type: string
2817+
examples:
2818+
WithExpires:
2819+
# This demonstrates that the text is required to be provided
2820+
# in the final format, and is not changed by serialization.
2821+
# In practice, it is not necessary to show both value fields.
2822+
dataValue: |
2823+
lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2824+
foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2825+
serializedValue: |
2826+
lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2827+
foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2828+
SetCookieWithNoSpaces:
2829+
schema:
2830+
type: object
2831+
required:
2832+
- lang
2833+
- foo
2834+
additionalProperties:
2835+
type: string
2836+
pattern: "^[^[:space:]]*$"
2837+
style: simple
2838+
explode: true
2839+
examples:
2840+
SetCookies:
2841+
dataValue: {
2842+
"lang": "en-US",
2843+
"foo": "bar"
2844+
}
2845+
serializedValue: |
2846+
lang=en-US
2847+
foo=bar
2848+
```
2849+
2850+
In an HTTP message, the serialized example with Expires would look like:
2851+
2852+
```http
2853+
Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GM
2854+
Set-Cookie: foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2855+
```
2856+
2857+
and the example without Expires would look like:
2858+
2859+
```http
2860+
Set-Cookie: lang=en-US
2861+
Set-Cookie: foo=bar
2862+
```
2863+
27792864
##### Header Object Example
27802865
27812866
A simple header of type `integer`:

0 commit comments

Comments
 (0)