Go 语言编程 — go-swagger OpenAPI 工具



go-swagger 是一个 Golang 的第三方库,是一个 Swagger 2.0(aka OpenAPI 2.0)的 Golang 实现。

  • Github:https://github.com/go-swagger/go-swagger

go-swagger 内含了许多包,其中关键得有:

  • go-openapi:OpenAPI 的 Golang 实现。
  • govalidator:可以对 Golang 的字符串、结构体以及集合进行校验和检查的工具包。

go-swagger 还内含了许多 Swagger 工具库,安装 swagger 指令:

go get -u github.com/go-swagger/go-swagger/cmd/swagger

swagger 指令支持:

  • Serve specification UI,为 OpenAPI Specification 提供一个具有 UI 界面的服务器。
  • Validate a specification,验证 OpenAPI Specification 文件进行检查和校验。
  • Generate an API server,通过 OpenAPI Specification 生成服务端。
  • Generate an API client,通过 OpenAPI Specification 生成客户端。
  • Generate a spec from source,通过源码生成 OpenAPI Specification 文档。
  • Generate a data model,生成数据结构。
  • Transform specs,转换规范。
  • Compare specs,比较规范。

Generate a spec from source

swagger generate 指令会找到 main.go 文件,然后遍历所有源码文件,解析源码中与 Swagger 相关的注释,然后自动生成 swagger.json 文件,例如:

// Package classification User API.
// The purpose of this service is to provide an application
// that is using plain go code to define an API
//      Host: localhost
//      Version: 0.0.1
// swagger:meta
package service


swagger generate spec -o ./swagger.json

当 swagger.json 就绪后,可以直接运行 swagger serve 指令启动 UI 程序:

swagger serve -F=swagger swagger.json

Generate an API server

有了 swagger.json 或 swagger.yaml 之后就可以生成 API server 了。

mkdir myapi
cd myapi
swagger generate server -f ~/myapi.yaml -A myapi
CGO_ENABLED=1 go build -o dist/myapi -a -gcflags "-N -l" ./cmd/myapi-server;



已标记关键词 清除标记
<div class="post-text" itemprop="text"> <p>Is there a cleaner way to implement OpenAPI 3.0 constructs around oneOf, anyOf, allOf in Golang. I feel that it would be wise to have a flat structure with a discriminator field. (example below)</p> <pre><code>package main import ( "encoding/json" "fmt" ) type OneOfCertification struct { AssetType string `json:"assetType"` CertDetails *json.RawMessage `json:"certDetails"` } type FlatCertification struct { AssetType string `json:"assetType"` DiamondCert *Diamond `json:"diamondCert,omitempty"` GemCert *Gem `json:"gemCert,omitempty"` } type Diamond struct { DiamondAttr string `json:"diamondAttr"` } type Gem struct { GemAttr string `json:"gemAttr"` } func oneOfUnmarshalMarshal(data []byte) ([]byte, error) { var cert OneOfCertification json.Unmarshal(data, &cert) switch cert.AssetType { case "DIA": var dia Diamond json.Unmarshal(*cert.CertDetails, &dia) fmt.Println("(oneOf) Diamond Cert Details:", dia) // Now lets try to Marshal concrete back to raw message b, _ := json.Marshal(&dia) certDtls := json.RawMessage(b) cert.CertDetails = &certDtls case "GEM": var gem Gem json.Unmarshal(*cert.CertDetails, &gem) fmt.Println("(oneOf) Gem Cert Details:", gem) // Now lets try to Marshal concrete back to raw message b, _ := json.Marshal(&gem) certDtls := json.RawMessage(b) cert.CertDetails = &certDtls } // Finally Marshal cert and send it back return json.Marshal(&cert) } func flatUnmarshal(data []byte) ([]byte, error) { var cert FlatCertification json.Unmarshal(data, &cert) switch cert.AssetType { case "DIA": fmt.Println("(flat) Gem Cert Details:", *cert.DiamondCert) case "GEM": fmt.Println("(flat) Gem Cert Details:", *cert.GemCert) } return json.Marshal(&cert) } func main() { oneOfDiaCert := `{"assetType":"DIA","certDetails":{"diamondAttr":"certified diamond"}}` oneOfGemCert := `{"assetType":"GEM","certDetails":{"gemAttr":"certified gem"}}` db, _ := oneOfUnmarshalMarshal([]byte(oneOfDiaCert)) gb, _ := oneOfUnmarshalMarshal([]byte(oneOfGemCert)) fmt.Println("(oneOf) marshalled diamond certification", string(db)) fmt.Println("(oneOf) marshalled gem certification", string(gb)) flatDiaCert := `{"assetType":"DIA","diamondCert":{"diamondAttr":"certified diamond"}}` flatGemCert := `{"assetType":"GEM","gemCert":{"gemAttr":"certified gem"}}` db, _ = flatUnmarshal([]byte(flatDiaCert)) gb, _ = flatUnmarshal([]byte(flatGemCert)) fmt.Println("(flat) marshalled diamond certification", string(db)) fmt.Println("(flat) marshalled gem certification", string(gb)) } </code></pre> <p>The sheer amount of code written to marshal and unmarshal with json.RawMessage or any interface {} makes it look ugly. Any thoughts on implementing it in a smarter fashion in Golang ??</p> </div>
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页
实付 49.90元
钱包余额 0