Skip to content

Commit 3b3356b

Browse files
committed
Add support for Cookie Parameter style
1 parent 9bba39f commit 3b3356b

8 files changed

+585
-3
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Cookie Parameter Style Example
2+
3+
This example demonstrates the new `cookie` parameter style introduced in OpenAPI 3.2.
4+
5+
## Example OpenAPI 3.2 Document
6+
7+
```yaml
8+
openapi: 3.2.0
9+
info:
10+
title: Cookie Parameter Example API
11+
version: 1.0.0
12+
paths:
13+
/secure-data:
14+
get:
15+
summary: Get secure data using cookie authentication
16+
parameters:
17+
- name: sessionToken
18+
in: cookie
19+
style: cookie
20+
required: true
21+
description: Session authentication token
22+
schema:
23+
type: string
24+
- name: userPreferences
25+
in: cookie
26+
# Note: style defaults to "form" for cookie parameters when not specified
27+
description: User preference settings
28+
schema:
29+
type: string
30+
responses:
31+
'200':
32+
description: Secure data retrieved successfully
33+
'401':
34+
description: Unauthorized - invalid session token
35+
```
36+
37+
## Code Examples
38+
39+
### Creating a Cookie Parameter in C#
40+
41+
```csharp
42+
// Create a cookie parameter with explicit cookie style
43+
var cookieParameter = new OpenApiParameter
44+
{
45+
Name = "sessionToken",
46+
In = ParameterLocation.Cookie,
47+
Style = ParameterStyle.Cookie, // New in OpenAPI 3.2
48+
Required = true,
49+
Description = "Session authentication token",
50+
Schema = new OpenApiSchema { Type = JsonSchemaType.String }
51+
};
52+
53+
// Create a cookie parameter with default form style
54+
var preferencesParameter = new OpenApiParameter
55+
{
56+
Name = "userPreferences",
57+
In = ParameterLocation.Cookie,
58+
// Style will default to ParameterStyle.Form for cookie location
59+
Description = "User preference settings",
60+
Schema = new OpenApiSchema { Type = JsonSchemaType.String }
61+
};
62+
```
63+
64+
### Serializing to Different OpenAPI Versions
65+
66+
```csharp
67+
// Serialize to OpenAPI 3.2 - Cookie style is supported
68+
var v32Json = await cookieParameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2);
69+
// Result: includes "style": "cookie"
70+
71+
// Attempting to serialize to earlier versions will throw an exception
72+
try
73+
{
74+
var v31Json = await cookieParameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1);
75+
}
76+
catch (OpenApiException ex)
77+
{
78+
// Exception: "Parameter style 'cookie' is only supported in OpenAPI 3.2 and later versions"
79+
}
80+
81+
// However, using default style (form) works in all versions
82+
var v31JsonDefault = await preferencesParameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1);
83+
// Works fine - uses default form style
84+
```
85+
86+
### Version Compatibility
87+
88+
| Parameter Style | OpenAPI 2.0 | OpenAPI 3.0 | OpenAPI 3.1 | OpenAPI 3.2+ |
89+
|-----------------|--------------|--------------|--------------|---------------|
90+
| `cookie` | ? Error | ? Error | ? Error | ? Supported |
91+
| `form` (default)| ? Supported | ? Supported | ? Supported | ? Supported |
92+
93+
### Default Behavior
94+
95+
- **Default Style**: Cookie parameters default to `form` style when not explicitly specified
96+
- **Default Explode**: Cookie style parameters default to `explode: true` (same as form style)
97+
- **Style Omission**: When a cookie parameter uses the default `form` style, the `style` property is not serialized to avoid redundancy
98+
99+
### Key Features
100+
101+
1. **Version Validation**: Automatic validation prevents using `cookie` style in unsupported OpenAPI versions
102+
2. **Automatic Deserialization**: The library automatically recognizes and deserializes `cookie` style parameters
103+
3. **Default Handling**: Smart default behavior ensures compatibility and reduces verbosity
104+
4. **Round-trip Serialization**: Full support for serializing and deserializing cookie style parameters
105+
106+
This implementation follows the OpenAPI 3.2 specification while maintaining backward compatibility and providing clear error messages when version constraints are violated.

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ParameterStyle? Style
4444
/// <inheritdoc/>
4545
public bool Explode
4646
{
47-
get => _explode ?? Style == ParameterStyle.Form;
47+
get => _explode ?? (Style is ParameterStyle.Form or ParameterStyle.Cookie);
4848
set => _explode = value;
4949
}
5050

@@ -115,6 +115,12 @@ internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion versio
115115
Action<IOpenApiWriter, IOpenApiSerializable> callback)
116116
{
117117
Utils.CheckArgumentNull(writer);
118+
119+
// Validate that Cookie style is only used in OpenAPI 3.2 and later
120+
if (Style == ParameterStyle.Cookie && version < OpenApiSpecVersion.OpenApi3_2)
121+
{
122+
throw new OpenApiException($"Parameter style 'cookie' is only supported in OpenAPI 3.2 and later versions. Current version: {version}");
123+
}
118124

119125
writer.WriteStartObject();
120126

@@ -143,7 +149,7 @@ internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion versio
143149
}
144150

145151
// explode
146-
writer.WriteProperty(OpenApiConstants.Explode, _explode, Style is ParameterStyle.Form);
152+
writer.WriteProperty(OpenApiConstants.Explode, _explode, Style is ParameterStyle.Form or ParameterStyle.Cookie);
147153

148154
// allowReserved
149155
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);
@@ -251,6 +257,12 @@ internal virtual void WriteRequestBodySchemaForV2(IOpenApiWriter writer, Diction
251257
public virtual void SerializeAsV2(IOpenApiWriter writer)
252258
{
253259
Utils.CheckArgumentNull(writer);
260+
261+
// Validate that Cookie style is only used in OpenAPI 3.2 and later
262+
if (Style == ParameterStyle.Cookie)
263+
{
264+
throw new OpenApiException($"Parameter style 'cookie' is only supported in OpenAPI 3.2 and later versions. Current version: {OpenApiSpecVersion.OpenApi2_0}");
265+
}
254266

255267
writer.WriteStartObject();
256268

src/Microsoft.OpenApi/Models/ParameterStyle.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public enum ParameterStyle
4141
/// <summary>
4242
/// Provides a simple way of rendering nested objects using form parameters.
4343
/// </summary>
44-
[Display("deepObject")] DeepObject
44+
[Display("deepObject")] DeepObject,
45+
46+
/// <summary>
47+
/// Cookie style parameters. Introduced in OpenAPI 3.2.
48+
/// </summary>
49+
[Display("cookie")] Cookie
4550
}
4651
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "sessionId",
3+
"in": "cookie",
4+
"description": "Session identifier stored in cookie",
5+
"style": "cookie",
6+
"schema": {
7+
"type": "string"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"sessionId","in":"cookie","description":"Session identifier stored in cookie","style":"cookie","schema":{"type":"string"}}

0 commit comments

Comments
 (0)