从gin项目生成OpenAPI规范。

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

Generate OpenAPI spec from gin project

问题

有没有一种方法可以从gin项目生成OpenAPI规范文件?这个问题真的很难搜索,到目前为止,我只找到了关于如何进行反向操作的内容。

英文:

Is there a way to generate OpenAPI spec files from a gin project? This issue is really hard to search for, all I could find so far are on doing there reverse.

答案1

得分: 2

gin-swagger 是从你的代码中提取注释。

运行 swag init 会生成:

  • docs.go:用于提供 SwaggerUI 页面
  • swagger.json:以 json 文件格式的 Swagger 规范
  • swagger.yaml:以 yaml 文件格式的 Swagger 规范
英文:

gin-swagger is extracting comments from your code.

Running swag init generates :

  • docs.go: To serve SwaggerUI pages
  • swagger.json: The Swagger Specification in json file format.
  • swagger.yaml: The Swagger Specification in yaml file format.

答案2

得分: 1

你是指API用户的API文档吗?

我看到你在问题中标记了swagger

你有两个选择:

  1. go-swagger

这个包包含了Swagger 2.0(也称为OpenAPI 2.0)的golang实现:它知道如何序列化和反序列化Swagger规范。

  1. swag

Swag将Go注释转换为Swagger文档2.0。我们为流行的Go Web框架创建了各种插件。这使您可以快速与现有的Go项目集成(使用Swagger UI)。

它们之间有什么区别?

这是来自go-swagger的答案:

目前的主要区别是这个实际上是可用的...

swagger-codegen项目(像swag一样)只生成一个可用的go客户端,即使在那里,它也只支持扁平模型。此外,由swagger-codegen生成的go服务器大多是一个存根。

有关这个答案的详细信息,请参阅go-swagger中的How is this different from go generator in swagger-codegen?

英文:

You mean API docs for API user?

I saw you tag the question with swagger.

You have two chooses:

  1. go-swagger

> This package contains a golang implementation of Swagger 2.0 (aka OpenAPI 2.0): it knows how to serialize and deserialize swagger specifications.

  1. swag

> Swag converts Go annotations to Swagger Documentation 2.0. We've created a variety of plugins for popular Go web frameworks. This allows you to quickly integrate with an existing Go project (using Swagger UI).

What difference between them?

Here is the answer from go-swagger:

> The main difference at this moment is that this one actually works...
>
> The swagger-codegen project (like swag) only generates a workable go client and even there it will only support flat models. Further, the go server generated by swagger-codegen is mostly a stub.

Detail of this answer in How is this different from go generator in swagger-codegen?
from go-swagger.

huangapple
  • 本文由 发表于 2022年5月23日 20:46:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/72348748.html
匿名

发表评论

匿名网友

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

确定