Skip to content

Commit

Permalink
WIP: use openapi v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Theel committed Jan 28, 2021
1 parent f0b8625 commit a962819
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 290 deletions.
73 changes: 35 additions & 38 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"time"

"github.com/Nerzal/swag"
"github.com/getkin/kin-openapi/openapi3"
"github.com/ghodss/yaml"
"github.com/go-openapi/spec"
)

// Gen presents a generate tool for swag.
Expand Down Expand Up @@ -164,7 +164,7 @@ func (g *Gen) formatSource(src []byte) []byte {
return code
}

func (g *Gen) writeGoDoc(packageName string, output io.Writer, swagger *spec.Swagger, config *Config) error {
func (g *Gen) writeGoDoc(packageName string, output io.Writer, swagger *openapi3.Swagger, config *Config) error {
generator, err := template.New("swagger_info").Funcs(template.FuncMap{
"printDoc": func(v string) string {
// Add schemes
Expand All @@ -177,35 +177,32 @@ func (g *Gen) writeGoDoc(packageName string, output io.Writer, swagger *spec.Swa
return err
}

swaggerSpec := &spec.Swagger{
VendorExtensible: swagger.VendorExtensible,
SwaggerProps: spec.SwaggerProps{
ID: swagger.ID,
Consumes: swagger.Consumes,
Produces: swagger.Produces,
Swagger: swagger.Swagger,
Info: &spec.Info{
VendorExtensible: swagger.Info.VendorExtensible,
InfoProps: spec.InfoProps{
Description: "{{.Description}}",
Title: "{{.Title}}",
TermsOfService: swagger.Info.TermsOfService,
Contact: swagger.Info.Contact,
License: swagger.Info.License,
Version: "{{.Version}}",
},
},
Host: "{{.Host}}",
BasePath: "{{.BasePath}}",
Paths: swagger.Paths,
Definitions: swagger.Definitions,
Parameters: swagger.Parameters,
Responses: swagger.Responses,
SecurityDefinitions: swagger.SecurityDefinitions,
Security: swagger.Security,
Tags: swagger.Tags,
ExternalDocs: swagger.ExternalDocs,
},
swaggerSpec := &openapi3.Swagger{
// VendorExtensible: swagger.VendorExtensible,
// SwaggerProps: openapi3.Swagger{
// ID: swagger.ID,
// Consumes: swagger.Consumes,
// Produces: swagger.Produces,
// Swagger: swagger.Swagger,
// Info: &openapi3.Info{
// VendorExtensible: swagger.Info.VendorExtensible,
// Description: "{{.Description}}",
// Title: "{{.Title}}",
// TermsOfService: swagger.Info.TermsOfService,
// Contact: swagger.Info.Contact,
// License: swagger.Info.License,
// Version: "{{.Version}}",
// },
// Host: "{{.Host}}",
// BasePath: "{{.BasePath}}",
// Paths: swagger.Paths,
// Definitions: swagger.Definitions,
// Parameters: swagger.Parameters,
// Responses: swagger.Responses,
// SecurityDefinitions: swagger.SecurityDefinitions,
Security: swagger.Security,
Tags: swagger.Tags,
ExternalDocs: swagger.ExternalDocs,
}

// crafted docs.json
Expand All @@ -230,13 +227,13 @@ func (g *Gen) writeGoDoc(packageName string, output io.Writer, swagger *spec.Swa
Timestamp: time.Now(),
GeneratedTime: config.GeneratedTime,
Doc: string(buf),
Host: swagger.Host,
PackageName: packageName,
BasePath: swagger.BasePath,
Schemes: swagger.Schemes,
Title: swagger.Info.Title,
Description: swagger.Info.Description,
Version: swagger.Info.Version,
// Host: swagger.Host,
PackageName: packageName,
// BasePath: swagger.BasePath,
// Schemes: swagger.Schemes,
Title: swagger.Info.Title,
Description: swagger.Info.Description,
Version: swagger.Info.Version,
})
if err != nil {
return err
Expand Down
9 changes: 3 additions & 6 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"testing"

"github.com/go-openapi/spec"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -282,11 +282,8 @@ func TestGen_writeGoDoc(t *testing.T) {
assert.Error(t, err)

packageTemplate = `{{.Data}}`
swagger := &spec.Swagger{
VendorExtensible: spec.VendorExtensible{},
SwaggerProps: spec.SwaggerProps{
Info: &spec.Info{},
},
swagger := &openapi3.Swagger{
Info: &openapi3.Info{},
}
err = gen.writeGoDoc("docs", &mockWriter{}, swagger, &Config{})
assert.Error(t, err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/Nerzal/swag
require (
github.com/KyleBanks/depth v1.2.1
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/getkin/kin-openapi v0.35.0
github.com/ghodss/yaml v1.0.0
github.com/go-openapi/spec v0.20.0
github.com/stretchr/testify v1.7.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/getkin/kin-openapi v0.35.0 h1:YoJusew7Es36hSpnEx3gRL2DGj/82T3j3Akbs9VIXDQ=
github.com/getkin/kin-openapi v0.35.0/go.mod h1:ZJSfy1PxJv2QQvH9EdBj3nupRTVvV42mkW6zKUlRBwk=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-openapi/jsonpointer v0.19.3 h1:gihV7YNZK1iK6Tgwwsxo2rJbD1GTbdm72325Bq8FI3w=
Expand Down Expand Up @@ -54,6 +56,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
Expand Down
Loading

0 comments on commit a962819

Please sign in to comment.