@@ -7,6 +7,7 @@ namespace Microsoft.Extensions.DependencyInjection;
77using Asp . Versioning ;
88using Asp . Versioning . ApiExplorer ;
99using Asp . Versioning . OpenApi ;
10+ using Asp . Versioning . OpenApi . Configuration ;
1011using Asp . Versioning . OpenApi . Reflection ;
1112using Asp . Versioning . OpenApi . Transformers ;
1213using Microsoft . AspNetCore . Http . Json ;
@@ -16,6 +17,7 @@ namespace Microsoft.Extensions.DependencyInjection;
1617using Microsoft . Extensions . Options ;
1718using System . Reflection ;
1819using static Microsoft . Extensions . DependencyInjection . ServiceDescriptor ;
20+ using EM = Microsoft . Extensions . DependencyInjection . ServiceProviderServiceExtensions ;
1921
2022/// <summary>
2123/// Provides OpenAPI specific extension methods for <see cref="IApiVersioningBuilder"/>.
@@ -34,60 +36,22 @@ public IApiVersioningBuilder AddOpenApi()
3436 ArgumentNullException . ThrowIfNull ( builder ) ;
3537
3638 AddOpenApiServices ( builder , GetAssemblies ( Assembly . GetCallingAssembly ( ) ) ) ;
37- builder . Services . TryAddKeyedTransient ( typeof ( ApiVersion ) , NoOptions ) ;
3839
3940 return builder ;
4041 }
4142
4243 /// <summary>
4344 /// Adds OpenAPI support for API versioning.
4445 /// </summary>
45- /// <param name="configureOptions">The function used to configure the target <see cref="OpenApiOptions">options</see>.</param>
46+ /// <param name="configureOptions">The function used to configure the
47+ /// <see cref="VersionedOpenApiOptions">versioned OpenAPI options</see>.</param>
4648 /// <returns>The original <see cref="IApiVersioningBuilder">builder</see>.</returns>
47- public IApiVersioningBuilder AddOpenApi ( Action < ApiVersionDescription , OpenApiOptions > configureOptions )
49+ public IApiVersioningBuilder AddOpenApi ( Action < VersionedOpenApiOptions > configureOptions )
4850 {
4951 ArgumentNullException . ThrowIfNull ( builder ) ;
5052
5153 AddOpenApiServices ( builder , GetAssemblies ( Assembly . GetCallingAssembly ( ) ) ) ;
52- builder . Services . TryAddKeyedTransient ( typeof ( ApiVersion ) , ( _ , _ ) => configureOptions ) ;
53-
54- return builder ;
55- }
56-
57- /// <summary>
58- /// Adds OpenAPI support for API versioning.
59- /// </summary>
60- /// <param name="descriptionOptions">The function used to configure the target
61- /// <see cref="OpenApiDocumentDescriptionOptions">title options</see>.</param>
62- /// <returns>The original <see cref="IApiVersioningBuilder">builder</see>.</returns>
63- public IApiVersioningBuilder AddOpenApi ( Action < OpenApiDocumentDescriptionOptions > descriptionOptions )
64- {
65- ArgumentNullException . ThrowIfNull ( builder ) ;
66-
67- AddOpenApiServices ( builder , GetAssemblies ( Assembly . GetCallingAssembly ( ) ) ) ;
68- builder . Services . Configure ( descriptionOptions ) ;
69- builder . Services . TryAddKeyedTransient ( typeof ( ApiVersion ) , NoOptions ) ;
70-
71- return builder ;
72- }
73-
74- /// <summary>
75- /// Adds OpenAPI support for API versioning.
76- /// </summary>
77- /// <param name="configureOptions">The function used to configure the target
78- /// <see cref="OpenApiOptions">OpenAPI options</see>.</param>
79- /// <param name="descriptionOptions">The function used to configure the target
80- /// <see cref="OpenApiDocumentDescriptionOptions">title options</see>.</param>
81- /// <returns>The original <see cref="IApiVersioningBuilder">builder</see>.</returns>
82- public IApiVersioningBuilder AddOpenApi (
83- Action < ApiVersionDescription , OpenApiOptions > configureOptions ,
84- Action < OpenApiDocumentDescriptionOptions > descriptionOptions )
85- {
86- ArgumentNullException . ThrowIfNull ( builder ) ;
87-
88- AddOpenApiServices ( builder , GetAssemblies ( Assembly . GetCallingAssembly ( ) ) ) ;
89- builder . Services . Configure ( descriptionOptions ) ;
90- builder . Services . TryAddKeyedTransient ( typeof ( ApiVersion ) , ( _ , _ ) => configureOptions ) ;
54+ builder . Services . Configure ( configureOptions ) ;
9155
9256 return builder ;
9357 }
@@ -102,9 +66,9 @@ private static void AddOpenApiServices( IApiVersioningBuilder builder, Assembly[
10266
10367 services . AddTransient ( NewRequestServices ) ;
10468 services . Add ( Singleton ( Type . IDocumentProvider , ResolveDocumentProvider ) ) ;
105- services . AddOptions < OpenApiDocumentDescriptionOptions > ( ) ;
106- services . Add ( Transient < ConfigureOpenApiOptions , ConfigureOpenApiOptions > ( ) ) ;
107- services . TryAddEnumerable ( Singleton < IConfigureOptions < OpenApiOptions > , ConfigureOpenApiOptions > ( static sp => sp . GetRequiredService < ConfigureOpenApiOptions > ( ) ) ) ;
69+ services . AddSingleton < VersionedOpenApiOptionsFactory > ( ) ;
70+ services . TryAddEnumerable ( Transient < IPostConfigureOptions < OpenApiOptions > , ConfigureOpenApiOptions > ( ) ) ;
71+ services . TryAdd ( Singleton < IOptionsFactory < VersionedOpenApiOptions > > ( EM . GetRequiredService < VersionedOpenApiOptionsFactory > ) ) ;
10872 builder . Services . AddSingleton ( sp => new XmlCommentsFile ( assemblies , sp . GetRequiredService < IHostEnvironment > ( ) ) ) ;
10973
11074 if ( GetJsonConfiguration ( ) is { } descriptor )
@@ -138,17 +102,12 @@ private static Assembly[] GetAssemblies( Assembly callingAssembly )
138102 return services . SingleOrDefault ( sd => sd . ServiceType == typeof ( IConfigureOptions < JsonOptions > ) ) ;
139103 }
140104
141- #pragma warning disable IDE0060
142-
143- private static Action < ApiVersionDescription , OpenApiOptions > NoOptions ( IServiceProvider provider , object key ) => static ( _ , _ ) => { } ;
144-
145105 private static object ResolveDocumentProvider ( IServiceProvider provider ) =>
146106 provider . GetRequiredService < KeyedServiceContainer > ( ) . GetRequiredService ( Type . IDocumentProvider ) ;
147107
148108 [ UnconditionalSuppressMessage ( "ILLink" , "IL3050" ) ]
149109 private static KeyedServiceContainer NewRequestServices ( IServiceProvider services )
150110 {
151- var configure = services . GetRequiredKeyedService < Action < ApiVersionDescription , OpenApiOptions > > ( typeof ( ApiVersion ) ) ;
152111 var provider = services . GetRequiredService < IApiVersionDescriptionProvider > ( ) ;
153112 var keyedServices = new KeyedServiceContainer ( services ) ;
154113 var names = new List < string > ( ) ;
0 commit comments