Skip to content

Commit

Permalink
protoc-gen-swagger: return error when encoding swagger file
Browse files Browse the repository at this point in the history
Previously, the error was being ignored
  • Loading branch information
Elliot Shepherd authored and johanbrandhorst committed Mar 8, 2019
1 parent a0b1534 commit 6c38131
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions protoc-gen-swagger/genswagger/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,21 @@ func mergeTargetFile(targets []*wrapper, mergeFileName string) *wrapper {
}

// convert swagger file obj to plugin.CodeGeneratorResponse_File
func encodeSwagger(file *wrapper) *plugin.CodeGeneratorResponse_File {
func encodeSwagger(file *wrapper) (*plugin.CodeGeneratorResponse_File, error) {
var formatted bytes.Buffer
enc := json.NewEncoder(&formatted)
enc.SetIndent("", " ")
enc.Encode(*file.swagger)
if err := enc.Encode(*file.swagger); err != nil {
return nil, err
}
name := file.fileName
ext := filepath.Ext(name)
base := strings.TrimSuffix(name, ext)
output := fmt.Sprintf("%s.swagger.json", base)
return &plugin.CodeGeneratorResponse_File{
Name: proto.String(output),
Content: proto.String(formatted.String()),
}
}, nil
}

func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGeneratorResponse_File, error) {
Expand Down Expand Up @@ -127,11 +129,19 @@ func (g *generator) Generate(targets []*descriptor.File) ([]*plugin.CodeGenerato

if g.reg.IsAllowMerge() {
targetSwagger := mergeTargetFile(swaggers, g.reg.GetMergeFileName())
files = append(files, encodeSwagger(targetSwagger))
f, err := encodeSwagger(targetSwagger)
if err != nil {
return nil, fmt.Errorf("failed to encode swagger for %s: %s", g.reg.GetMergeFileName(), err)
}
files = append(files, f)
glog.V(1).Infof("New swagger file will emit")
} else {
for _, file := range swaggers {
files = append(files, encodeSwagger(file))
f, err := encodeSwagger(file)
if err != nil {
return nil, fmt.Errorf("failed to encode swagger for %s: %s", file.fileName, err)
}
files = append(files, f)
glog.V(1).Infof("New swagger file will emit")
}
}
Expand Down

0 comments on commit 6c38131

Please sign in to comment.