Skip to content

Commit

Permalink
Merge pull request #622 from uber/fix_nil_pointer_dereference
Browse files Browse the repository at this point in the history
Fixing nil pointer dereference for optional fields
  • Loading branch information
tejaswiagarwal authored Aug 14, 2019
2 parents ab0af1d + 6435a6c commit d9eb102
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 8 additions & 6 deletions codegen/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,22 +804,24 @@ func (ms *MethodSpec) setClientRequestHeaderFields(
} else {
headerNameValuePair = "headers[%q]= string(*r%s)"
}
if len(seenOptStructs) == 0 {
statements.appendf(headerNameValuePair,
headerName, bodyIdentifier,
)
} else {
if len(seenOptStructs) != 0 {
closeFunction := ""
for seenStruct := range seenOptStructs {
if strings.HasPrefix(longFieldName, seenStruct) {
statements.appendf("if r%s != nil {", seenStruct)
closeFunction = closeFunction + "}"
}
}
statements.append(closeFunction)
}
if field.Required {
statements.appendf(headerNameValuePair,
headerName, bodyIdentifier,
)
statements.append(closeFunction)
} else {
statements.appendf("if r%s != nil {", bodyIdentifier)
statements.appendf(headerNameValuePair, headerName, bodyIdentifier)
statements.append("}")
}
}
}
Expand Down
4 changes: 3 additions & 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.

0 comments on commit d9eb102

Please sign in to comment.