Skip to content

Commit

Permalink
feat: add SwaggerExampleFromExampleProviderAttribute (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr authored Aug 22, 2024
1 parent fff45fc commit 9a057d6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Altinn.Swashbuckle.Examples;
using System.Diagnostics.CodeAnalysis;

namespace Altinn.Swashbuckle.Filters;

/// <summary>
/// Attribute for specifying that a type should have example generated using the <see cref="IExampleDataProvider{TSelf}"/> machinery.
/// </summary>
[ExcludeFromCodeCoverage]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = false)]
public sealed class SwaggerExampleFromExampleProviderAttribute
: Attribute
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Altinn.Swashbuckle.Filters;
/// <summary>
/// Attribute for specifying that a type should be represented as a string in the OpenAPI schema.
/// </summary>
[ExcludeFromCodeCoverage]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, AllowMultiple = false)]
public sealed class SwaggerStringAttribute
: Attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Extension methods for adding example-data to the generated OpenAPI spec.
/// </summary>
[ExcludeFromCodeCoverage]
public static class AltinnSwashbuckleServiceCollectionExtensions
{
/// <summary>
Expand All @@ -32,14 +34,18 @@ public static OptionsBuilder<ExampleDataOptions> AddOpenApiExampleProvider(this
/// <returns><paramref name="services"/>.</returns>
public static IServiceCollection AddSwaggerFilterAttributeSupport(this IServiceCollection services)
{
services.AddOpenApiExampleProvider();

services.AddSingleton<SchemaFilterAttributeFilter>();
services.AddSingleton<SwaggerStringAttributeFilter>();
services.AddSingleton<SwaggerExampleFromExampleProviderFilter>();

services.AddOptions<SwaggerGenOptions>()
.Configure((SwaggerGenOptions options, IServiceProvider s) =>
{
options.SchemaGeneratorOptions.SchemaFilters.Add(s.GetRequiredService<SchemaFilterAttributeFilter>());
options.SchemaGeneratorOptions.SchemaFilters.Add(s.GetRequiredService<SwaggerStringAttributeFilter>());
options.SchemaGeneratorOptions.SchemaFilters.Add(s.GetRequiredService<SwaggerExampleFromExampleProviderFilter>());
});

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Altinn.Swashbuckle.Examples;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Diagnostics.CodeAnalysis;

namespace Altinn.Swashbuckle.Filters;

[ExcludeFromCodeCoverage]
internal class SwaggerExampleFromExampleProviderFilter
: AttributeFilter<SwaggerExampleFromExampleProviderAttribute>
{
private readonly OpenApiExampleProvider _provider;

public SwaggerExampleFromExampleProviderFilter(OpenApiExampleProvider provider)
{
_provider = provider;
}

protected override void Apply(SwaggerExampleFromExampleProviderAttribute attribute, OpenApiSchema schema, SchemaFilterContext context)
{
schema.Example ??= _provider.GetExample(context.Type)?.FirstOrDefault();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Diagnostics.CodeAnalysis;

namespace Altinn.Swashbuckle.Filters;

[ExcludeFromCodeCoverage]
internal class SwaggerStringAttributeFilter
: AttributeFilter<SwaggerStringAttribute>
{
Expand Down

0 comments on commit 9a057d6

Please sign in to comment.