Skip to content

Commit

Permalink
gen: Encode ML PredictRequest to JSON once only
Browse files Browse the repository at this point in the history
The ML API expects users to prepare a JSON-encoded string as part of the
payload when building a GoogleCloudMlV1__PredictRequest. However, the
generator produces code which assumes a PredictRequest needs to be
encoded to JSON, even though the data field is in fact already encoded.
Furthermore, the generator produces code that drops the data portion of
the API response.

This commit adds special handling for requests to the ML predict
endpoint, in order to correctly marshal requests and unmarshal
responses. For a discussion of API requests and responses, see
https://cloud.google.com/ml-engine/docs/v1/predict-request

Fixes #241.

Change-Id: I6ce90cae80d319b64173dd285051b65b07ba1255
Reviewed-on: https://code-review.googlesource.com/c/34871
Reviewed-by: Jean de Klerk <deklerk@google.com>
  • Loading branch information
enocom committed Nov 1, 2018
1 parent 799fe93 commit 45a6069
Show file tree
Hide file tree
Showing 5 changed files with 9,874 additions and 11 deletions.
27 changes: 21 additions & 6 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1851,12 +1851,18 @@ func (meth *Method) generateCode() {
}
pn("var body io.Reader = nil")
if ba := args.bodyArg(); ba != nil && httpMethod != "GET" {
style := "WithoutDataWrapper"
if a.needsDataWrapper() {
style = "WithDataWrapper"
if meth.m.ID == "ml.projects.predict" {
// Skip JSONReader for APIs that require clients to pass in JSON already.
pn("body = strings.NewReader(c.%s.HttpBody.Data)", ba.goname)
} else {
style := "WithoutDataWrapper"
if a.needsDataWrapper() {
style = "WithDataWrapper"
}
pn("body, err := googleapi.%s.JSONReader(c.%s)", style, ba.goname)
pn("if err != nil { return nil, err }")
}
pn("body, err := googleapi.%s.JSONReader(c.%s)", style, ba.goname)
pn("if err != nil { return nil, err }")

pn(`reqHeaders.Set("Content-Type", "application/json")`)
}
pn(`c.urlParams_.Set("alt", alt)`)
Expand Down Expand Up @@ -1985,7 +1991,16 @@ func (meth *Method) generateCode() {
} else {
pn("target := &ret")
}
pn("if err := gensupport.DecodeResponse(target, res); err != nil { return nil, err }")

if meth.m.ID == "ml.projects.predict" {
pn("var b bytes.Buffer")
pn("if _, err := io.Copy(&b, res.Body); err != nil { return nil, err }")
pn("if err := res.Body.Close(); err != nil { return nil, err }")
pn("if err := json.NewDecoder(bytes.NewReader(b.Bytes())).Decode(target); err != nil { return nil, err }")
pn("ret.Data = b.String()")
} else {
pn("if err := gensupport.DecodeResponse(target, res); err != nil { return nil, err }")
}
pn("return ret, nil")
}

Expand Down
1 change: 1 addition & 0 deletions google-api-go-generator/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestAPIs(t *testing.T) {
"blogger-3",
"floats",
"getwithoutbody",
"json-body",
"mapofany",
"mapofarrayofobjects",
"mapofint64strings",
Expand Down
Loading

0 comments on commit 45a6069

Please sign in to comment.