Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate code that keeps swagger spec as a string instead of a []byte #856

Merged
merged 2 commits into from Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 30 additions & 30 deletions generator/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion generator/support.go
Expand Up @@ -15,6 +15,7 @@
package generator

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -655,9 +656,25 @@ func (a *appGenerator) makeCodegenApp() (GenApp, error) {
Operations: genOps,
OperationGroups: opGroups,
Principal: prin,
SwaggerJSON: fmt.Sprintf("%#v", jsonb),
SwaggerJSON: generateReadableSpec(jsonb),
ExcludeSpec: a.GenOpts != nil && a.GenOpts.ExcludeSpec,
WithContext: a.GenOpts != nil && a.GenOpts.WithContext,
GenOpts: a.GenOpts,
}, nil
}

// generateReadableSpec makes swagger json spec as a string instead of bytes
// the only character that needs to be escaped is '`' symbol, since it cannot be escaped in the GO string
// that is quoted as `string data`. The function doesn't care about the beginning or the ending of the
// string it escapes since all data that needs to be escaped is always in the middle of the swagger spec.
func generateReadableSpec(spec []byte) string {
buf := &bytes.Buffer{}
for _, b := range string(spec) {
if b == '`' {
buf.WriteString("`+\"`\"+`")
} else {
buf.WriteRune(b)
}
}
return buf.String()
}
2 changes: 1 addition & 1 deletion generator/templates/swagger_json_embed.gotmpl
Expand Up @@ -16,5 +16,5 @@ import (
var SwaggerJSON json.RawMessage

func init() {
SwaggerJSON = json.RawMessage({{ .SwaggerJSON }})
SwaggerJSON = json.RawMessage([]byte(`{{ .SwaggerJSON }}`))
}