Call Swagger generator for plugins

寵の児 提交于 2019-12-13 04:15:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/53869774/call-swagger-generator-for-plugins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!