Open ylbillyli opened 5 years ago
Thanks! I was looking at the NSwag extension code and it looks like AddOpenApiDocument passes in IServiceProvider through Action<AspNetCoreOpenApiDocumentGeneratorSettings, IServiceProvider>
, so we can just use it directly without having to build the service provider also.
/// <summary>Adds services required for Swagger 2.0 generation (change document settings to generate OpenAPI 3.0).</summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">Configure the document.</param>
public static IServiceCollection AddOpenApiDocument(this IServiceCollection serviceCollection,
Action<AspNetCoreOpenApiDocumentGeneratorSettings, IServiceProvider> configure = null)`
services.AddOpenApiDocument((document, serviceProvider) =>
{
var fluentValidationSchemaProcessor = serviceProvider.GetService<FluentValidationSchemaProcessor>();
document.SchemaProcessors.Add(fluentValidationSchemaProcessor);
});
I'll update the documentation.
Instead of building
ServiceProvider
on-the-fly just to resolveFluentValidationSchemaProcessor
, I resolveFluentValidationSchemaProcessor
duringConfigure(IApplicationBuilder app)
into aprivate static
variable (it is singleton anyway) and then use it in theSchemaProcessors.Add
call