Skip to content

Commit 6e5c402

Browse files
authored
Merge pull request #36 from BinkyLabs/feat/7-Add-support-for-querystring-parameter-location
feat: Add support for querystring parameter location
2 parents ec9b962 + 7c14e8c commit 6e5c402

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ internal void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion versio
122122
throw new OpenApiException($"Parameter style 'cookie' is only supported in OpenAPI 3.2 and later versions. Current version: {version}");
123123
}
124124

125+
// Check for querystring restrictions
126+
if (In == ParameterLocation.QueryString)
127+
{
128+
if (version < OpenApiSpecVersion.OpenApi3_2)
129+
{
130+
throw new InvalidOperationException("Parameter location 'querystring' is only supported in OpenAPI 3.2.0 and above.");
131+
}
132+
// Only throw if forbidden properties are explicitly set (not just default values)
133+
if ((_style.HasValue) || (_explode.HasValue && _explode.Value) || AllowReserved || Schema != null)
134+
{
135+
throw new InvalidOperationException("When 'in' is 'querystring', 'style', 'explode', 'allowReserved', and 'schema' properties MUST NOT be used as per OpenAPI 3.2 specification.");
136+
}
137+
}
138+
125139
writer.WriteStartObject();
126140

127141
// name
@@ -264,6 +278,12 @@ public virtual void SerializeAsV2(IOpenApiWriter writer)
264278
throw new OpenApiException($"Parameter style 'cookie' is only supported in OpenAPI 3.2 and later versions. Current version: {OpenApiSpecVersion.OpenApi2_0}");
265279
}
266280

281+
// Throw if 'querystring' is used in V2
282+
if (In == ParameterLocation.QueryString)
283+
{
284+
throw new InvalidOperationException("Parameter location 'querystring' is not supported in OpenAPI 2.0.");
285+
}
286+
267287
writer.WriteStartObject();
268288

269289
// in

src/Microsoft.OpenApi/Models/ParameterLocation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public enum ParameterLocation
2727
/// <summary>
2828
/// Used to pass a specific cookie value to the API.
2929
/// </summary>
30-
[Display("cookie")] Cookie
30+
[Display("cookie")] Cookie,
31+
32+
/// <summary>
33+
/// Parameters that are appended to the URL query string (OpenAPI 3.2+ only).
34+
/// </summary>
35+
[Display("querystring")] QueryString
3136
}
3237
}

test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Globalization;
67
using System.IO;
@@ -406,5 +407,52 @@ public async Task SerializeParameterWithFormStyleAndExplodeTrueWorksAsync(bool p
406407
// Assert
407408
await Verifier.Verify(outputStringWriter).UseParameters(produceTerseOutput);
408409
}
410+
411+
[Fact]
412+
public void SerializeQueryStringParameter_BelowV32_Throws()
413+
{
414+
var parameter = new OpenApiParameter
415+
{
416+
Name = "foo",
417+
In = ParameterLocation.QueryString
418+
};
419+
var writer = new OpenApiJsonWriter(new StringWriter());
420+
// Style, Explode, AllowReserved, and Schema must be unset for this test to throw as expected
421+
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV3(writer));
422+
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV31(writer));
423+
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV2(writer));
424+
}
425+
426+
[Fact]
427+
public void SerializeQueryStringParameter_WithForbiddenProperties_Throws()
428+
{
429+
var parameter = new OpenApiParameter
430+
{
431+
Name = "foo",
432+
In = ParameterLocation.QueryString,
433+
Style = ParameterStyle.Form,
434+
Explode = true,
435+
AllowReserved = true,
436+
Schema = new OpenApiSchema { Type = JsonSchemaType.String }
437+
};
438+
var writer = new OpenApiJsonWriter(new StringWriter());
439+
Assert.Throws<InvalidOperationException>(() => parameter.SerializeAsV32(writer));
440+
}
441+
442+
[Fact]
443+
public async Task SerializeQueryStringParameter_V32_Succeeds()
444+
{
445+
var parameter = new OpenApiParameter
446+
{
447+
Name = "foo",
448+
In = ParameterLocation.QueryString,
449+
Style = null,
450+
AllowReserved = false,
451+
Schema = null,
452+
// Explode must be false (default) and not set
453+
};
454+
var json = await parameter.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2);
455+
Assert.Contains("querystring", json);
456+
}
409457
}
410458
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,8 @@ namespace Microsoft.OpenApi
17361736
Path = 2,
17371737
[Microsoft.OpenApi.Display("cookie")]
17381738
Cookie = 3,
1739+
[Microsoft.OpenApi.Display("querystring")]
1740+
QueryString = 4,
17391741
}
17401742
public enum ParameterStyle
17411743
{

0 commit comments

Comments
 (0)