How to generate a Swagger file from an AspNet Core WebAPI DLL without having Swagger packages in the project?

huangapple go评论52阅读模式
英文:

How to generate a Swagger file from an AspNet Core WebAPI DLL without having Swagger packages in the project?

问题

使用Swagger CLI工具,我可以使用以下命令生成Swagger文件:

swagger tofile --output swagger.json MyApi.dll v1

为了使这个命令生效,API DLL必须包含Swagger包并进行如下配置:

builder.Services.AddSwaggerGen();

是否可能从不包含Swagger包和配置的API DLL中生成Swagger文件,使用Swagger CLI或其他工具?

这样做的主要原因是我有很多API,我不想修改它们以添加Swagger包和配置,只是为了生成swagger.json文件。此外,我想在自动化构建期间执行此操作,所以我选择了命令行方法。

英文:

Using the Swagger CLI tool I can generate the swagger file with this command:

swagger tofile --output swagger.json MyApi.dll v1

For this command to work it is necessary that the API DLL contains the Swagger packages and configured like this:

builder.Services.AddSwaggerGen();

Is it possible to generate the Swagger file from an API DLL that doesn't contains the Swagger packages and configuration using Swagger CLI or any other tool?

The main reason for this is that I have a lot of APIs and I don't want to modify the APIs adding Swagger packages and configurations just to generate the swagger.json file. Also, I would like to do this during automated build, so I decided on the command line approach.

答案1

得分: 1

I'm not aware of any tools that can generate OpenAPI definitions by DLL inspection. All known OpenAPI-from-code generators (such as Swashbuckle or Swagger-Net) are libraries that need to be included in the app.

Without modifying the code, your best option is probably to record traffic to your API and generate an OpenAPI file from that. There are a few tools that can do that. Check out the examples of using Optic and Akita to get an idea of how this works.

Or if you have an existing Postman collection for your API, there are Postman-to-OpenAPI converters such as APIMatic Transformer (note: requires an account).

英文:

I'm not aware of any tools that can generate OpenAPI definitions by DLL inspection. All known OpenAPI-from-code generators (such as Swashbuckle or Swagger-Net) are libraries that need to be included in the app.

Without modifying the code, your best option is probably to record traffic to your API and generate an OpenAPI file from that. There are a few tools that can do that. Check out the examples of using Optic and Akita to get an idea of how this works.

Or if you have an existing Postman collection for your API, there are Postman-to-OpenAPI converters such as APIMatic Transformer (note: requires an account).

huangapple
  • 本文由 发表于 2023年6月15日 10:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定