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

generator-go-sdk: compatiable with literal string response #1756

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions tools/generator-go-sdk/generator/templater_methods_autorest.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,16 @@ func (c methodsAutoRestTemplater) responderTemplate(responseStructName string, d

steps := make([]string, 0)
steps = append(steps, fmt.Sprintf("azure.WithErrorUnlessStatusCode(%s)", strings.Join(expectedStatusCodes, ", ")))
// delcare []byte for string body
var bsDeclare, bsAssign string
if c.operation.ResponseObject != nil {
if discriminatedType == "" { // If this is a discriminated type with a parent interface, do not use the autorest decorator to unmarshal it
if c.operation.FieldContainingPaginationDetails != nil {
steps = append(steps, "autorest.ByUnmarshallingJSON(&respObj)")
} else if typ := c.operation.ContentType; typ != nil && *typ == "text/powershell" {
bsDeclare = "var content []byte\n\t"
bsAssign = "str := string(content)\n\tresult.Model = &str\n\t"
steps = append(steps, "autorest.ByUnmarshallingBytes(&content)")
} else {
steps = append(steps, "autorest.ByUnmarshallingJSON(&result.Model)")
}
Expand Down Expand Up @@ -678,14 +684,15 @@ func (c %[1]s) responderFor%[2]s(resp *http.Response) (result %[5]s, err error)
// responderFor%[2]s handles the response to the %[2]s request. The method always
// closes the http.Response Body.
func (c %[1]s) responderFor%[2]s(resp *http.Response) (result %[5]s, err error) {
err = autorest.Respond(
%[6]serr = autorest.Respond(
resp,
%[3]s)
result.HttpResponse = resp
%[7]sresult.HttpResponse = resp

return
}
`, data.serviceClientName, c.operationName, strings.Join(steps, ",\n\t\t"), discriminatedType, responseStructName)
`, data.serviceClientName, c.operationName, strings.Join(steps, ",\n\t\t"), discriminatedType, responseStructName,
bsDeclare, bsAssign)

return &output, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func TestTemplateMethodsAutoRestBaseTypePredicates(t *testing.T) {
Type: resourcemanager.StringApiObjectDefinitionType,
},
ResourceIdName: stringPointer("PandaPop"),
ContentType: stringPointer("text/powershell"),
},
operationName: "List",
}.listOperationTemplate(input)
Expand Down Expand Up @@ -180,6 +181,7 @@ func (c pandaClient) List(ctx context.Context, id PandaPop) (resp ListOperationR
"api-version": defaultApiVersion,
}
preparer := autorest.CreatePreparer(
autorest.AsContentType("text/powershell"),
autorest.AsGet(),
autorest.WithBaseURL(c.baseUri),
autorest.WithPath(id.ID()),
Expand All @@ -190,11 +192,14 @@ func (c pandaClient) List(ctx context.Context, id PandaPop) (resp ListOperationR
// responderForList handles the response to the List request. The method always
// closes the http.Response Body.
func (c pandaClient) responderForList(resp *http.Response) (result ListOperationResponse, err error) {
var content []byte
err = autorest.Respond(
resp,
azure.WithErrorUnlessStatusCode(),
autorest.ByUnmarshallingJSON(&result.Model),
autorest.ByUnmarshallingBytes(&content),
autorest.ByClosing())
str := string(content)
result.Model = &str
result.HttpResponse = resp
return
}
Expand Down