Skip to content

Commit dabd398

Browse files
authored
Update Swagger support for .NET 10.0 and improve enum docs (#7377)
- Bump project version to 2.16. - Add Swashbuckle.AspNetCore packages for .NET 10.0 builds. - Refactor XEnumNamesSchemaFilter with conditional logic for .NET 10.0+ using new OpenAPI APIs. - Update ServiceCollectionExtensions to use new schema types and mappings for .NET 10.0+. - Preserve compatibility with earlier .NET versions.
1 parent 6a9b8a7 commit dabd398

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<!-- https://github.com/dotnet/sourcelink -->
1616
<IncludeSymbols>true</IncludeSymbols>
1717
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
18-
<Version>2.15</Version>
18+
<Version>2.16</Version>
1919

2020
<IsTrimmable>false</IsTrimmable>
2121
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>

src/server/Elsa.Server.Api/Elsa.Server.Api.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
<PackageReference Include="System.Linq.Async" Version="6.0.1">
2727
<ExcludeAssets>compile</ExcludeAssets>
2828
</PackageReference>
29+
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
30+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.1.7" />
31+
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="10.0.1" />
32+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.1.7" />
2933
</ItemGroup>
3034

3135
<ItemGroup>

src/server/Elsa.Server.Api/Extensions/SchemaFilters/XEnumNamesSchemaFilter.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
#if NET10_0_OR_GREATER
2+
using Microsoft.OpenApi;
3+
using System.Text.Json.Nodes;
4+
using System.Collections.Generic;
5+
#else
16
using Microsoft.OpenApi.Any;
27
using Microsoft.OpenApi.Models;
8+
#endif
39
using Swashbuckle.AspNetCore.SwaggerGen;
410
using System;
511
using System.Linq;
6-
using System.Reflection;
712

813
namespace Elsa.Server.Api.Extensions.SchemaFilters
914
{
@@ -13,6 +18,64 @@ public class XEnumNamesSchemaFilter : ISchemaFilter
1318
private const string openapiGenerator = "x-enum-varnames";
1419

1520

21+
#if NET10_0_OR_GREATER
22+
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
23+
{
24+
if (context.Type.IsEnum && schema is OpenApiSchema model)
25+
{
26+
AddGeneratorSupport(model, context, nswag);
27+
AddGeneratorSupport(model, context, openapiGenerator);
28+
AddSwaggerUiSupport(model, context);
29+
}
30+
}
31+
private void AddGeneratorSupport(OpenApiSchema model, SchemaFilterContext context, string generatorType)
32+
{
33+
if (model.Extensions?.ContainsKey(generatorType) != true)
34+
{
35+
var names = Enum.GetNames(context.Type).Select(x => (JsonNode)x).ToList();
36+
model.Extensions ??= new Dictionary<string, IOpenApiExtension>();
37+
model.Extensions.Add(generatorType, new EnumNamesOpenApiExtension(names));
38+
}
39+
}
40+
41+
private void AddSwaggerUiSupport(OpenApiSchema model, SchemaFilterContext context)
42+
{
43+
model.Enum?.Clear();
44+
model.Enum ??= [];
45+
Enum.GetNames(context.Type)
46+
.ToList()
47+
.ForEach(n =>
48+
{
49+
model.Enum.Add((JsonNode)n);
50+
model.Type = JsonSchemaType.String;
51+
model.Format = null;
52+
});
53+
}
54+
55+
internal class EnumNamesOpenApiExtension : IOpenApiExtension
56+
{
57+
public EnumNamesOpenApiExtension(List<JsonNode> enumDescriptions)
58+
{
59+
EnumDescriptions = enumDescriptions;
60+
}
61+
62+
public List<JsonNode> EnumDescriptions { get; }
63+
64+
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
65+
{
66+
if (writer is null)
67+
{
68+
throw new ArgumentNullException(nameof(writer));
69+
}
70+
writer.WriteStartArray();
71+
foreach (var description in EnumDescriptions)
72+
{
73+
writer.WriteAny(description);
74+
}
75+
writer.WriteEndArray();
76+
}
77+
}
78+
#else
1679
public void Apply(OpenApiSchema model, SchemaFilterContext context)
1780
{
1881
if (context.Type.IsEnum)
@@ -47,5 +110,7 @@ private void AddSwaggerUiSupport(OpenApiSchema model, SchemaFilterContext contex
47110
model.Format = null;
48111
});
49112
}
113+
#endif
114+
50115
}
51116
}

src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
using Elsa.Server.Api.Mapping;
88
using Elsa.Server.Api.Services;
99
using Elsa.Server.Api.Swagger.Examples;
10-
using Microsoft.AspNetCore.Mvc;
10+
#if NET10_0_OR_GREATER
11+
using Microsoft.OpenApi;
12+
#else
1113
using Microsoft.OpenApi.Any;
1214
using Microsoft.OpenApi.Models;
15+
#endif
1316
using Swashbuckle.AspNetCore.Filters;
1417
using Swashbuckle.AspNetCore.SwaggerGen;
1518
using System;
@@ -73,17 +76,29 @@ public static IServiceCollection AddElsaSwagger(this IServiceCollection services
7376
//c.ExampleFilters(); I don't know why, this line will make swagger error
7477
c.MapType<VersionOptions?>(() => new OpenApiSchema
7578
{
79+
#if NET10_0_OR_GREATER
80+
Type = JsonSchemaType.String | JsonSchemaType.Null,
81+
Example = "Latest",
82+
Description = "Any of Latest, Published, Draft, LatestOrPublished or a specific version number.",
83+
Default = "Latest"
84+
#else
7685
Type = PrimitiveType.String.ToString().ToLower(),
7786
Example = new OpenApiString("Latest"),
7887
Description = "Any of Latest, Published, Draft, LatestOrPublished or a specific version number.",
7988
Nullable = true,
8089
Default = new OpenApiString("Latest")
90+
#endif
8191
});
8292

8393
c.MapType<Type>(() => new OpenApiSchema
8494
{
95+
#if NET10_0_OR_GREATER
96+
Type = JsonSchemaType.String,
97+
Example = "System.String, mscorlib"
98+
#else
8599
Type = PrimitiveType.String.ToString().ToLower(),
86100
Example = new OpenApiString("System.String, mscorlib")
101+
#endif
87102
});
88103

89104
//Allow enums to be displayed

0 commit comments

Comments
 (0)