You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, when im generating Golang code for a simple endpoint that returns plain text the generated client runs in an error: undefined response type . I added the code snippets below.
When it now comes to decoding the response of the endpoint the generate Golang code looks like:
func (a*AdministrationApiService) AboutVersionGet(ctx context.Context) (string, *http.Response, error) {
var (
localVarHttpMethod=strings.ToUpper("Get")
localVarPostBodyinterface{}
localVarFileNamestringlocalVarFileBytes []bytelocalVarReturnValuestring
)
// create path and map variableslocalVarPath:=a.client.cfg.BasePath+"/about/version"localVarHeaderParams:=make(map[string]string)
localVarQueryParams:= url.Values{}
localVarFormParams:= url.Values{}
// to determine the Content-Type headerlocalVarHttpContentTypes:= []string{"application/json"}
// set Content-Type headerlocalVarHttpContentType:=selectHeaderContentType(localVarHttpContentTypes)
iflocalVarHttpContentType!="" {
localVarHeaderParams["Content-Type"] =localVarHttpContentType
}
// to determine the Accept headerlocalVarHttpHeaderAccepts:= []string{"text/plain"}
// set Accept headerlocalVarHttpHeaderAccept:=selectHeaderAccept(localVarHttpHeaderAccepts)
iflocalVarHttpHeaderAccept!="" {
localVarHeaderParams["Accept"] =localVarHttpHeaderAccept
}
r, err:=a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
iferr!=nil {
returnlocalVarReturnValue, nil, err
}
localVarHttpResponse, err:=a.client.callAPI(r)
iferr!=nil||localVarHttpResponse==nil {
returnlocalVarReturnValue, localVarHttpResponse, err
}
localVarBody, err:=ioutil.ReadAll(localVarHttpResponse.Body)
localVarHttpResponse.Body.Close()
iferr!=nil {
returnlocalVarReturnValue, localVarHttpResponse, err
}
iflocalVarHttpResponse.StatusCode<300 {
// If we succeed, return the data, otherwise pass on to decode error.//added by issue author: normal cast form byte to string would be enough if Content-Type is plain-texterr=a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
returnlocalVarReturnValue, localVarHttpResponse, err
}
iflocalVarHttpResponse.StatusCode>=300 {
newErr:=GenericSwaggerError{
body: localVarBody,
error: localVarHttpResponse.Status,
}
iflocalVarHttpResponse.StatusCode==200 { //added by issue author: is this even reachable?varvstringerr=a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
iferr!=nil {
newErr.error=err.Error()
returnlocalVarReturnValue, localVarHttpResponse, newErr
}
newErr.model=vreturnlocalVarReturnValue, localVarHttpResponse, newErr
}
iflocalVarHttpResponse.StatusCode==400 {
varvErrorMessageerr=a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
iferr!=nil {
newErr.error=err.Error()
returnlocalVarReturnValue, localVarHttpResponse, newErr
}
newErr.model=vreturnlocalVarReturnValue, localVarHttpResponse, newErr
}
iflocalVarHttpResponse.StatusCode==500 {
varvErrorMessageerr=a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
iferr!=nil {
newErr.error=err.Error()
returnlocalVarReturnValue, localVarHttpResponse, newErr
}
newErr.model=vreturnlocalVarReturnValue, localVarHttpResponse, newErr
}
returnlocalVarReturnValue, localVarHttpResponse, newErr
}
returnlocalVarReturnValue, localVarHttpResponse, nil
}
The problem here is that the decode() function only is able to decode xml or JSON data.
What we need is cast from byte to string if the contenttype is text/plain
The text was updated successfully, but these errors were encountered:
I think we should add the choice of strings.Contains(contentType, "text/plain") to the decode function in client.go that is generated by template file client.mustache.
It should be modified to look like this:
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
if strings.Contains(contentType, "application/xml") {
if err = xml.Unmarshal(b, v); err != nil {
return err
}
return nil
} else if strings.Contains(contentType, "application/json") {
if err = json.Unmarshal(b, v); err != nil {
return err
}
return nil
} else if strings.Contains(contentType, "text/plain") {
// I'm not sure that's going to cause panic
*(v.(*string)) = string(b)
return nil
}
return errors.New("undefined response type")
}
In order to return the message directly when the contentType is text/plain.
Description
Hello, when im generating Golang code for a simple endpoint that returns plain text the generated client runs in an error:
undefined response type
. I added the code snippets below.Swagger-codegen version
2.0
Swagger declaration file content or url
Command line used for generation
https://editor.swagger.io/
Steps to reproduce
Generate Go Client with Swagger generator
Related issues/PRs
#7751
Suggest a fix/enhancement
When it now comes to decoding the response of the endpoint the generate Golang code looks like:
The problem here is that the decode() function only is able to decode xml or JSON data.
What we need is cast from byte to string if the contenttype is text/plain
The text was updated successfully, but these errors were encountered: