问题
i am working on a webapi with plugins. I want to update the swagger documentation when a plugin is added. I can handle this when i don't use version. All methods are added to the swagger documentation.
but when the api has apiversion turned on, the generation of the new version swagger document failed. It returns a 404.
do i need to so anything for versioning to work and pick up the dynamic controller functions...
private string AddSwaggerVersionDocument(PluginMetadata metadata)
{
var version = metadata.Version.ToApiVersion();
if (SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions.SwaggerDocs.ContainsKey(version) == false)
{
SwaggerElements.GeneratorOptions.SwaggerDoc(version, new Info
{
Title = "webapi API",
Version = $"{version}",
Description = "Web API demo",
TermsOfService = "None",
Contact = new Contact
{
Name = "Frans van Ek",
Email = string.Empty,
Url = "https://fransvanek.nl"
},
License = new License
{
Name = "Use under LICX",
Url = "https://fransvanek.nl"
}
});
SwaggerElements.UIOptions.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"My API : {version}");
}
return version;
}
回答1:
I see now. You're using your own versioning mechanism and generating Swagger documents on-demand. Swashbuckle expects everything to be defined upfront. This is reasonable as the supported versions should be deterministic at the start of the application. If your application is completely dynamic, then your current solution will work, but can vary between invocations. If plug-ins are discovered and loaded ahead of time, then you can register an IConfigureOptions<SwaggerGenOptions>
that configures Swashbuckle with your plugin information. Something like:
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
readonly IApiDescriptionGroupCollectionProvider provider;
public ConfigureSwaggerOptions(
IApiDescriptionGroupCollectionProvider provider ) => this.provider = provider;
public void Configure( SwaggerGenOptions options )
{
// TODO: configure swashbuckler with plug-in information
}
}
And then register it in the service container with:
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
I hope that helps
来源:https://stackoverflow.com/questions/53762697/swagger-add-document-and-operation-for-a-generic-controller