don't use ReadAll when decoding JSON

An empty stream isn't valid JSON, so we shouldn't silently ignore it.
This commit is contained in:
Daniel Martí
2019-05-27 11:02:50 +01:00
parent 098d963881
commit 9bcdcc128f
11 changed files with 29 additions and 53 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
package jira package jira
import ( import (
"encoding/json"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata" "gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
) )
@@ -19,7 +21,7 @@ func GetAttachment(ua HttpClient, endpoint string, id string) (*jiradata.Attachm
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.Attachment{} results := &jiradata.Attachment{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
+1 -1
View File
@@ -31,7 +31,7 @@ func CreateComponent(ua HttpClient, endpoint string, cp ComponentProvider) (*jir
if resp.StatusCode == 201 { if resp.StatusCode == 201 {
results := &jiradata.Component{} results := &jiradata.Component{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
+1 -1
View File
@@ -53,7 +53,7 @@ func EpicSearch(ua HttpClient, endpoint string, epic string, sp SearchProvider)
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.SearchResults{} results := &jiradata.SearchResults{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
+2 -1
View File
@@ -1,6 +1,7 @@
package jira package jira
import ( import (
"encoding/json"
"net/http" "net/http"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata" "gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
@@ -8,7 +9,7 @@ import (
func responseError(resp *http.Response) error { func responseError(resp *http.Response) error {
results := &jiradata.ErrorCollection{} results := &jiradata.ErrorCollection{}
if err := readJSON(resp.Body, results); err != nil { if err := json.NewDecoder(resp.Body).Decode(results); err != nil {
results.Status = resp.StatusCode results.Status = resp.StatusCode
results.ErrorMessages = append(results.ErrorMessages, err.Error()) results.ErrorMessages = append(results.ErrorMessages, err.Error())
} }
+3 -1
View File
@@ -1,6 +1,8 @@
package jira package jira
import ( import (
"encoding/json"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata" "gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
) )
@@ -18,7 +20,7 @@ func GetFields(ua HttpClient, endpoint string) ([]jiradata.Field, error) {
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := []jiradata.Field{} results := []jiradata.Field{}
return results, readJSON(resp.Body, &results) return results, json.NewDecoder(resp.Body).Decode(&results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
+11 -11
View File
@@ -69,7 +69,7 @@ func GetIssue(ua HttpClient, endpoint string, issue string, iqg IssueQueryProvid
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.Issue{} results := &jiradata.Issue{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -95,7 +95,7 @@ func GetIssueWorklog(ua HttpClient, endpoint string, issue string) (*jiradata.Wo
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.WorklogWithPagination{} results := &jiradata.WorklogWithPagination{}
err := readJSON(resp.Body, results) err := json.NewDecoder(resp.Body).Decode(results)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -133,7 +133,7 @@ func AddIssueWorklog(ua HttpClient, endpoint string, issue string, wp WorklogPro
if resp.StatusCode == 201 { if resp.StatusCode == 201 {
results := &jiradata.Worklog{} results := &jiradata.Worklog{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -153,7 +153,7 @@ func GetIssueEditMeta(ua HttpClient, endpoint string, issue string) (*jiradata.E
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.EditMeta{} results := &jiradata.EditMeta{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -206,7 +206,7 @@ func CreateIssue(ua HttpClient, endpoint string, iup IssueUpdateProvider) (*jira
if resp.StatusCode == 201 { if resp.StatusCode == 201 {
results := &jiradata.IssueCreateResponse{} results := &jiradata.IssueCreateResponse{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -227,7 +227,7 @@ func GetIssueCreateMetaProject(ua HttpClient, endpoint string, projectKey string
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.CreateMeta{} results := &jiradata.CreateMeta{}
err = readJSON(resp.Body, results) err = json.NewDecoder(resp.Body).Decode(results)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -257,7 +257,7 @@ func GetIssueCreateMetaIssueType(ua HttpClient, endpoint string, projectKey, iss
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.CreateMeta{} results := &jiradata.CreateMeta{}
err = readJSON(resp.Body, results) err = json.NewDecoder(resp.Body).Decode(results)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -319,7 +319,7 @@ func GetIssueTransitions(ua HttpClient, endpoint string, issue string) (*jiradat
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.TransitionsMeta{} results := &jiradata.TransitionsMeta{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -367,7 +367,7 @@ func GetIssueLinkTypes(ua HttpClient, endpoint string) (*jiradata.IssueLinkTypes
}{ }{
IssueLinkTypes: jiradata.IssueLinkTypes{}, IssueLinkTypes: jiradata.IssueLinkTypes{},
} }
return &results.IssueLinkTypes, readJSON(resp.Body, &results) return &results.IssueLinkTypes, json.NewDecoder(resp.Body).Decode(&results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -501,7 +501,7 @@ func IssueAddComment(ua HttpClient, endpoint string, issue string, cp CommentPro
if resp.StatusCode == 201 { if resp.StatusCode == 201 {
results := jiradata.Comment{} results := jiradata.Comment{}
return &results, readJSON(resp.Body, &results) return &results, json.NewDecoder(resp.Body).Decode(&results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -579,7 +579,7 @@ func IssueAttachFile(ua HttpClient, endpoint string, issue, filename string, con
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := jiradata.ListOfAttachment{} results := jiradata.ListOfAttachment{}
return &results, readJSON(resp.Body, &results) return &results, json.NewDecoder(resp.Body).Decode(&results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
+2 -15
View File
@@ -3,7 +3,6 @@ package jiracmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/url" "net/url"
"strings" "strings"
@@ -75,21 +74,9 @@ func CmdRequest(o *oreo.Client, globals *jiracli.GlobalOptions, opts *RequestOpt
} }
defer resp.Body.Close() defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if len(content) == 0 {
if !globals.Quiet.Value {
fmt.Println("No content in response")
}
return nil
}
var data interface{} var data interface{}
err = json.Unmarshal(content, &data) if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
if err != nil { return fmt.Errorf("JSON Parse Error: %v", err)
return fmt.Errorf("JSON Parse Error: %s from %q", err, content)
} }
return opts.PrintTemplate(&data) return opts.PrintTemplate(&data)
} }
+3 -1
View File
@@ -1,6 +1,8 @@
package jira package jira
import ( import (
"encoding/json"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata" "gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
) )
@@ -19,7 +21,7 @@ func GetProjectComponents(ua HttpClient, endpoint string, project string) (*jira
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := jiradata.Components{} results := jiradata.Components{}
return &results, readJSON(resp.Body, &results) return &results, json.NewDecoder(resp.Body).Decode(&results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
+1 -1
View File
@@ -120,7 +120,7 @@ func Search(ua HttpClient, endpoint string, sp SearchProvider, opts ...SearchOpt
} }
page := &jiradata.SearchResults{} page := &jiradata.SearchResults{}
err = readJSON(resp.Body, page) err = json.NewDecoder(resp.Body).Decode(page)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+2 -2
View File
@@ -43,7 +43,7 @@ func NewSession(ua HttpClient, endpoint string, ap AuthProvider) (*jiradata.Auth
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.AuthSuccess{} results := &jiradata.AuthSuccess{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
@@ -63,7 +63,7 @@ func GetSession(ua HttpClient, endpoint string) (*jiradata.CurrentUser, error) {
if resp.StatusCode == 200 { if resp.StatusCode == 200 {
results := &jiradata.CurrentUser{} results := &jiradata.CurrentUser{}
return results, readJSON(resp.Body, results) return results, json.NewDecoder(resp.Body).Decode(results)
} }
return nil, responseError(resp) return nil, responseError(resp)
} }
-18
View File
@@ -1,29 +1,11 @@
package jira package jira
import ( import (
"encoding/json"
"fmt" "fmt"
"io"
"io/ioutil"
"net/url" "net/url"
"path" "path"
) )
func readJSON(input io.Reader, data interface{}) error {
content, err := ioutil.ReadAll(input)
if err != nil {
return err
}
if len(content) == 0 {
return nil
}
err = json.Unmarshal(content, data)
if err != nil {
return fmt.Errorf("JSON Parse Error: %s from %q", err, content)
}
return nil
}
func URLJoin(endpoint string, paths ...string) string { func URLJoin(endpoint string, paths ...string) string {
u, err := url.Parse(endpoint) u, err := url.Parse(endpoint)
if err != nil { if err != nil {