Skip to content

Commit 067e5e1

Browse files
committed
Add new schema filter to exclude nullable fields from required
1 parent c672d5a commit 067e5e1

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/Bss.Platform.Api.Documentation/DependencyInjection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static IServiceCollection AddPlatformApiDocumentation(
3232
{
3333
x.SwaggerDoc("api", new OpenApiInfo { Title = title });
3434
x.SchemaFilter<XEnumNamesSchemaFilter>();
35+
x.SchemaFilter<ExcludeNullableFromRequiredSchemaFilter>();
3536

3637
x.AddSecurityDefinition(
3738
AuthorizationScheme,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.OpenApi.Models;
2+
3+
using Swashbuckle.AspNetCore.SwaggerGen;
4+
5+
namespace Bss.Platform.Api.Documentation.SchemaFilters;
6+
7+
/// <summary>
8+
/// Exclude nullable property from required.
9+
/// Needed for supporting correct front-end code generation by GenGen https://github.com/Luxoft/gengen
10+
/// Additional to https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2036
11+
/// </summary>
12+
public class ExcludeNullableFromRequiredSchemaFilter : ISchemaFilter
13+
{
14+
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
15+
{
16+
if (schema.Required.Count == 0)
17+
{
18+
return;
19+
}
20+
21+
var nullableProperties = context.Type.GetProperties()
22+
.Where(x => !x.IsNonNullableReferenceType())
23+
.Select(x => x.Name);
24+
25+
var requiredNullableProperty = schema.Required
26+
.Intersect(nullableProperties, StringComparer.OrdinalIgnoreCase)
27+
.ToArray();
28+
29+
foreach (var property in requiredNullableProperty)
30+
{
31+
schema.Required.Remove(property);
32+
}
33+
}
34+
}

src/__SolutionItems/CommonAssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
[assembly: AssemblyCompany("Luxoft")]
55
[assembly: AssemblyCopyright("Copyright © Luxoft 2024")]
66

7-
[assembly: AssemblyVersion("1.5.7.0")]
8-
[assembly: AssemblyFileVersion("1.5.7.0")]
9-
[assembly: AssemblyInformationalVersion("1.5.7.0")]
7+
[assembly: AssemblyVersion("1.5.8.0")]
8+
[assembly: AssemblyFileVersion("1.5.8.0")]
9+
[assembly: AssemblyInformationalVersion("1.5.8.0")]
1010

1111
#if DEBUG
1212
[assembly: AssemblyConfiguration("Debug")]

0 commit comments

Comments
 (0)