@@ -21,16 +21,24 @@ public static IApplicationBuilder UseDefaultOpenApi(this WebApplication app)
2121 return app ;
2222 }
2323
24- app . MapOpenApi ( ) ;
24+ app . MapOpenApi ( ) . WithDocumentPerVersion ( ) ;
2525
2626 if ( app . Environment . IsDevelopment ( ) )
2727 {
28+ var descriptions = app . DescribeApiVersions ( ) ;
29+ var defaultDocument = descriptions . Count > 0 ? descriptions [ ^ 1 ] . GroupName : "v1" ;
30+
2831 app . MapScalarApiReference ( options =>
2932 {
3033 // Disable default fonts to avoid download unnecessary fonts
3134 options . DefaultFonts = false ;
35+
36+ foreach ( var description in descriptions )
37+ {
38+ options . AddDocument ( description . GroupName , description . GroupName , isDefault : description . GroupName == defaultDocument ) ;
39+ }
3240 } ) ;
33- app . MapGet ( "/" , ( ) => Results . Redirect ( "/scalar/v1 " ) ) . ExcludeFromDescription ( ) ;
41+ app . MapGet ( "/" , ( ) => Results . Redirect ( $ "/scalar/{ defaultDocument } ") ) . ExcludeFromDescription ( ) ;
3442 }
3543
3644 return app ;
@@ -57,19 +65,21 @@ public static IHostApplicationBuilder AddDefaultOpenApi(
5765 {
5866 // the default format will just be ApiVersion.ToString(); for example, 1.0.
5967 // this will format the version as "'v'major[.minor][-status]"
60- var versioned = apiVersioning . AddApiExplorer ( options => options . GroupNameFormat = "'v'VVV" ) ;
61- string [ ] versions = [ "v1" , "v2" ] ;
62- foreach ( var description in versions )
63- {
64- builder . Services . AddOpenApi ( description , options =>
68+ apiVersioning . AddApiExplorer ( options =>
69+ {
70+ options . GroupNameFormat = "'v'VVV" ;
71+ options . DefaultApiVersionParameterDescription = "The API version, in the format 'major.minor'." ;
72+ } )
73+ . AddOpenApi ( options =>
6574 {
66- options . ApplyApiVersionInfo ( openApi . GetRequiredValue ( "Document:Title" ) , openApi . GetRequiredValue ( "Document:Description" ) ) ;
67- options . ApplyAuthorizationChecks ( [ .. scopes . Keys ] ) ;
68- options . ApplySecuritySchemeDefinitions ( ) ;
69- options . ApplyOperationDeprecatedStatus ( ) ;
70- options . ApplyApiVersionDescription ( ) ;
75+ var document = options . Document ;
76+
77+ document . ApplyApiVersionInfo ( openApi . GetRequiredValue ( "Document:Title" ) , openApi . GetRequiredValue ( "Document:Description" ) ) ;
78+ document . ApplyAuthorizationChecks ( [ .. scopes . Keys ] ) ;
79+ document . ApplySecuritySchemeDefinitions ( ) ;
80+ document . ApplyOperationDeprecatedStatus ( ) ;
81+ document . ApplyApiVersionDescription ( ) ;
7182 } ) ;
72- }
7383 }
7484
7585 return builder ;
0 commit comments