Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion versio
{
Utils.CheckArgumentNull(writer);

// Check for querystring restrictions
if (In == ParameterLocation.QueryString)
{
if (version < OpenApiSpecVersion.OpenApi3_2)
{
throw new InvalidOperationException("Parameter location 'querystring' is only supported in OpenAPI 3.2.0 and above.");
}
// Only throw if forbidden properties are explicitly set (not just default values)
if ((_style.HasValue) || (_explode.HasValue && _explode.Value) || AllowReserved || Schema != null)
{
throw new InvalidOperationException("When 'in' is 'querystring', 'style', 'explode', 'allowReserved', and 'schema' properties MUST NOT be used as per OpenAPI 3.2 specification.");
}
}

writer.WriteStartObject();

// name
Expand Down Expand Up @@ -252,6 +266,12 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
{
Utils.CheckArgumentNull(writer);

// Throw if 'querystring' is used in V2
if (In == ParameterLocation.QueryString)
{
throw new InvalidOperationException("Parameter location 'querystring' is not supported in OpenAPI 2.0.");
}

writer.WriteStartObject();

// in
Expand Down
7 changes: 6 additions & 1 deletion src/Microsoft.OpenApi/Models/ParameterLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public enum ParameterLocation
/// <summary>
/// Used to pass a specific cookie value to the API.
/// </summary>
[Display("cookie")] Cookie
[Display("cookie")] Cookie,

/// <summary>
/// Parameters that are appended to the URL query string (OpenAPI 3.2+ only).
/// </summary>
[Display("querystring")] QueryString
}
}
48 changes: 48 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -406,5 +407,52 @@ public async Task SerializeParameterWithFormStyleAndExplodeTrueWorksAsync(bool p
// Assert
await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput);
}

[Fact]
public void SerializeQueryStringParameter_BelowV32_Throws()
{
var parameter = new OpenApiParameter
{
Name = "foo",
In = ParameterLocation.QueryString
};
var writer = new OpenApiJsonWriter(new StringWriter());
// Style, Explode, AllowReserved, and Schema must be unset for this test to throw as expected
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV3(writer));
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV31(writer));
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV2(writer));
}

[Fact]
public void SerializeQueryStringParameter_WithForbiddenProperties_Throws()
{
var parameter = new OpenApiParameter
{
Name = "foo",
In = ParameterLocation.QueryString,
Style = ParameterStyle.Form,
Explode = true,
AllowReserved = true,
Schema = new OpenApiSchema { Type = JsonSchemaType.String }
};
var writer = new OpenApiJsonWriter(new StringWriter());
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV32(writer));
}

[Fact]
public async Task SerializeQueryStringParameter_V32_Succeeds()
{
var parameter = new OpenApiParameter
{
Name = "foo",
In = ParameterLocation.QueryString,
Style = null,
AllowReserved = false,
Schema = null,
// Explode must be false (default) and not set
};
var json = await parameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2);
Assert.Contains("querystring", json);
}
}
}
2 changes: 2 additions & 0 deletions test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,8 @@ namespace Microsoft.OpenApi
Path = 2,
[Microsoft.OpenApi.Display("cookie")]
Cookie = 3,
[Microsoft.OpenApi.Display("querystring")]
QueryString = 4,
}
public enum ParameterStyle
{
Expand Down