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

Maintenance #207

Merged
merged 14 commits into from
Aug 20, 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 @@ -35,13 +35,6 @@ public string Generate()
sb.Append(codeDocumentationTagsGenerator.GenerateTags(0, parameters.DocumentationTags));
}

if (parameters.Authorization is not null)
{
sb.AppendLine(parameters.Authorization.UseAllowAnonymous
? "[AllowAnonymous]"
: "[Authorize]");
}

sb.AppendLine(codeAttributeGenerator.Generate());

sb.AppendLine($"public sealed class {parameters.ApiGroupName}EndpointDefinition : IEndpointDefinition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public string Generate()
sb.AppendLine();
sb.AppendLine(8, "app.UseEndpointDefinitions();");
sb.AppendLine();
sb.AppendLine(8, "app.MapGet(\"/\", () => TypedResults.Text(\"OK\", \"text/plain\")).ExcludeFromDescription();");
sb.AppendLine();
sb.AppendLine(8, "app.UseGlobalErrorHandler();");
sb.AppendLine();
sb.AppendLine(8, "// Enabling the status code pages middleware, will allow Problem Details to be used in some extra non-exception related framework scenarios,");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ public string Generate()
sb.AppendLine(8, "this IServiceCollection services)");
sb.AppendLine(4, "{");
sb.AppendLine(8, "services.AddEndpointsApiExplorer();");
sb.AppendLine(8, "services.AddApiVersioning(");
sb.AppendLine(16, "options =>");
sb.AppendLine(16, "{");
sb.AppendLine(20, "// Specify the default API Version");
sb.AppendLine(20, "options.DefaultApiVersion = new ApiVersion(1, 0);");
sb.AppendLine(8, "services");
sb.AppendLine(12, ".AddApiVersioning(options =>");
sb.AppendLine(12, "{");
sb.AppendLine(16, "// Specify the default API Version");
sb.AppendLine(16, "options.DefaultApiVersion = new ApiVersion(1, 0);");
sb.AppendLine();
sb.AppendLine(20, "// If the client hasn't specified the API version in the request, use the default API version number");
sb.AppendLine(20, "options.AssumeDefaultVersionWhenUnspecified = true;");
sb.AppendLine(16, "// If the client hasn't specified the API version in the request, use the default API version number");
sb.AppendLine(16, "options.AssumeDefaultVersionWhenUnspecified = true;");
sb.AppendLine();
sb.AppendLine(20, "// reporting api versions will return the headers");
sb.AppendLine(20, "// \"api-supported-versions\" and \"api-deprecated-versions\"");
sb.AppendLine(20, "options.ReportApiVersions = true;");
sb.AppendLine(16, "// reporting api versions will return the headers");
sb.AppendLine(16, "// \"api-supported-versions\" and \"api-deprecated-versions\"");
sb.AppendLine(16, "options.ReportApiVersions = true;");
sb.AppendLine();
sb.AppendLine(20, "//// DEFAULT Version reader is QueryStringApiVersionReader();");
sb.AppendLine(20, "//// clients request the specific version using the x-api-version header");
sb.AppendLine(20, "//// Supporting multiple versioning scheme");
sb.AppendLine(20, "options.ApiVersionReader = ApiVersionReader.Combine(");
sb.AppendLine(24, "new HeaderApiVersionReader(ApiVersionConstants.ApiVersionHeaderParameter),");
sb.AppendLine(24, "new MediaTypeApiVersionReader(ApiVersionConstants.ApiVersionMediaTypeParameter),");
sb.AppendLine(24, "new QueryStringApiVersionReader(ApiVersionConstants.ApiVersionQueryParameter),");
sb.AppendLine(24, "new QueryStringApiVersionReader(ApiVersionConstants.ApiVersionQueryParameterShort),");
sb.AppendLine(24, "new UrlSegmentApiVersionReader());");
sb.AppendLine(16, "})");
sb.AppendLine(16, "//// DEFAULT Version reader is QueryStringApiVersionReader();");
sb.AppendLine(16, "//// clients request the specific version using the x-api-version header");
sb.AppendLine(16, "//// Supporting multiple versioning scheme");
sb.AppendLine(16, "options.ApiVersionReader = ApiVersionReader.Combine(");
sb.AppendLine(20, "new HeaderApiVersionReader(ApiVersionConstants.ApiVersionHeaderParameter),");
sb.AppendLine(20, "new MediaTypeApiVersionReader(ApiVersionConstants.ApiVersionMediaTypeParameter),");
sb.AppendLine(20, "new QueryStringApiVersionReader(ApiVersionConstants.ApiVersionQueryParameter),");
sb.AppendLine(20, "new QueryStringApiVersionReader(ApiVersionConstants.ApiVersionQueryParameterShort),");
sb.AppendLine(20, "new UrlSegmentApiVersionReader());");
sb.AppendLine(12, "})");
sb.AppendLine(12, ".AddApiExplorer(options =>");
sb.AppendLine(12, "{");
sb.AppendLine(16, "// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service");
Expand All @@ -60,7 +60,8 @@ public string Generate()
sb.AppendLine(4, "public static void ConfigureSwagger(");
sb.AppendLine(8, "this IServiceCollection services)");
sb.AppendLine(4, "{");
sb.AppendLine(8, "services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();");
sb.AppendLine(8, "services.ConfigureOptions<ConfigureSwaggerDocOptions>();");
sb.AppendLine();
sb.AppendLine(8, "services.AddSwaggerGen(options =>");
sb.AppendLine(8, "{");
sb.AppendLine(12, "options.OperationFilter<SwaggerDefaultValues>();");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
namespace Atc.Rest.ApiGenerator.Framework.Minimal.ContentGenerators;

public class ContentGeneratorServeWebApplicationExtensions : IContentGenerator
public class ContentGeneratorServerWebApplicationExtensions : IContentGenerator
{
private readonly ContentGeneratorBaseParameters parameters;

public ContentGeneratorServeWebApplicationExtensions(
public ContentGeneratorServerWebApplicationExtensions(
ContentGeneratorBaseParameters parameters)
{
this.parameters = parameters;
Expand All @@ -21,7 +21,7 @@ public string Generate()
sb.AppendLine();
sb.AppendLine("public static class WebApplicationExtensions");
sb.AppendLine("{");
sb.AppendLine(4, "private static readonly string[] PatchHttpMethods = { \"patch\" };");
sb.AppendLine(4, "private static readonly string[] PatchHttpMethods = [\"patch\"];");
sb.AppendLine();
sb.AppendLine(4, "public static RouteHandlerBuilder MapPatch(");
sb.AppendLine(8, "this WebApplication app,");
Expand All @@ -39,6 +39,11 @@ public string Generate()
sb.AppendLine(8, "this WebApplication app,");
sb.AppendLine(8, "string applicationName)");
sb.AppendLine(4, "{");
sb.AppendLine(8, "if (!app.Environment.IsDevelopment())");
sb.AppendLine(8, "{");
sb.AppendLine(12, "return app;");
sb.AppendLine(8, "}");
sb.AppendLine();
sb.AppendLine(8, "app.UseSwagger();");
sb.AppendLine(8, "app.UseSwaggerUI(options =>");
sb.AppendLine(8, "{");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void ScaffoldWebApplicationBuilderExtensions()
public void ScaffoldWebApplicationExtensions(
SwaggerThemeMode swaggerThemeMode)
{
var contentGenerator = new ContentGenerators.ContentGeneratorServeWebApplicationExtensions(
var contentGenerator = new ContentGenerators.ContentGeneratorServerWebApplicationExtensions(
new ContentGeneratorBaseParameters(Namespace: projectName))
{
SwaggerThemeMode = swaggerThemeMode,
Expand All @@ -181,29 +181,6 @@ public void ScaffoldWebApplicationExtensions(
overrideIfExist: false);
}

public void ScaffoldConfigureSwaggerOptions()
{
var fullNamespace = $"{projectName}";

var contentGeneratorServerSwaggerDocOptionsParameters = ContentGeneratorServerSwaggerDocOptionsParameterFactory
.Create(
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new ContentGeneratorServeConfigureSwaggerOptions(
contentGeneratorServerSwaggerDocOptionsParameters);

var content = contentGenerator.Generate();

var contentWriter = new ContentWriter(logger);
contentWriter.Write(
projectPath,
projectPath.CombineFileInfo("Options", "ConfigureSwaggerOptions.cs"),
ContentWriterArea.Src,
content,
overrideIfExist: false);
}

public void ScaffoldProgramFile(
SwaggerThemeMode swaggerThemeMode)
{
Expand Down Expand Up @@ -251,7 +228,7 @@ public void GenerateConfigureSwaggerDocOptions()
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new ContentGeneratorServerSwaggerDocOptions(
var contentGenerator = new ContentGeneratorServerConfigureSwaggerDocOptions(
new GeneratedCodeHeaderGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
new GeneratedCodeAttributeGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
contentGeneratorServerSwaggerDocOptionsParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,6 @@ public void ScaffoldWebApplicationExtensions(
SwaggerThemeMode swaggerThemeMode)
=> throw new NotSupportedException($"{nameof(ScaffoldWebApplicationExtensions)} is not supported for MVC");

public void ScaffoldConfigureSwaggerOptions()
{
var fullNamespace = $"{projectName}";

var contentGeneratorServerSwaggerDocOptionsParameters = ContentGeneratorServerSwaggerDocOptionsParameterFactory
.Create(
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new ContentGeneratorServeConfigureSwaggerOptions(
contentGeneratorServerSwaggerDocOptionsParameters);

var content = contentGenerator.Generate();

var contentWriter = new ContentWriter(logger);
contentWriter.Write(
projectPath,
projectPath.CombineFileInfo("Options", "ConfigureSwaggerOptions.cs"),
ContentWriterArea.Src,
content,
overrideIfExist: false);
}

public void ScaffoldProgramFile(
SwaggerThemeMode swaggerThemeMode)
{
Expand Down Expand Up @@ -204,7 +181,7 @@ public void GenerateConfigureSwaggerDocOptions()
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new ContentGeneratorServerSwaggerDocOptions(
var contentGenerator = new ContentGeneratorServerConfigureSwaggerDocOptions(
new GeneratedCodeHeaderGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
new GeneratedCodeAttributeGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
contentGeneratorServerSwaggerDocOptionsParameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@
namespace Atc.Rest.ApiGenerator.Framework.ContentGenerators.Server;

public class ContentGeneratorServeConfigureSwaggerOptions : IContentGenerator
public sealed class ContentGeneratorServerConfigureSwaggerDocOptions : IContentGenerator
{
private readonly GeneratedCodeHeaderGenerator codeHeaderGenerator;
private readonly GeneratedCodeAttributeGenerator codeAttributeGenerator;
private readonly ContentGeneratorServerSwaggerDocOptionsParameters parameters;

public ContentGeneratorServeConfigureSwaggerOptions(
public ContentGeneratorServerConfigureSwaggerDocOptions(
GeneratedCodeHeaderGenerator codeHeaderGenerator,
GeneratedCodeAttributeGenerator codeAttributeGenerator,
ContentGeneratorServerSwaggerDocOptionsParameters parameters)
{
this.codeHeaderGenerator = codeHeaderGenerator;
this.codeAttributeGenerator = codeAttributeGenerator;
this.parameters = parameters;
}

public string Generate()
{
var sb = new StringBuilder();

sb.Append(codeHeaderGenerator.Generate());
sb.AppendLine($"namespace {parameters.Namespace}.Options;"); // TODO: Move to constant
sb.AppendLine();
sb.AppendLine("public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>");
sb.AppendLine(codeAttributeGenerator.Generate());
sb.AppendLine("public class ConfigureSwaggerDocOptions : IConfigureOptions<SwaggerGenOptions>");
sb.AppendLine("{");
sb.AppendLine(4, "private readonly IApiVersionDescriptionProvider provider;");
sb.AppendLine(4, "private readonly IWebHostEnvironment environment;");
sb.AppendLine();
sb.AppendLine(4, "/// <summary>");
sb.AppendLine(4, "/// Initializes a new instance of the <see cref=\"ConfigureSwaggerOptions\"/> class.");
sb.AppendLine(4, "/// Initializes a new instance of the <see cref=\"ConfigureSwaggerDocOptions\"/> class.");
sb.AppendLine(4, "/// </summary>");
sb.AppendLine(4, "/// <param name=\"provider\">The <see cref=\"IApiVersionDescriptionProvider\">provider</see> used to generate Swagger documents.</param>");
sb.AppendLine(4, "/// <param name=\"environment\">The environment.</param>");
sb.AppendLine(4, "public ConfigureSwaggerOptions(");
sb.AppendLine(4, "public ConfigureSwaggerDocOptions(");
sb.AppendLine(8, "IApiVersionDescriptionProvider provider,");
sb.AppendLine(8, "IWebHostEnvironment environment)");
sb.AppendLine(4, "{");
sb.AppendLine(8, "this.provider = provider;");
sb.AppendLine(8, "this.environment = environment;");
sb.AppendLine(4, "}");
sb.AppendLine();
sb.AppendLine(4, "/// <inheritdoc />");
sb.AppendLine(4, "public void Configure(");
sb.AppendLine(8, "SwaggerGenOptions options)");
sb.AppendLine(4, "{");
sb.AppendLine(8, "// Add a swagger document for each discovered API version");
sb.AppendLine(8, "// note: you might choose to skip or document deprecated API versions differently");
sb.AppendLine(8, "foreach (var description in provider.ApiVersionDescriptions)");
sb.AppendLine(8, "{");
sb.AppendLine(12, "options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));");
sb.AppendLine(8, "}");
sb.AppendLine();
sb.AppendLine(8, "options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, \"xml\"));");
sb.AppendLine(4, "}");

sb.AppendLine();
sb.AppendLine(4, "private OpenApiInfo CreateInfoForApiVersion(");
sb.AppendLine(8, "ApiVersionDescription description)");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ void ScaffoldWebApplicationExtensions(

void GenerateConfigureSwaggerDocOptions();

void ScaffoldConfigureSwaggerOptions();

void ScaffoldProgramFile(
SwaggerThemeMode swaggerThemeMode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NugetPackageReferenceProvider(
{ "Microsoft.NETCore.Platforms", "7.0.4" },
{ "Microsoft.NET.Test.Sdk", "17.10.0" },
{ "NSubstitute", "5.1.0" },
{ "Swashbuckle.AspNetCore", "6.6.2" },
{ "Swashbuckle.AspNetCore", "6.7.0" },
{ "xunit", "2.8.1" },
{ "xunit.runner.visualstudio", "2.8.1" },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ public static bool IsUsingRequiredForMicrosoftAspNetCoreAuthorization(
continue;
}

var isOperationAuthenticationRequired =
apiOperationPair.Value.Extensions.ExtractAuthenticationRequired();
var isOperationAuthenticationRequired = apiOperationPair.Value.Extensions.ExtractAuthenticationRequired();
if (isOperationAuthenticationRequired is not null && isOperationAuthenticationRequired.Value)
{
return true;
Expand All @@ -394,8 +393,8 @@ public static bool IsUsingRequiredForMicrosoftAspNetCoreAuthorization(

var authenticationRoles = openApiPath.Value.Extensions.ExtractAuthorizationRoles();
var authenticationSchemes = openApiPath.Value.Extensions.ExtractAuthenticationSchemes();
if (authenticationRoles is not null && authenticationRoles.Count > 0 &&
authenticationSchemes is not null && authenticationSchemes.Count > 0)
if ((authenticationRoles is not null && authenticationRoles.Count > 0) ||
(authenticationSchemes is not null && authenticationSchemes.Count > 0))
{
return true;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Atc.Rest.ApiGenerator/Generators/ServerHostGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public async Task<bool> Generate()
projectOptions.ApiOptions.Generator.SwaggerThemeMode);
serverHostGeneratorMvc.ScaffoldStartupFile();
serverHostGeneratorMvc.ScaffoldWebConfig();
serverHostGeneratorMvc.ScaffoldConfigureSwaggerOptions();

serverHostGeneratorMvc.GenerateConfigureSwaggerDocOptions();

Expand Down Expand Up @@ -149,7 +148,6 @@ public async Task<bool> Generate()
serverHostGeneratorMinimalApi.ScaffoldWebApplicationBuilderExtensions();
serverHostGeneratorMinimalApi.ScaffoldWebApplicationExtensions(
projectOptions.ApiOptions.Generator.SwaggerThemeMode);
serverHostGeneratorMinimalApi.ScaffoldConfigureSwaggerOptions();

serverHostGeneratorMinimalApi.GenerateConfigureSwaggerDocOptions();

Expand Down
Loading
Loading