Skip to content
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

Fix for ApiDescriptionProvider throws NRE #3280

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private OpenApiParameter GenerateParameterWithoutFilter(

private static ParameterStyle? GetParameterStyle(Type type, BindingSource source)
{
return source == BindingSource.Query && type.IsGenericType &&
return source == BindingSource.Query && type?.IsGenericType == true &&
typeof(IEnumerable<KeyValuePair<string, string>>).IsAssignableFrom(type)
? ParameterStyle.DeepObject
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void GetSwagger_GeneratesSwaggerDocument_ForApiDescriptionsWithMatchingGr
c => nameof(c.ActionWithNoParameters), groupName: "v1", httpMethod: "GET", relativePath: "resource"),

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

[Fact]
public void GetSwagger_BindingSourceQueryParameter_NotThrowsException()
{
var apiDescription = new ApiDescription
{
HttpMethod = "GET",
ActionDescriptor = new ActionDescriptor
{
RouteValues = new Dictionary<string, string>
{
["controller"] = "Catalog"
}
},
RelativePath = "api/v1/Images/{image}",
GroupName = "v1",
ParameterDescriptions =
{
new ApiParameterDescription
{
Name = "width",
Source = BindingSource.Query,
DefaultValue = string.Empty,
Type = typeof(int)
}
}
};
var subject = Subject(
apiDescriptions:
[
apiDescription
],
options: new SwaggerGeneratorOptions
{
SwaggerDocs = new Dictionary<string, OpenApiInfo>
{
["v1"] = new() { Version = "V1", Title = "Test API" }
}
}
);

var document = subject.GetSwagger("v1");

Assert.NotNull(document);
}

private static SwaggerGenerator Subject(
IEnumerable<ApiDescription> apiDescriptions,
SwaggerGeneratorOptions options = null,
Expand Down