You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a specific problem? Or an existing feature?
If you extend existing solutions that come with a preconfigured Swashbuckle (for example Umbraco), you may want to add your own documents for your own API's, but not wish to inherit the existing configuration (eg. UseOneOfForPolymorphism or existing Filters).
Describe the solution you'd like
Currently, there can only ever be one SwaggerGenOptions, and it's split into a SwaggerGeneratorOptions and SchemaGeneratorOptions during startup using ConfigureSwaggerGeneratorOptions and ConfigureSchemaGeneratorOptions.
As such, the SwaggerGenerator and SchemaGenerator will use the same configuration for all documents. While you can make Filters opt-in by checking the documentName before proceeding with the logic, it's pretty much impossible to do something as simple as enabling/disabling UseOneOfForPolymorphism for one specific document without reinventing the wheel quite a bit.
My proposed solution is making the default ISwaggerProvider/IAsyncSwaggerProvider "document-aware". If the DI has a specific configuration for the string documentName, that one is used instead of the "global" one.
I made a proof of concept for this by creating a custom DocumentAwareSwaggerGenerator with this particular logic:
publicstatic IServiceCollection ConfigureSwaggerGen<T>(thisIServiceCollectionservices,stringname,OpenApiInfoinfo)whereT:class,IConfigureOptions<SwaggerGenOptions>{
services.Configure<SwaggerGenOptions>(options =>{ options.SwaggerDoc(name, info);});//NOTE: Transient so the constructor can inject all types of scope
services.AddKeyedTransient<IConfigureOptions<SwaggerGenOptions>,T>(name);//NOTE: Singleton so the factory is only run once
services.AddKeyedSingleton(name,(provider,_)=>{varswaggerGenOptions=new SwaggerGenOptions(); swaggerGenOptions.SwaggerGeneratorOptions.SwaggerDocs.Add( name, info); provider.GetRequiredKeyedService<IConfigureOptions<SwaggerGenOptions>>(name).Configure(swaggerGenOptions);//Snip: ConfigureSwaggerGeneratorOptions CreateFilter logic//Snip: ConfigureSchemaGeneratorOptions CreateFilter logicreturnswaggerGenOptions;});returnservices;}
All-in-all, this works! But I feel something like this should be included as part of Swashbuckle by default.
Additional context
No response
The text was updated successfully, but these errors were encountered:
Is your feature request related to a specific problem? Or an existing feature?
If you extend existing solutions that come with a preconfigured Swashbuckle (for example Umbraco), you may want to add your own documents for your own API's, but not wish to inherit the existing configuration (eg.
UseOneOfForPolymorphism
or existing Filters).Describe the solution you'd like
Currently, there can only ever be one
SwaggerGenOptions
, and it's split into aSwaggerGeneratorOptions
andSchemaGeneratorOptions
during startup usingConfigureSwaggerGeneratorOptions
andConfigureSchemaGeneratorOptions
.As such, the
SwaggerGenerator
andSchemaGenerator
will use the same configuration for all documents. While you can make Filters opt-in by checking the documentName before proceeding with the logic, it's pretty much impossible to do something as simple as enabling/disablingUseOneOfForPolymorphism
for one specific document without reinventing the wheel quite a bit.My proposed solution is making the default
ISwaggerProvider
/IAsyncSwaggerProvider
"document-aware". If the DI has a specific configuration for thestring documentName
, that one is used instead of the "global" one.I made a proof of concept for this by creating a custom
DocumentAwareSwaggerGenerator
with this particular logic:I then added an Extension method:
All-in-all, this works! But I feel something like this should be included as part of Swashbuckle by default.
Additional context
No response
The text was updated successfully, but these errors were encountered: