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

Fix query-param handling on the client side #655

Merged
merged 1 commit into from
Oct 9, 2019
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
88 changes: 52 additions & 36 deletions codegen/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,27 @@ func (ms *MethodSpec) hasQueryParams(field *compile.FieldSpec, defaultIsQuery bo
return httpRefAnnotation == "" && defaultIsQuery
}

// getContainedQueryParams - finds all query params of interest in this field
// In the case of structs, it recursively drills down
func (ms *MethodSpec) getContainedQueryParams(
field *compile.FieldSpec, defaultIsQuery bool, defaultPrefix string) []string {
rval := []string{}
myDefaultParam := defaultPrefix + strings.ToLower(field.Name)
annotation := field.Annotations[ms.annotations.HTTPRef]
if strings.HasPrefix(annotation, queryAnnotationPrefix) {
rval = append(rval, strings.TrimPrefix(annotation, queryAnnotationPrefix))
} else if defaultIsQuery && annotation == "" {
rval = append(rval, myDefaultParam)
}
// If it is a struct, look to see if any of the fields are query params
if container, ok := compile.RootTypeSpec(field.Type).(*compile.StructSpec); ok {
for _, subField := range container.Fields {
rval = append(rval, ms.getContainedQueryParams(subField, defaultIsQuery, myDefaultParam+".")...)
}
}
return rval
}

func (ms *MethodSpec) setWriteQueryParamStatements(
funcSpec *compile.FunctionSpec, packageHelper *PackageHelper, hasNoBody bool,
) error {
Expand Down Expand Up @@ -1181,7 +1202,7 @@ func (ms *MethodSpec) setWriteQueryParamStatements(
return false
}

longQueryName := ms.getLongQueryName(field, thriftPrefix)
longQueryName, shortQueryParam := ms.getQueryParamInfo(field, thriftPrefix)
identifierName := CamelCase(longQueryName) + "Query"
_, isList := realType.(*compile.ListSpec)
_, isSet := realType.(*compile.SetSpec)
Expand All @@ -1195,34 +1216,34 @@ func (ms *MethodSpec) setWriteQueryParamStatements(
if isList {
encodeExpr := getQueryEncodeExpression(field.Type, "value")
statements.appendf("for _, value := range %s {", "r"+longFieldName)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", longQueryName, encodeExpr)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", shortQueryParam, encodeExpr)
statements.append("}")
} else if isSet {
encodeExpr := getQueryEncodeExpression(field.Type, "value")
statements.appendf("for value := range %s {", "r"+longFieldName)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", longQueryName, encodeExpr)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", shortQueryParam, encodeExpr)
statements.append("}")
} else {
encodeExpr := getQueryEncodeExpression(field.Type, "r"+longFieldName)
statements.appendf("%s := %s", identifierName, encodeExpr)
statements.appendf("queryValues.Set(\"%s\", %s)", longQueryName, identifierName)
statements.appendf("queryValues.Set(\"%s\", %s)", shortQueryParam, identifierName)
}
} else {
statements.appendf("if r%s != nil {", longFieldName)
if isList {
encodeExpr := getQueryEncodeExpression(field.Type, "value")
statements.appendf("for _, value := range %s {", "r"+longFieldName)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", longQueryName, encodeExpr)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", shortQueryParam, encodeExpr)
statements.append("}")
} else if isSet {
encodeExpr := getQueryEncodeExpression(field.Type, "value")
statements.appendf("for value := range %s {", "r"+longFieldName)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", longQueryName, encodeExpr)
statements.appendf("\tqueryValues.Add(\"%s\", %s)", shortQueryParam, encodeExpr)
statements.append("}")
} else {
encodeExpr := getQueryEncodeExpression(field.Type, "*r"+longFieldName)
statements.appendf("\t%s := %s", identifierName, encodeExpr)
statements.appendf("\tqueryValues.Set(\"%s\", %s)", longQueryName, identifierName)
statements.appendf("\tqueryValues.Set(\"%s\", %s)", shortQueryParam, identifierName)
}
statements.append("}")
}
Expand Down Expand Up @@ -1256,7 +1277,7 @@ func (ms *MethodSpec) setParseQueryParamStatements(
) bool {
realType := compile.RootTypeSpec(field.Type)
longFieldName := goPrefix + "." + PascalCase(field.Name)
longQueryName := ms.getLongQueryName(field, thriftPrefix)
longQueryName, shortQueryParam := ms.getQueryParamInfo(field, thriftPrefix)

// Skip if there are no query params in the field or its components
if !ms.hasQueryParams(field, hasNoBody) {
Expand Down Expand Up @@ -1310,8 +1331,6 @@ func (ms *MethodSpec) setParseQueryParamStatements(
}
}
case *compile.StructSpec:
// If the type is a struct then we cannot really do anything

typeName, err := GoType(packageHelper, realType)
if err != nil {
finalError = err
Expand All @@ -1320,18 +1339,19 @@ func (ms *MethodSpec) setParseQueryParamStatements(

if !field.Required {
stack = append(stack, longFieldName)
applicableQueryParams := ms.getContainedQueryParams(field, hasNoBody, "")

statements.appendf(
"if req.HasQueryPrefix(%q) || requestBody%s != nil {",
longQueryName,
longFieldName,
)
statements.append("var _queryNeeded bool")
statements.appendf("for _, _pfx := range %#v {", applicableQueryParams)
statements.append("\tif _queryNeeded = req.HasQueryPrefix(_pfx); _queryNeeded {")
statements.append("\t\tbreak")
statements.append("\t}")
statements.append("}")
statements.append("if _queryNeeded {")
}

statements.appendf("if requestBody%s == nil {", longFieldName)
statements.appendf("\trequestBody%s = &%s{}",
longFieldName, typeName,
)
statements.appendf("\trequestBody%s = &%s{}", longFieldName, typeName)
statements.append("}")

return false
Expand All @@ -1341,22 +1361,22 @@ func (ms *MethodSpec) setParseQueryParamStatements(
okIdentifierName := CamelCase(longQueryName) + "Ok"
if field.Required {
statements.appendf("%s := req.CheckQueryValue(%q)",
okIdentifierName, longQueryName,
okIdentifierName, shortQueryParam,
)
statements.appendf("if !%s {", okIdentifierName)
statements.append("\treturn")
statements.append("}")
} else {
statements.appendf("%s := req.HasQueryValue(%q)",
okIdentifierName, longQueryName,
okIdentifierName, shortQueryParam,
)
statements.appendf("if %s {", okIdentifierName)
}

queryMethodName := getQueryMethodForType(realType)

statements.appendf("%s, ok := req.%s(%q)",
identifierName, queryMethodName, longQueryName,
identifierName, queryMethodName, shortQueryParam,
)

statements.append("if !ok {")
Expand Down Expand Up @@ -1430,32 +1450,28 @@ func (ms *MethodSpec) setParseQueryParamStatements(
return nil
}

func (ms *MethodSpec) getLongQueryName(field *compile.FieldSpec, thriftPrefix string) string {
var longQueryName string
// getQueryParamInfo -- returns the fully-qualified query name and the query param
// The query param is what is specified in the annotation if present, otherwise it is the same as the long query name
func (ms *MethodSpec) getQueryParamInfo(field *compile.FieldSpec, thriftPrefix string) (string, string) {
var longQueryName, queryParam string

queryName := field.Name
queryAnnotation := field.Annotations[ms.annotations.HTTPRef]
if strings.HasPrefix(queryAnnotation, queryAnnotationPrefix) {
queryName = strings.TrimPrefix(queryAnnotation, queryAnnotationPrefix)
queryParam = queryName
}

if thriftPrefix == "" {
longQueryName = queryName
} else if thriftPrefix[0] == '.' {
longQueryName = thriftPrefix[1:] + "." + queryName
} else {
longQueryName = thriftPrefix + "." + queryName
longQueryName = strings.TrimPrefix(thriftPrefix+"."+queryName, ".")
// default the short query param to the fully qualified long path
if queryParam == "" {
queryParam = longQueryName
}

return longQueryName
return longQueryName, queryParam
}

func (ms *MethodSpec) isRequestBoxed(f *compile.FunctionSpec) bool {
boxed, ok := f.Annotations[ms.annotations.HTTPReqDefBoxed]
if ok && boxed == "true" {
return true
}
return false
return ok && boxed == "true"
}

func headers(annotation string) []string {
Expand Down
2 changes: 1 addition & 1 deletion examples/example-gateway/build/clients/bar/bar.go

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

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

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

16 changes: 8 additions & 8 deletions examples/example-gateway/idl/clients/bar/bar.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ struct BarResponseRecur {
}

struct QueryParamsStruct {
1: required string name (zanzibar.http.ref = "query.name")
2: optional string userUUID (zanzibar.http.ref = "query.userUUID")
1: required string name
2: optional string userUUID
// TODO: support header annotation
3: optional string authUUID (zanzibar.http.ref = "query.authUUID")
3: optional string authUUID
4: optional string authUUID2 (zanzibar.http.ref = "query.myuuid")
5: required list<string> foo (zanzibar.http.ref = "query.foo")
5: required list<string> foo
}

struct QueryParamsOptsStruct {
1: required string name (zanzibar.http.ref = "query.name")
2: optional string userUUID (zanzibar.http.ref = "query.userUUID")
3: optional string authUUID (zanzibar.http.ref = "query.authUUID")
4: optional string authUUID2 (zanzibar.http.ref = "query.authUUID2")
1: required string name
2: optional string userUUID
3: optional string authUUID
4: optional string authUUID2
}

struct QueryParamsUntaggedStruct {
Expand Down
14 changes: 7 additions & 7 deletions examples/example-gateway/idl/endpoints/bar/bar.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ struct BarResponse {
}

struct QueryParamsStruct {
1: required string name (zanzibar.http.ref = "query.name")
2: optional string userUUID (zanzibar.http.ref = "query.userUUID")
1: required string name
2: optional string userUUID
3: optional string authUUID (zanzibar.http.ref="headers.x-uuid")
4: optional string authUUID2 (zanzibar.http.ref="headers.x-uuid2")
5: required list<string> foo (zanzibar.http.ref = "query.foo")
5: required list<string> foo
}

struct QueryParamsOptsStruct {
1: required string name (zanzibar.http.ref = "query.name")
2: optional string userUUID (zanzibar.http.ref = "query.userUUID")
3: optional string authUUID (zanzibar.http.ref = "query.authUUID")
4: optional string authUUID2 (zanzibar.http.ref = "query.authUUID2")
1: required string name
2: optional string userUUID
3: optional string authUUID
4: optional string authUUID2
}

struct QueryParamsUntaggedStruct {
Expand Down
7 changes: 3 additions & 4 deletions test/endpoints/bar/bar_arg_with_query_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ func TestBarWithNestedQueryParams(t *testing.T) {
"GET", "/bar/argWithNestedQueryParams",
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t,
"request.authUUID=auth-uuid&request.foo=hi&request.myuuid=auth-uuid2"+
"myuuid=auth-uuid2&request.authUUID=auth-uuid&request.foo=hi"+
"&request.name=a-name&request.userUUID=a-uuid",
r.URL.RawQuery,
)
Expand Down Expand Up @@ -871,9 +871,8 @@ func TestBarWithNestedQueryParamsWithOpts(t *testing.T) {
"GET", "/bar/argWithNestedQueryParams",
func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t,
"opt.name=b-name&opt.userUUID=b-uuid&request.authUUID=auth-uuid&"+
"request.foo=hi&request.myuuid=auth-uuid2&"+
"request.name=a-name&request.userUUID=a-uuid",
"myuuid=auth-uuid2&opt.name=b-name&opt.userUUID=b-uuid&request.authUUID=auth-uuid&"+
"request.foo=hi&request.name=a-name&request.userUUID=a-uuid",
r.URL.RawQuery,
)

Expand Down