Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Feb 11, 2024
1 parent b3aec0e commit 634d2cd
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# v3.0.0
- Add support for using `<remarks>` on schema XML comments.
- Adds support for using `<remarks>` on schema XML comments.
When using `<remarks>` on a class, it will be used as the description for the schema.
When there is also `<summary>` defined, the `<summary>` will be used as the title for the schema.
When `<summary>` is defined alone it is used as description for the schema.
- Add support for defining examples using `SwaggerSchemaAttribute`.
- Adds support for defining examples using `SwaggerSchemaAttribute`.
- Adds support for custom Required and Binding attributes
- Fixes an issue where UniqueItems is not set true for `IReadOnlySet<T>`
- Adds support for `WithSummary` and `WithDescription` metadata


# v2.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.OpenApi.Models;
using DotSwashbuckle.AspNetCore.Swagger;
using Microsoft.AspNetCore.Http.Metadata;

namespace DotSwashbuckle.AspNetCore.SwaggerGen
{
Expand Down Expand Up @@ -212,7 +213,9 @@ private OpenApiOperation GenerateOperation(ApiDescription apiDescription, Schema
Parameters = GenerateParameters(apiDescription, schemaRepository),
RequestBody = GenerateRequestBody(apiDescription, schemaRepository),
Responses = GenerateResponses(apiDescription, schemaRepository),
Deprecated = apiDescription.CustomAttributes().OfType<ObsoleteAttribute>().Any()
Deprecated = apiDescription.CustomAttributes().OfType<ObsoleteAttribute>().Any(),
Summary = GenerateSummary(apiDescription),
Description = GenerateDescription(apiDescription),
};

apiDescription.TryGetMethodInfo(out MethodInfo methodInfo);
Expand All @@ -232,6 +235,30 @@ private OpenApiOperation GenerateOperation(ApiDescription apiDescription, Schema
}
}

private string GenerateSummary(ApiDescription apiDescription)
{
string operationSummary = apiDescription
.ActionDescriptor
?.EndpointMetadata
?.OfType<IEndpointSummaryMetadata>()
.Select(s => s.Summary)
.LastOrDefault();

return operationSummary;
}

private string GenerateDescription(ApiDescription apiDescription)
{
string operationDescription = apiDescription
.ActionDescriptor
?.EndpointMetadata
?.OfType<IEndpointDescriptionMetadata>()
.Select(s => s.Description)
.LastOrDefault();

return operationDescription;
}

private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription apiDescription, SchemaRepository schemaRepository)
{
var metadata = apiDescription.ActionDescriptor?.EndpointMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,54 @@ public void GetSwagger_SetsParameterTypeToString_IfApiParameterHasNoCorrespondin
Assert.Equal("string", operation.Parameters.First().Schema.Type);
}

[Fact]
public void GetSwagger_CanReadEndpointSummaryFromMetadata()
{
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameter));
var actionDescriptor = new ActionDescriptor
{
EndpointMetadata = new List<object>() { new EndpointSummaryAttribute("A Test Summary") },
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("A Test Summary", document.Paths["/resource"].Operations[OperationType.Post].Summary);
}

[Fact]
public void GetSwagger_CanReadEndpointDescriptionFromMetadata()
{
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithParameter));
var actionDescriptor = new ActionDescriptor
{
EndpointMetadata = new List<object>() { new EndpointDescriptionAttribute("A Test Description") },
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("A Test Description", document.Paths["/resource"].Operations[OperationType.Post].Description);
}

[Fact]
public void GetSwagger_GeneratesRequestBody_ForFirstApiParameterThatIsBoundToBody()
{
Expand Down

0 comments on commit 634d2cd

Please sign in to comment.