Skip to content

Commit

Permalink
Add manual ewndpoint metadata annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Feb 20, 2024
1 parent 56854b1 commit 27f9cbb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/PackageRegistryService/API/EndpointsV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,28 @@ public static RouteGroupBuilder MapApiV1(this RouteGroupBuilder group)
{
group.MapGet("/packages", GetAllPackages)
.WithOpenApi()
.WithName("GetAllPackages")
.WithSummary("This is a summary")
.WithDescription("This is a description");
.WithName("GetAllPackages");
//.WithSummary("This is a summary") // not working currently, fixed via document processor in /OpenAPI/DocGen.cs
//.WithDescription("This is a description");

group.MapGet("/packages/{name}", GetLatestPackageByName)
.WithOpenApi()
.WithName("GetLatestPackageByName")
.WithSummary("This is a summary")
.WithDescription("This is a description");
.WithName("GetLatestPackageByName");
//.WithSummary("This is a summary") // not working currently, fixed via document processor in /OpenAPI/DocGen.cs
//.WithDescription("This is a description");

group.MapGet("/packages/{name}/{version}", GetPackageByNameAndVersion)
.WithOpenApi()
.WithName("GetPackageByNameAndVersion")
.WithSummary("This is a summary")
.WithDescription("This is a description");
.WithName("GetPackageByNameAndVersion");
//.WithSummary("This is a summary") // not working currently, fixed via document processor in /OpenAPI/DocGen.cs
//.WithDescription("This is a description");

group.MapPost("/packages", CreatePackage)
.WithOpenApi()
.WithName("CreatePackage")
.WithSummary("This is a summary")
.WithDescription("This is a description")
.AddEndpointFilter<APIKeyEndpointFilter>(); // creating packages via post requests requires an API key
//.WithSummary("This is a summary") // not working currently, fixed via document processor in /OpenAPI/DocGen.cs
//.WithDescription("This is a description");

return group;
}
Expand Down
3 changes: 3 additions & 0 deletions src/PackageRegistryService/OpenAPI/DocGen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NSwag.Generation.AspNetCore;
using NSwag.Generation.Processors;

namespace PackageRegistryService.OpenAPI
{
Expand Down Expand Up @@ -29,6 +30,8 @@ public static void GeneratorSetup (AspNetCoreOpenApiDocumentGeneratorSettings se
secureEndpointIds: ["CreatePackage"]
)
);
// fix for WithDescription and WithSummary methods not working with nswag and minimal API endpoints
settings.OperationProcessors.Add(new OperationMetadataProcessor());
}
}
}
56 changes: 56 additions & 0 deletions src/PackageRegistryService/OpenAPI/OperationMetadataProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using NSwag;
using NSwag.Generation.Processors;
using NSwag.Generation.Processors.Contexts;

namespace PackageRegistryService.OpenAPI
{
public class OperationMetadataProcessor : IOperationProcessor
{
public Dictionary<string, Dictionary<string,string>> EndpointMetadata = new Dictionary<string, Dictionary<string,string>>
{
{
"CreatePackage", new Dictionary<string, string>
{
{ "Summary", "Submit a new validation package" },
{ "Description", "Submit a new validation package to the package registry. This Endpoint requires API Key authentication." }
}
},
{
"GetAllPackages", new Dictionary<string, string>
{
{ "Summary", "Get all validation packages" },
{ "Description", "Get all validation packages from the package registry. Note that this endpoint returns all versions of each package. Package content is a base64 encoded byte array containing the package executable." }
}
},
{
"GetLatestPackageByName", new Dictionary<string, string>
{
{ "Summary", "Get the latest version of a validation package" },
{ "Description", "Get the latest version of a validation package from the package registry. Package content is a base64 encoded byte array containing the package executable." }
}
},
{
"GetPackageByNameAndVersion", new Dictionary<string, string>
{
{ "Summary", "Get a specific version of a validation package" },
{ "Description", "Get a specific version of a validation package from the package registry. Package content is a base64 encoded byte array containing the package executable." }
}
}
};

public bool Process(OperationProcessorContext operationProcessorContext)
{
foreach (OpenApiOperationDescription operationDescription in operationProcessorContext.AllOperationDescriptions)
{
var op = operationDescription.Operation;
if (EndpointMetadata.ContainsKey(op.OperationId)) {
op.Summary = EndpointMetadata[op.OperationId]["Summary"];
op.Description = EndpointMetadata[op.OperationId]["Description"];
}
}

return true;
}

}
}

0 comments on commit 27f9cbb

Please sign in to comment.