|
| 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. |
0 commit comments