-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SwaggerExampleFromExampleProviderAttribute (#123)
- Loading branch information
Showing
5 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...src/Altinn.Swashbuckle.Abstractions/Filters/SwaggerExampleFromExampleProviderAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...inn.Swashbuckle/src/Altinn.Swashbuckle/Filters/SwaggerExampleFromExampleProviderFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Altinn.Swashbuckle/src/Altinn.Swashbuckle/Filters/SwaggerStringAttributeFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters