Skip to content

Commit

Permalink
Allow no match to be found to avoid throwing an exception (#3188)
Browse files Browse the repository at this point in the history
Allow no match to be found to avoid throwing an exception for raw content.
  • Loading branch information
moni-dips authored Dec 9, 2024
1 parent 3832d77 commit fc77e5a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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

0 comments on commit fc77e5a

Please sign in to comment.