Skip to content

Commit

Permalink
fix: breaking change introduced by generation of 2 documents in the s…
Browse files Browse the repository at this point in the history
…ame package (#1127)
  • Loading branch information
ubogdan authored Feb 6, 2022
1 parent e969efb commit 24209aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 167 deletions.
67 changes: 14 additions & 53 deletions example/basic/docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag

package docs

import (
"bytes"
"encoding/json"
"strings"
"text/template"

"github.com/swaggo/swag"
)
import "github.com/swaggo/swag"

var doc = `{
const docTemplate_swagger = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"termsOfService": "http://swagger.io/terms/",
"contact": {
Expand Down Expand Up @@ -262,49 +254,18 @@ var doc = `{
}
}`

type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "1.0",
Host: "petstore.swagger.io",
BasePath: "/v2",
Schemes: []string{},
Title: "Swagger Example API",
Description: "This is a sample server Petstore server.",
}

type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}

return tpl.String()
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "petstore.swagger.io",
BasePath: "/v2",
Schemes: []string{},
Title: "Swagger Example API",
Description: "This is a sample server Petstore server.",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate_swagger,
}

func init() {
swag.Register(swag.Name, &s{})
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
69 changes: 12 additions & 57 deletions example/celler/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
// This file was generated by swaggo/swag
package docs

import (
"bytes"
"encoding/json"
"strings"
"text/template"
import "github.com/swaggo/swag"

"github.com/swaggo/swag"
)

var doc = `{
const docTemplate_swagger = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
Expand Down Expand Up @@ -1038,56 +1031,18 @@ var doc = `{
}
}`

type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "1.0",
Host: "localhost:8080",
BasePath: "/api/v1",
Schemes: []string{},
Title: "Swagger Example API",
Description: "This is a sample server celler server.",
}

type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
"escape": func(v interface{}) string {
// escape tabs
str := strings.Replace(v.(string), "\t", "\\t", -1)
// replace " with \", and if that results in \\", replace that with \\\"
str = strings.Replace(str, "\"", "\\\"", -1)
return strings.Replace(str, "\\\\\"", "\\\\\\\"", -1)
},
}).Parse(doc)
if err != nil {
return doc
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}

return tpl.String()
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "localhost:8080",
BasePath: "/api/v1",
Schemes: []string{},
Title: "Swagger Example API",
Description: "This is a sample server celler server.",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate_swagger,
}

func init() {
swag.Register("swagger", &s{})
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
67 changes: 14 additions & 53 deletions example/object-map-example/docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag

package docs

import (
"bytes"
"encoding/json"
"strings"
"text/template"

"github.com/swaggo/swag"
)
import "github.com/swaggo/swag"

var doc = `{
const docTemplate_swagger = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"termsOfService": "http://swagger.io/terms/",
"contact": {},
Expand Down Expand Up @@ -93,49 +85,18 @@ var doc = `{
}
}`

type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "1.0",
Host: "localhost:8080",
BasePath: "/api/v1",
Schemes: []string{},
Title: "Swagger Map Example API",
Description: "",
}

type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}

return tpl.String()
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "localhost:8080",
BasePath: "/api/v1",
Schemes: []string{},
Title: "Swagger Map Example API",
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate_swagger,
}

func init() {
swag.Register(swag.Name, &s{})
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
6 changes: 3 additions & 3 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ import "github.com/swaggo/swag"
const docTemplate_{{ .InstanceName }} = ` + "`{{ printDoc .Doc}}`" + `
// SwaggerInfo_{{ .InstanceName }} holds exported Swagger Info so clients can modify it
var SwaggerInfo_{{ .InstanceName }} = &swag.Spec{
// SwaggerInfo{{ if ne .InstanceName "swagger" }}_{{ .InstanceName }} {{- end }} holds exported Swagger Info so clients can modify it
var SwaggerInfo {{ if ne .InstanceName "swagger" }}_{{ .InstanceName }} {{- end }} = &swag.Spec{
Version: {{ printf "%q" .Version}},
Host: {{ printf "%q" .Host}},
BasePath: {{ printf "%q" .BasePath}},
Expand All @@ -422,6 +422,6 @@ var SwaggerInfo_{{ .InstanceName }} = &swag.Spec{
}
func init() {
swag.Register(SwaggerInfo_{{ .InstanceName }}.InstanceName(), SwaggerInfo_{{ .InstanceName }})
swag.Register(SwaggerInfo{{ if ne .InstanceName "swagger" }}_{{ .InstanceName }} {{- end }}.InstanceName(), SwaggerInfo{{ if ne .InstanceName "swagger" }}_{{ .InstanceName }} {{- end }})
}
`
2 changes: 1 addition & 1 deletion gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestGen_BuildInstanceName(t *testing.T) {
}
if !strings.Contains(
string(expectedCode),
"swag.Register(SwaggerInfo_swagger.InstanceName(), SwaggerInfo_swagger)",
"swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)",
) {
t.Fatal(errors.New("generated go code does not contain the correct default registration sequence"))
}
Expand Down

0 comments on commit 24209aa

Please sign in to comment.