forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for Cookie Parameter style #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
baywet
merged 4 commits into
feat/oai-3-2-support
from
feat/6-Add-support-for-Cookie-Parameter-style
Oct 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Cookie Parameter Style Example | ||
|
||
This example demonstrates the new `cookie` parameter style introduced in OpenAPI 3.2. | ||
|
||
## Example OpenAPI 3.2 Document | ||
|
||
```yaml | ||
openapi: 3.2.0 | ||
info: | ||
title: Cookie Parameter Example API | ||
version: 1.0.0 | ||
paths: | ||
/secure-data: | ||
get: | ||
summary: Get secure data using cookie authentication | ||
parameters: | ||
- name: sessionToken | ||
in: cookie | ||
style: cookie | ||
required: true | ||
description: Session authentication token | ||
schema: | ||
type: string | ||
- name: userPreferences | ||
in: cookie | ||
# Note: style defaults to "form" for cookie parameters when not specified | ||
description: User preference settings | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: Secure data retrieved successfully | ||
'401': | ||
description: Unauthorized - invalid session token | ||
``` | ||
|
||
## Code Examples | ||
|
||
### Creating a Cookie Parameter in C# | ||
|
||
```csharp | ||
// Create a cookie parameter with explicit cookie style | ||
var cookieParameter = new OpenApiParameter | ||
{ | ||
Name = "sessionToken", | ||
In = ParameterLocation.Cookie, | ||
Style = ParameterStyle.Cookie, // New in OpenAPI 3.2 | ||
Required = true, | ||
Description = "Session authentication token", | ||
Schema = new OpenApiSchema { Type = JsonSchemaType.String } | ||
}; | ||
|
||
// Create a cookie parameter with default form style | ||
var preferencesParameter = new OpenApiParameter | ||
{ | ||
Name = "userPreferences", | ||
In = ParameterLocation.Cookie, | ||
// Style will default to ParameterStyle.Form for cookie location | ||
Description = "User preference settings", | ||
Schema = new OpenApiSchema { Type = JsonSchemaType.String } | ||
}; | ||
``` | ||
|
||
### Serializing to Different OpenAPI Versions | ||
|
||
```csharp | ||
// Serialize to OpenAPI 3.2 - Cookie style is supported | ||
var v32Json = await cookieParameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2); | ||
// Result: includes "style": "cookie" | ||
|
||
// Attempting to serialize to earlier versions will throw an exception | ||
try | ||
{ | ||
var v31Json = await cookieParameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1); | ||
} | ||
catch (OpenApiException ex) | ||
{ | ||
// Exception: "Parameter style 'cookie' is only supported in OpenAPI 3.2 and later versions" | ||
} | ||
|
||
// However, using default style (form) works in all versions | ||
var v31JsonDefault = await preferencesParameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1); | ||
// Works fine - uses default form style | ||
``` | ||
|
||
### Version Compatibility | ||
|
||
| Parameter Style | OpenAPI 2.0 | OpenAPI 3.0 | OpenAPI 3.1 | OpenAPI 3.2+ | | ||
|-----------------|--------------|--------------|--------------|---------------| | ||
| `cookie` | ? Error | ? Error | ? Error | ? Supported | | ||
| `form` (default)| ? Supported | ? Supported | ? Supported | ? Supported | | ||
|
||
### Default Behavior | ||
|
||
- **Default Style**: Cookie parameters default to `form` style when not explicitly specified | ||
- **Default Explode**: Cookie style parameters default to `explode: true` (same as form style) | ||
- **Style Omission**: When a cookie parameter uses the default `form` style, the `style` property is not serialized to avoid redundancy | ||
|
||
### Key Features | ||
|
||
1. **Version Validation**: Automatic validation prevents using `cookie` style in unsupported OpenAPI versions | ||
2. **Automatic Deserialization**: The library automatically recognizes and deserializes `cookie` style parameters | ||
3. **Default Handling**: Smart default behavior ensures compatibility and reduces verbosity | ||
4. **Round-trip Serialization**: Full support for serializing and deserializing cookie style parameters | ||
|
||
This implementation follows the OpenAPI 3.2 specification while maintaining backward compatibility and providing clear error messages when version constraints are violated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...leTests.SerializeCookieParameterAsV32JsonWorksAsync_produceTerseOutput=False.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "sessionId", | ||
"in": "cookie", | ||
"description": "Session identifier stored in cookie", | ||
"style": "cookie", | ||
"schema": { | ||
"type": "string" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...yleTests.SerializeCookieParameterAsV32JsonWorksAsync_produceTerseOutput=True.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"sessionId","in":"cookie","description":"Session identifier stored in cookie","style":"cookie","schema":{"type":"string"}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.