Skip to content

Commit 21b5f47

Browse files
committed
[Swashbuckle] RequiredMemberEvaluator can be configured.
1 parent 462c798 commit 21b5f47

File tree

34 files changed

+2579
-33
lines changed

34 files changed

+2579
-33
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>8.8.1</VersionPrefix>
5+
<VersionPrefix>8.9.0</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

samples/AspNetCore.Samples/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ private static Task StartMinimalWebApiAsync(ILoggerFactory loggerFactory)
169169

170170
var app = builder.Build();
171171

172+
app.MapSwagger();
173+
172174
var routeGroup = app.MapGroup("/api");
173175

174176
routeGroup.MapGet("category/{category}", (ProductCategory category) => new { Value = category, category.IsValid });

src/Thinktecture.Runtime.Extensions.SourceGenerator/Extensions/NamedTypeSymbolExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static IReadOnlyList<ContainingTypeState> GetContainingTypes(
6262
this INamedTypeSymbol type)
6363
{
6464
if (type.ContainingType is null)
65-
return Array.Empty<ContainingTypeState>();
65+
return [];
6666

6767
var types = new List<ContainingTypeState>();
6868
var containingType = type.ContainingType;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
using Microsoft.OpenApi.Models;
3+
using Swashbuckle.AspNetCore.SwaggerGen;
4+
5+
namespace Thinktecture.Swashbuckle.Internal.ComplexValueObjects;
6+
7+
/// <summary>
8+
/// This is an internal API that supports the Thinktecture.Runtime.Extensions infrastructure and not subject to
9+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
10+
/// any release. You should only use it directly in your code with extreme caution and knowing that
11+
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
12+
/// </summary>
13+
public class AllRequiredMemberEvaluator : IRequiredMemberEvaluator
14+
{
15+
/// <inheritdoc />
16+
public bool IsRequired(OpenApiSchema schema, SchemaFilterContext context, MemberInfo member)
17+
{
18+
return true;
19+
}
20+
}

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/Internal/ComplexValueObjects/ComplexValueObjectSchemaFilter.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Reflection;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
using Microsoft.Extensions.Options;
16
using Microsoft.OpenApi.Models;
27
using Swashbuckle.AspNetCore.SwaggerGen;
38
using Thinktecture.Internal;
@@ -13,16 +18,21 @@ namespace Thinktecture.Swashbuckle.Internal.ComplexValueObjects;
1318
public class ComplexValueObjectSchemaFilter : IInternalComplexValueObjectSchemaFilter
1419
{
1520
private readonly IRequiredMemberEvaluator _requiredMemberEvaluator;
21+
private readonly JsonSerializerOptions _jsonSerializerOptions;
1622

1723
/// <summary>
1824
/// This is an internal API that supports the Thinktecture.Runtime.Extensions infrastructure and not subject to
1925
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
2026
/// any release. You should only use it directly in your code with extreme caution and knowing that
2127
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
2228
/// </summary>
23-
public ComplexValueObjectSchemaFilter(IRequiredMemberEvaluator requiredMemberEvaluator)
29+
public ComplexValueObjectSchemaFilter(
30+
IServiceProvider serviceProvider,
31+
JsonSerializerOptionsResolver jsonSerializerOptionsResolver,
32+
IOptions<ThinktectureSchemaFilterOptions> options)
2433
{
25-
_requiredMemberEvaluator = requiredMemberEvaluator;
34+
_requiredMemberEvaluator = options.Value.RequiredMemberEvaluator.CreateEvaluator(serviceProvider);
35+
_jsonSerializerOptions = jsonSerializerOptionsResolver.JsonSerializerOptions;
2636
}
2737

2838
/// <inheritdoc />
@@ -39,8 +49,14 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context, Metadata.Co
3949
{
4050
foreach (var memberInfo in metadata.AssignableMembers)
4151
{
42-
if (_requiredMemberEvaluator.IsRequired(schema, context, memberInfo))
43-
schema.Required.Add(memberInfo.Name);
52+
if (memberInfo.GetCustomAttribute<RequiredAttribute>() is null && _requiredMemberEvaluator.IsRequired(schema, context, memberInfo))
53+
{
54+
var name = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name
55+
?? _jsonSerializerOptions.PropertyNamingPolicy?.ConvertName(memberInfo.Name)
56+
?? memberInfo.Name;
57+
58+
schema.Required.Add(name);
59+
}
4460
}
4561
}
4662
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
using Microsoft.OpenApi.Models;
3+
using Swashbuckle.AspNetCore.SwaggerGen;
4+
5+
namespace Thinktecture.Swashbuckle.Internal.ComplexValueObjects;
6+
7+
/// <summary>
8+
/// This is an internal API that supports the Thinktecture.Runtime.Extensions infrastructure and not subject to
9+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
10+
/// any release. You should only use it directly in your code with extreme caution and knowing that
11+
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
12+
/// </summary>
13+
public class NoRequiredMemberEvaluator : IRequiredMemberEvaluator
14+
{
15+
/// <inheritdoc />
16+
public bool IsRequired(OpenApiSchema schema, SchemaFilterContext context, MemberInfo member)
17+
{
18+
return false;
19+
}
20+
}

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/Internal/SmartEnums/AllOfSmartEnumSchemaFilter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Options;
12
using Microsoft.OpenApi.Models;
23

34
namespace Thinktecture.Swashbuckle.Internal.SmartEnums;
@@ -17,9 +18,10 @@ public class AllOfSmartEnumSchemaFilter : SmartEnumSchemaFilterBase
1718
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
1819
/// </summary>
1920
public AllOfSmartEnumSchemaFilter(
21+
IServiceProvider serviceProvider,
2022
IOpenApiValueFactoryProvider valueFactoryProvider,
21-
ISmartEnumSchemaExtension extension)
22-
: base(valueFactoryProvider, extension)
23+
IOptions<ThinktectureSchemaFilterOptions> options)
24+
: base(serviceProvider, valueFactoryProvider, options)
2325
{
2426
}
2527

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/Internal/SmartEnums/AnyOfSmartEnumSchemaFilter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Options;
12
using Microsoft.OpenApi.Models;
23

34
namespace Thinktecture.Swashbuckle.Internal.SmartEnums;
@@ -17,9 +18,10 @@ public class AnyOfSmartEnumSchemaFilter : SmartEnumSchemaFilterBase
1718
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
1819
/// </summary>
1920
public AnyOfSmartEnumSchemaFilter(
21+
IServiceProvider serviceProvider,
2022
IOpenApiValueFactoryProvider valueFactoryProvider,
21-
ISmartEnumSchemaExtension extension)
22-
: base(valueFactoryProvider, extension)
23+
IOptions<ThinktectureSchemaFilterOptions> options)
24+
: base(serviceProvider, valueFactoryProvider, options)
2325
{
2426
}
2527

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/Internal/SmartEnums/DefaultSmartEnumSchemaFilter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Options;
12
using Microsoft.OpenApi.Models;
23

34
namespace Thinktecture.Swashbuckle.Internal.SmartEnums;
@@ -17,9 +18,10 @@ public class DefaultSmartEnumSchemaFilter : SmartEnumSchemaFilterBase
1718
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
1819
/// </summary>
1920
public DefaultSmartEnumSchemaFilter(
21+
IServiceProvider serviceProvider,
2022
IOpenApiValueFactoryProvider valueFactoryProvider,
21-
ISmartEnumSchemaExtension extension)
22-
: base(valueFactoryProvider, extension)
23+
IOptions<ThinktectureSchemaFilterOptions> options)
24+
: base(serviceProvider, valueFactoryProvider, options)
2325
{
2426
}
2527

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/Internal/SmartEnums/OneOfSmartEnumSchemaFilter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Options;
12
using Microsoft.OpenApi.Models;
23

34
namespace Thinktecture.Swashbuckle.Internal.SmartEnums;
@@ -17,9 +18,10 @@ public class OneOfSmartEnumSchemaFilter : SmartEnumSchemaFilterBase
1718
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
1819
/// </summary>
1920
public OneOfSmartEnumSchemaFilter(
21+
IServiceProvider serviceProvider,
2022
IOpenApiValueFactoryProvider valueFactoryProvider,
21-
ISmartEnumSchemaExtension extension)
22-
: base(valueFactoryProvider, extension)
23+
IOptions<ThinktectureSchemaFilterOptions> options)
24+
: base(serviceProvider, valueFactoryProvider, options)
2325
{
2426
}
2527

0 commit comments

Comments
 (0)