Skip to content

Commit

Permalink
test: fix tests around ConfigureSwaggerDocOptions & ConfigureSwaggerO…
Browse files Browse the repository at this point in the history
…ptions
  • Loading branch information
davidkallesen committed Aug 6, 2024
1 parent fa6809a commit 1100b4f
Show file tree
Hide file tree
Showing 36 changed files with 1,152 additions and 250 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global using System.CodeDom.Compiler;
global using System.Reflection;
global using System.Text;

global using Asp.Versioning.ApiExplorer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -11,29 +11,15 @@ public class ConfigureSwaggerDocOptions : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerDocOptions(
IApiVersionDescriptionProvider provider)
=> this.provider = provider;

public void Configure(
SwaggerGenOptions options)
{
foreach (var version in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(
version.GroupName,
new OpenApiInfo
{
Version = "1.0",
Title = "Demo Sample Api",
Description = "Demo Sample Api - SingleFileVersion",
Contact = new OpenApiContact
{
Name = "atc-net A/S",
},
});
}

options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
}
=> options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace DemoSample.Api.Options;

public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;
private readonly IWebHostEnvironment environment;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
/// <param name="environment">The environment.</param>
public ConfigureSwaggerOptions(
IApiVersionDescriptionProvider provider,
IWebHostEnvironment environment)
{
this.provider = provider;
this.environment = environment;
}

/// <inheritdoc />
public void Configure(
SwaggerGenOptions options)
{
// Add a swagger document for each discovered API version
// note: you might choose to skip or document deprecated API versions differently
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
}
}

private OpenApiInfo CreateInfoForApiVersion(
ApiVersionDescription description)
{
var text = new StringBuilder("Demo Sample Api - SingleFileVersion");
var info = new OpenApiInfo
{
Title = $"{environment.ApplicationName} {description.GroupName.ToUpperInvariant()}",
Version = description.ApiVersion.ToString(),
Contact = new OpenApiContact
{
Name = "atc-net A/S",
},
};

if (description.IsDeprecated)
{
text.Append(" This API version has been deprecated.");
}

if (description.SunsetPolicy is { } policy)
{
if (policy.Date is { } when)
{
text.Append(" The API will be sunset on ")
.Append(when.Date.ToShortDateString())
.Append('.');
}

if (policy.HasLinks)
{
text.AppendLine();

foreach (var link in policy.Links)
{
if (link.Type != "text/html")
{
continue;
}

text.AppendLine();

if (link.Title.HasValue)
{
text.Append(link.Title.Value).Append(": ");
}

text.Append(link.LinkTarget.OriginalString);
}
}
}

info.Description = text.ToString();

return info;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global using System.CodeDom.Compiler;
global using System.Reflection;
global using System.Text;

global using Asp.Versioning.ApiExplorer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -11,29 +11,15 @@ public class ConfigureSwaggerDocOptions : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerDocOptions(
IApiVersionDescriptionProvider provider)
=> this.provider = provider;

public void Configure(
SwaggerGenOptions options)
{
foreach (var version in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(
version.GroupName,
new OpenApiInfo
{
Version = "1.0",
Title = "Demo Sample Api",
Description = "Demo Sample Api - SingleFileVersion",
Contact = new OpenApiContact
{
Name = "atc-net A/S",
},
});
}

options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
}
=> options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace DemoSample.Api.Options;

public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;
private readonly IWebHostEnvironment environment;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
/// <param name="environment">The environment.</param>
public ConfigureSwaggerOptions(
IApiVersionDescriptionProvider provider,
IWebHostEnvironment environment)
{
this.provider = provider;
this.environment = environment;
}

/// <inheritdoc />
public void Configure(
SwaggerGenOptions options)
{
// Add a swagger document for each discovered API version
// note: you might choose to skip or document deprecated API versions differently
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
}
}

private OpenApiInfo CreateInfoForApiVersion(
ApiVersionDescription description)
{
var text = new StringBuilder("Demo Sample Api - SingleFileVersion");
var info = new OpenApiInfo
{
Title = $"{environment.ApplicationName} {description.GroupName.ToUpperInvariant()}",
Version = description.ApiVersion.ToString(),
Contact = new OpenApiContact
{
Name = "atc-net A/S",
},
};

if (description.IsDeprecated)
{
text.Append(" This API version has been deprecated.");
}

if (description.SunsetPolicy is { } policy)
{
if (policy.Date is { } when)
{
text.Append(" The API will be sunset on ")
.Append(when.Date.ToShortDateString())
.Append('.');
}

if (policy.HasLinks)
{
text.AppendLine();

foreach (var link in policy.Links)
{
if (link.Type != "text/html")
{
continue;
}

text.AppendLine();

if (link.Title.HasValue)
{
text.Append(link.Title.Value).Append(": ");
}

text.Append(link.LinkTarget.OriginalString);
}
}
}

info.Description = text.ToString();

return info;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global using System.CodeDom.Compiler;
global using System.Reflection;
global using System.Text;

global using Asp.Versioning.ApiExplorer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -11,29 +11,15 @@ public class ConfigureSwaggerDocOptions : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerDocOptions(
IApiVersionDescriptionProvider provider)
=> this.provider = provider;

public void Configure(
SwaggerGenOptions options)
{
foreach (var version in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(
version.GroupName,
new OpenApiInfo
{
Version = "1.0",
Title = "Example-Response-Types",
Description = "Example With All Response Types Api",
Contact = new OpenApiContact
{
Name = "atc-net A/S",
},
});
}

options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
}
=> options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
Loading

0 comments on commit 1100b4f

Please sign in to comment.