1- using Microsoft . OpenApi ;
1+ using Asp . Versioning . ApiExplorer ;
2+ using Microsoft . OpenApi ;
23
34namespace 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}
0 commit comments