为插件调用 Swagger 生成器 [英] Call Swagger generator for plugins
问题描述
我想为插件生成 swagger 文档.
i want to generate the swagger document for plugins.
我将 api 的端点指向一个插件控制器.在这里,我有一种方法可以为特定版本创建文档.加载插件时,所有项目都已在 swagger 工具中注册.(不知何故,swagger 中间件不会接收到新文档,这就是我需要这种解决方法的原因.)
I point the endpoint for the api to a plugincontroller. In this i have a method to create the documentation for a particular version. While loading the plugin all items are already registered in the swagger tooling. (somehow the new documents don't get picked up by the swagger middleware that is why i need this workaround.)
[HttpGet("api/plugins/swaggerdoc/{version}")]
public IActionResult GetSwaggerDoc(string version)
{
SwaggerDocument gen = new SwaggerGenerator(apiDescriptionGroupCollectionProvider, schemaRegistryFactory, Swagger.SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions).GetSwagger(version);
return Ok(gen);
}
但无法正确生成文档.它显示了有关属性的大量信息.例如
but it fails to generate the document properly. It shows to much information about the properties. e.g.
"parameters":[
{
"name":"api-version",
"in":"query",
"description":null,
"required":false,
"type":"string",
"format":null,
"items":null,
"collectionFormat":null,
"default":null,
"maximum":null,
"exclusiveMaximum":null,
"minimum":null,
"exclusiveMinimum":null,
"maxLength":null,
"minLength":null,
"pattern":null,
"maxItems":null,
"minItems":null,
"uniqueItems":null,
"enum":null,
"multipleOf":null
}
我该如何解决这个问题?
how can i resolve this issue?
推荐答案
我找到了解决方案:
所有需要的元素都可以通过依赖注入来获取,或者在启动时做一个静态引用来控制它.像生成器选项一样.
All elements needed can be retrieved through dependency injection, or make a static reference in the startup to get a hold on it. Like generatoroptions.
public PluginsController(IActionDescriptorChangeProvider changeProvider, IOptions<MvcJsonOptions> mvcJsonOptionsAccessor, ISchemaRegistryFactory schemaRegistryFactory, IApiDescriptionGroupCollectionProvider apiDescriptionGroupCollectionProvider)
{
this.changeProvider = changeProvider;
this.schemaRegistryFactory = schemaRegistryFactory;
this.apiDescriptionGroupCollectionProvider = apiDescriptionGroupCollectionProvider;
this.mvcJsonOptionsAccessor = mvcJsonOptionsAccessor;
}
该方法的实现将是这样的:
the implementation of the method will be something like this:
SwaggerDocument gen = new SwaggerGenerator(apiDescriptionGroupCollectionProvider, schemaRegistryFactory, Swagger.SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions).GetSwagger(version);
var jsonBuilder = new StringBuilder();
var _swaggerSerializer = SwaggerSerializerFactory.Create(mvcJsonOptionsAccessor);
using (var writer = new StringWriter(jsonBuilder))
{
_swaggerSerializer.Serialize(writer, gen);
return Ok(jsonBuilder.ToString());
}
这将适用于 Swashbuckle.AspNetCore 的 4.0.1 版本...由于 openapi 2 和 3 的支持,Swashbuckle.AspNetCore (5.x) 的 upcomping 版本必须有另一个实现.
This will work for the 4.0.1 version of Swashbuckle.AspNetCore... The upcomping version of Swashbuckle.AspNetCore (5.x) will have to have another implementation because of the support of openapi 2 and 3.
这篇关于为插件调用 Swagger 生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!