Skip to content

Commit

Permalink
Disambiguate nearly identical query params (#664)
Browse files Browse the repository at this point in the history
If there are two query params that differ in their casing only, then zanzibar treats them as the same
We have had some legacy endpoints where people had arguments foo_bar and fooBar and wanted both to be treated differently

This adds support for it in Zanzibar
  • Loading branch information
argouber committed Oct 29, 2019
1 parent fc98106 commit 8175628
Show file tree
Hide file tree
Showing 16 changed files with 3,100 additions and 4,956 deletions.
17 changes: 15 additions & 2 deletions codegen/method.go
Expand Up @@ -1262,6 +1262,17 @@ func (ms *MethodSpec) setWriteQueryParamStatements(
return nil
}

// makeUniqIdent appends an integer to the identifier name if there is duplication already
// The reason for this is to disambiguate a query param "deviceID" from "device_ID" - yes people did do that
func makeUniqIdent(identifier string, seen map[string]int) string {
count := seen[identifier]
seen[identifier] = count + 1
if count > 0 {
return identifier + strconv.Itoa(count)
}
return identifier
}

func (ms *MethodSpec) setParseQueryParamStatements(
funcSpec *compile.FunctionSpec, packageHelper *PackageHelper, hasNoBody bool,
) error {
Expand All @@ -1271,6 +1282,7 @@ func (ms *MethodSpec) setParseQueryParamStatements(

var finalError error
var stack = []string{}
seenIdents := map[string]int{}

visitor := func(
goPrefix string, thriftPrefix string, field *compile.FieldSpec,
Expand Down Expand Up @@ -1356,9 +1368,10 @@ func (ms *MethodSpec) setParseQueryParamStatements(

return false
}
identifierName := CamelCase(longQueryName) + "Query"
baseIdent := makeUniqIdent(CamelCase(longQueryName), seenIdents)
identifierName := baseIdent + "Query"
okIdentifierName := baseIdent + "Ok"

okIdentifierName := CamelCase(longQueryName) + "Ok"
if field.Required {
statements.appendf("%s := req.CheckQueryValue(%q)",
okIdentifierName, shortQueryParam,
Expand Down
271 changes: 124 additions & 147 deletions 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.

0 comments on commit 8175628

Please sign in to comment.