Skip to content

Commit 4bac05b

Browse files
authored
Fix #3278 (#3280)
Add null check for type, add test.
1 parent 1842380 commit 4bac05b

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ private OpenApiParameter GenerateParameterWithoutFilter(
611611

612612
private static ParameterStyle? GetParameterStyle(Type type, BindingSource source)
613613
{
614-
return source == BindingSource.Query && type.IsGenericType &&
614+
return source == BindingSource.Query && type?.IsGenericType == true &&
615615
typeof(IEnumerable<KeyValuePair<string, string>>).IsAssignableFrom(type)
616616
? ParameterStyle.DeepObject
617617
: null;

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void GetSwagger_GeneratesSwaggerDocument_ForApiDescriptionsWithMatchingGr
3838
c => nameof(c.ActionWithNoParameters), groupName: "v1", httpMethod: "GET", relativePath: "resource"),
3939

4040
ApiDescriptionFactory.Create<FakeController>(
41-
c => nameof(c.ActionWithNoParameters), groupName: "v2", httpMethod: "POST", relativePath: "resource"),
41+
c => nameof(c.ActionWithNoParameters), groupName: "v2", httpMethod: "POST", relativePath: "resource")
4242
},
4343
options: new SwaggerGeneratorOptions
4444
{
@@ -2523,6 +2523,51 @@ public void GetSwagger_OpenApiOperationWithRawContent_IsHandled()
25232523
Assert.Single(document.Paths["/resource"].Operations);
25242524
}
25252525

2526+
[Fact]
2527+
public void GetSwagger_BindingSourceQueryParameter_NotThrowsException()
2528+
{
2529+
var apiDescription = new ApiDescription
2530+
{
2531+
HttpMethod = "GET",
2532+
ActionDescriptor = new ActionDescriptor
2533+
{
2534+
RouteValues = new Dictionary<string, string>
2535+
{
2536+
["controller"] = "Catalog"
2537+
}
2538+
},
2539+
RelativePath = "api/v1/Images/{image}",
2540+
GroupName = "v1",
2541+
ParameterDescriptions =
2542+
{
2543+
new ApiParameterDescription
2544+
{
2545+
Name = "width",
2546+
Source = BindingSource.Query,
2547+
DefaultValue = string.Empty,
2548+
Type = typeof(int)
2549+
}
2550+
}
2551+
};
2552+
var subject = Subject(
2553+
apiDescriptions:
2554+
[
2555+
apiDescription
2556+
],
2557+
options: new SwaggerGeneratorOptions
2558+
{
2559+
SwaggerDocs = new Dictionary<string, OpenApiInfo>
2560+
{
2561+
["v1"] = new() { Version = "V1", Title = "Test API" }
2562+
}
2563+
}
2564+
);
2565+
2566+
var document = subject.GetSwagger("v1");
2567+
2568+
Assert.NotNull(document);
2569+
}
2570+
25262571
private static SwaggerGenerator Subject(
25272572
IEnumerable<ApiDescription> apiDescriptions,
25282573
SwaggerGeneratorOptions options = null,

0 commit comments

Comments
 (0)