Skip to content

Commit 356f514

Browse files
authored
fix: dotnet 10 openapi (#261)
1 parent ac363cf commit 356f514

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

Common/OpenAPI/OpenApiExtensions.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.OpenApi;
1+
using Asp.Versioning.ApiExplorer;
2+
using Microsoft.OpenApi;
23

34
namespace OpenShock.Common.OpenAPI;
45

@@ -16,16 +17,23 @@ public static IServiceCollection AddOpenApiExt<TProgram>(this WebApplicationBuil
1617
{
1718
options.AddPolicy("OpenAPI", policy => policy.Expire(TimeSpan.FromMinutes(10)));
1819
});
19-
builder.Services.AddOpenApi(options =>
20-
{
21-
options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1;
22-
options.AddDocumentTransformer(DocumentDefaults.GetDocumentTransformer(version: "1"));
23-
});
24-
builder.Services.AddOpenApi("v2", options =>
20+
21+
using (var tempProvider = builder.Services.BuildServiceProvider())
2522
{
26-
options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1;
27-
options.AddDocumentTransformer(DocumentDefaults.GetDocumentTransformer(version: "2"));
28-
});
23+
var apiVersionProvider = tempProvider.GetRequiredService<IApiVersionDescriptionProvider>();
24+
25+
// Configure OpenAPI for each API version
26+
foreach (var description in apiVersionProvider.ApiVersionDescriptions)
27+
{
28+
builder.Services.AddOpenApi(description.GroupName, options =>
29+
{
30+
options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1;
31+
options.AddDocumentTransformer(DocumentDefaults.GetDocumentTransformer(
32+
version: description.ApiVersion.ToString()));
33+
});
34+
}
35+
}
36+
2937
builder.Services.AddOpenApi("oauth", options =>
3038
{
3139
options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1;
@@ -38,7 +46,7 @@ public static IServiceCollection AddOpenApiExt<TProgram>(this WebApplicationBuil
3846
options.ShouldInclude = apiDescription => apiDescription.GroupName is "admin";
3947
options.AddDocumentTransformer(DocumentDefaults.GetDocumentTransformer(version: "1"));
4048
});
41-
49+
4250
return builder.Services;
4351
}
4452
}

Common/OpenShockMiddlewareHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public static async Task<IApplicationBuilder> UseCommonOpenShockMiddleware(this
8484

8585
app.MapScalarApiReference("/scalar/viewer", options =>
8686
options
87-
.WithOpenApiRoutePattern("/swagger/{documentName}/swagger.json")
88-
.AddDocument("1", "Version 1")
89-
.AddDocument("2", "Version 2")
87+
.WithOpenApiRoutePattern("/openapi/{documentName}.json")
88+
.AddDocument("v1", "Version 1")
89+
.AddDocument("v2", "Version 2")
9090
);
9191

9292
app.MapControllers();

Common/OpenShockServiceHelper.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ public static IServiceCollection AddOpenShockServices(this IServiceCollection se
143143
{
144144
options.DefaultApiVersion = new ApiVersion(1, 0);
145145
options.AssumeDefaultVersionWhenUnspecified = true;
146-
});
147-
148-
apiVersioningBuilder.AddApiExplorer(setup =>
146+
options.ReportApiVersions = true;
147+
options.ApiVersionReader = new UrlSegmentApiVersionReader();
148+
})
149+
.AddMvc() // mvc required for ApiExplorer
150+
.AddApiExplorer(setup =>
149151
{
150-
setup.GroupNameFormat = "VVV";
152+
setup.GroupNameFormat = "'v'V";
151153
setup.SubstituteApiVersionInUrl = true;
152154
setup.DefaultApiVersion = new ApiVersion(1, 0);
153155
setup.AssumeDefaultVersionWhenUnspecified = true;

0 commit comments

Comments
 (0)