Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Bss.Platform.Api.Documentation/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public static IServiceCollection AddPlatformApiDocumentation(
this IServiceCollection services,
IWebHostEnvironment hostEnvironment,
string title = "API",
Action<SwaggerGenOptions>? setupAction = null)
Action<SwaggerGenOptions>? setupAction = null,
bool enableExperimentalRequiredFeature = false)
{
if (hostEnvironment.IsProduction())
{
Expand All @@ -32,6 +33,13 @@ public static IServiceCollection AddPlatformApiDocumentation(
{
x.SwaggerDoc("api", new OpenApiInfo { Title = title });
x.SchemaFilter<XEnumNamesSchemaFilter>();

if (enableExperimentalRequiredFeature)
{
x.SupportNonNullableReferenceTypes();
x.NonNullableReferenceTypesAsRequired();
x.SchemaFilter<ExcludeNullableFromRequiredSchemaFilter>();
}

x.AddSecurityDefinition(
AuthorizationScheme,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace Bss.Platform.Api.Documentation.SchemaFilters;

/// <summary>
/// Exclude nullable property from required.
/// Needed for supporting correct front-end code generation by GenGen https://github.com/Luxoft/gengen
/// Additional to https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2036
/// </summary>
public class ExcludeNullableFromRequiredSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (schema.Required.Count == 0)
{
return;
}

var nullableProperties = context.Type.GetProperties()
.Where(x => !x.IsNonNullableReferenceType())
.Select(x => x.Name);

var requiredNullableProperty = schema.Required
.Intersect(nullableProperties, StringComparer.OrdinalIgnoreCase)
.ToArray();

foreach (var property in requiredNullableProperty)
{
schema.Required.Remove(property);
}
}
}
6 changes: 3 additions & 3 deletions src/__SolutionItems/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
[assembly: AssemblyCompany("Luxoft")]
[assembly: AssemblyCopyright("Copyright © Luxoft 2024")]

[assembly: AssemblyVersion("1.5.7.0")]
[assembly: AssemblyFileVersion("1.5.7.0")]
[assembly: AssemblyInformationalVersion("1.5.7.0")]
[assembly: AssemblyVersion("1.5.8.0")]
[assembly: AssemblyFileVersion("1.5.8.0")]
[assembly: AssemblyInformationalVersion("1.5.8.0")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
Expand Down