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

Allow no match to be found to avoid throwing an exception #3188

Merged
merged 2 commits into from
Dec 9, 2024
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 @@ -443,7 +443,7 @@ private async Task<OpenApiOperation> GenerateOpenApiOperationFromMetadataAsync(A
}
else
{
bodyParameterDescription = apiDescription.ParameterDescriptions.Single(desc => desc.IsFromBody());
bodyParameterDescription = apiDescription.ParameterDescriptions.SingleOrDefault(desc => desc.IsFromBody());
if (bodyParameterDescription is not null)
{
contentTypeValue.Schema = GenerateSchema(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,46 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithSt
Assert.Equal(ParameterStyle.Form, content.Value.Encoding["param"].Style);
}

[Fact]
public void GetSwagger_OpenApiOperationWithRawContent_IsHandled()
{
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameter));
var actionDescriptor = new ActionDescriptor
{
EndpointMetadata = new List<object>()
{
new OpenApiOperation()
{
RequestBody = new OpenApiRequestBody()
{
Content = new Dictionary<string, OpenApiMediaType>()
{
{ "text/plain", new OpenApiMediaType() }
}
}
}
},
RouteValues = new Dictionary<string, string>
{
["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty)
}
};
var subject = Subject(
apiDescriptions: new[]
{
ApiDescriptionFactory.Create(actionDescriptor, methodInfo, groupName: "v1", httpMethod: "POST", relativePath: "resource"),
}
);

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

Assert.Equal("V1", document.Info.Version);
Assert.Equal("Test API", document.Info.Title);
Assert.Equal(new[] { "/resource" }, document.Paths.Keys.ToArray());
Assert.Equal(new[] { OperationType.Post }, document.Paths["/resource"].Operations.Keys);
Assert.Single(document.Paths["/resource"].Operations);
}

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