调用Swagger生成器插件

时间:2018-12-20 13:34:37

标签: c# .net-core swagger swashbuckle

我想为插件生成庞大的文档。

我将api的端点指向一个plugincontroller。在这种情况下,我有一种方法可以为特定版本创建文档。加载插件时,所有项目均已在swagger工具中注册。 (以某种方式,新文件不会被笨拙的中间件所占用,这就是为什么我需要这种解决方法。)

  [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);

        }

,但无法正确生成文档。它显示了有关属性的大量信息。例如

"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
           }

我该如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

所需的所有元素都可以通过依赖项注入来检索,或者在启动时进行静态引用以控制它。类似于generatoroptions。

  public PluginsController(IActionDescriptorChangeProvider changeProvider, IOptions<MvcJsonOptions> mvcJsonOptionsAccessor, ISchemaRegistryFactory schemaRegistryFactory, IApiDescriptionGroupCollectionProvider apiDescriptionGroupCollectionProvider)
    {
        this.changeProvider = changeProvider;
        this.schemaRegistryFactory = schemaRegistryFactory;
        this.apiDescriptionGroupCollectionProvider = apiDescriptionGroupCollectionProvider;
        this.mvcJsonOptionsAccessor = mvcJsonOptionsAccessor;
    }

该方法的实现将如下所示:

            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)版本必须具有另一个实现。