调用Swagger生成器插件 [英] Call Swagger generator for plugins

查看:431
本文介绍了调用Swagger生成器插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  [HttpGet( api / plugins / swaggerdoc / {version})] 
public IActionResult GetSwaggerDoc(字符串版本)
{
SwaggerDocument gen = new SwaggerGenerator(apiDescriptionGroupCollectionProvider,schemaRegistryFactory,Swagger。 SwaggerElements.GeneratorOptions.SwaggerGeneratorOptions).GetSwagger(version);

返回Ok(gen);

}

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

 参数:[
{
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
}

我如何解决此问题?

解决方案

我找到了解决方法:



所需的所有元素都可以通过依赖项注入来检索,或者在启动时进行静态引用以保留它。像generatoroptions一样。
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);
返回Ok(jsonBuilder.ToString());
}

这将适用于Swashbuckle.AspNetCore 4.0.1版本...即将结束的版本Swashbuckle.AspNetCore(5.x)由于必须支持openapi 2和3,因此必须具有另一个实现。


i want to generate the swagger document for plugins.

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?

解决方案

I found a solution:

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());
            }

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆