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 all commits
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
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"}} |
248 changes: 248 additions & 0 deletions
248
test/Microsoft.OpenApi.Tests/Models/OpenApiParameterCookieStyleTests.cs
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,248 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using VerifyXunit; | ||
|
||
namespace Microsoft.OpenApi.Tests.Models | ||
{ | ||
[Collection("DefaultSettings")] | ||
public class OpenApiParameterCookieStyleTests | ||
{ | ||
private static OpenApiParameter CookieParameter => new() | ||
{ | ||
Name = "sessionId", | ||
In = ParameterLocation.Cookie, | ||
Style = ParameterStyle.Cookie, | ||
Description = "Session identifier stored in cookie", | ||
Schema = new OpenApiSchema() | ||
{ | ||
Type = JsonSchemaType.String | ||
} | ||
}; | ||
|
||
private static OpenApiParameter CookieParameterWithDefault => new() | ||
{ | ||
Name = "preferences", | ||
In = ParameterLocation.Cookie, | ||
Description = "User preferences stored in cookie", | ||
Schema = new OpenApiSchema() | ||
{ | ||
Type = JsonSchemaType.String | ||
} | ||
}; | ||
|
||
[Fact] | ||
public void CookieParameterStyleIsAvailable() | ||
{ | ||
// Arrange & Act | ||
var parameter = CookieParameter; | ||
|
||
// Assert | ||
Assert.Equal(ParameterStyle.Cookie, parameter.Style); | ||
Assert.Equal(ParameterLocation.Cookie, parameter.In); | ||
} | ||
|
||
[Fact] | ||
public void CookieParameterHasCorrectDefaultStyle() | ||
{ | ||
// Arrange & Act | ||
var parameter = CookieParameterWithDefault; | ||
|
||
// Assert | ||
Assert.Equal(ParameterStyle.Form, parameter.Style); // Default for cookie location should be Form | ||
} | ||
|
||
[Fact] | ||
public void CookieParameterStyleDisplayNameIsCookie() | ||
{ | ||
// Arrange & Act | ||
var displayName = ParameterStyle.Cookie.GetDisplayName(); | ||
|
||
// Assert | ||
Assert.Equal("cookie", displayName); | ||
} | ||
|
||
[Fact] | ||
public async Task SerializeCookieParameterAsV32JsonWorks() | ||
{ | ||
// Arrange | ||
var expected = | ||
""" | ||
{ | ||
"name": "sessionId", | ||
"in": "cookie", | ||
"description": "Session identifier stored in cookie", | ||
"style": "cookie", | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
"""; | ||
|
||
// Act | ||
var actual = await CookieParameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2); | ||
|
||
// Assert | ||
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(actual), JsonNode.Parse(expected))); | ||
} | ||
|
||
[Fact] | ||
public async Task SerializeCookieParameterWithDefaultStyleAsV32JsonWorks() | ||
{ | ||
// Arrange | ||
var expected = | ||
""" | ||
{ | ||
"name": "preferences", | ||
"in": "cookie", | ||
"description": "User preferences stored in cookie", | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
"""; | ||
|
||
// Act | ||
var actual = await CookieParameterWithDefault.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2); | ||
|
||
// Assert | ||
Assert.True(JsonNode.DeepEquals(JsonNode.Parse(actual), JsonNode.Parse(expected))); | ||
} | ||
|
||
[Theory] | ||
[InlineData(OpenApiSpecVersion.OpenApi2_0)] | ||
[InlineData(OpenApiSpecVersion.OpenApi3_0)] | ||
[InlineData(OpenApiSpecVersion.OpenApi3_1)] | ||
public void SerializeCookieParameterStyleThrowsForEarlierVersions(OpenApiSpecVersion version) | ||
{ | ||
// Arrange | ||
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); | ||
var writer = new OpenApiJsonWriter(outputStringWriter); | ||
|
||
// Act & Assert | ||
var exception = Assert.Throws<OpenApiException>(() => | ||
{ | ||
switch (version) | ||
{ | ||
case OpenApiSpecVersion.OpenApi2_0: | ||
CookieParameter.SerializeAsV2(writer); | ||
break; | ||
case OpenApiSpecVersion.OpenApi3_0: | ||
CookieParameter.SerializeAsV3(writer); | ||
break; | ||
case OpenApiSpecVersion.OpenApi3_1: | ||
CookieParameter.SerializeAsV31(writer); | ||
break; | ||
} | ||
}); | ||
|
||
Assert.Contains("Parameter style 'cookie' is only supported in OpenAPI 3.2 and later versions", exception.Message); | ||
Assert.Contains($"Current version: {version}", exception.Message); | ||
} | ||
|
||
[Theory] | ||
[InlineData(OpenApiSpecVersion.OpenApi2_0)] | ||
[InlineData(OpenApiSpecVersion.OpenApi3_0)] | ||
[InlineData(OpenApiSpecVersion.OpenApi3_1)] | ||
public async Task SerializeCookieParameterWithDefaultStyleWorksForEarlierVersions(OpenApiSpecVersion version) | ||
{ | ||
// Arrange & Act | ||
string actual = version switch | ||
{ | ||
OpenApiSpecVersion.OpenApi2_0 => await CookieParameterWithDefault.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi2_0), | ||
OpenApiSpecVersion.OpenApi3_0 => await CookieParameterWithDefault.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0), | ||
OpenApiSpecVersion.OpenApi3_1 => await CookieParameterWithDefault.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1), | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
|
||
// Assert - Should not throw because default style (Form) is being used | ||
Assert.NotEmpty(actual); | ||
Assert.DoesNotContain("\"style\"", actual); // Style should not be emitted when it's the default | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public async Task SerializeCookieParameterAsV32JsonWorksAsync(bool produceTerseOutput) | ||
{ | ||
// Arrange | ||
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); | ||
var writer = new OpenApiJsonWriter(outputStringWriter, new() { Terse = produceTerseOutput }); | ||
|
||
// Act | ||
CookieParameter.SerializeAsV32(writer); | ||
await writer.FlushAsync(); | ||
|
||
// Assert | ||
await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput); | ||
} | ||
|
||
[Fact] | ||
public void CookieParameterStyleEnumValueExists() | ||
{ | ||
// Arrange & Act | ||
var cookieStyleExists = Enum.IsDefined(typeof(ParameterStyle), ParameterStyle.Cookie); | ||
|
||
// Assert | ||
Assert.True(cookieStyleExists); | ||
} | ||
|
||
[Fact] | ||
public void CookieParameterStyleCanBeDeserialized() | ||
{ | ||
// Arrange | ||
var cookieStyleString = "cookie"; | ||
|
||
// Act | ||
var success = cookieStyleString.TryGetEnumFromDisplayName<ParameterStyle>(out var parameterStyle); | ||
|
||
// Assert | ||
Assert.True(success); | ||
Assert.Equal(ParameterStyle.Cookie, parameterStyle); | ||
} | ||
|
||
[Fact] | ||
public async Task SerializeCookieParameterAsYamlV32Works() | ||
{ | ||
// Arrange | ||
var expected = """ | ||
name: sessionId | ||
in: cookie | ||
description: Session identifier stored in cookie | ||
style: cookie | ||
schema: | ||
type: string | ||
"""; | ||
|
||
// Act | ||
var actual = await CookieParameter.SerializeAsYamlAsync(OpenApiSpecVersion.OpenApi3_2); | ||
|
||
// Assert | ||
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), actual.MakeLineBreaksEnvironmentNeutral()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(ParameterStyle.Form, true)] | ||
[InlineData(ParameterStyle.SpaceDelimited, false)] | ||
[InlineData(ParameterStyle.Cookie, true)] | ||
public void WhenStyleIsFormOrCookieTheDefaultValueOfExplodeShouldBeTrueOtherwiseFalse(ParameterStyle? style, bool expectedExplode) | ||
{ | ||
// Arrange | ||
var parameter = new OpenApiParameter | ||
{ | ||
Name = "name1", | ||
In = ParameterLocation.Query, | ||
Style = style | ||
}; | ||
|
||
// Act & Assert | ||
Assert.Equal(expectedExplode, parameter.Explode); | ||
} | ||
} | ||
} |
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
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.