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

Golang Generated api client does not work for plain text body types #7751

Closed
Vad1mo opened this issue Mar 2, 2018 · 1 comment
Closed

Golang Generated api client does not work for plain text body types #7751

Vad1mo opened this issue Mar 2, 2018 · 1 comment
Milestone

Comments

@Vad1mo
Copy link

Vad1mo commented Mar 2, 2018

Description

The Generated API Client for GoLang does not work properly for plain string body types.

The error message is: Invalid body type text/plain

Swagger-codegen version

2.3.1

Swagger declaration file content or url
/Token/:
    post:
      operationId: Token
      consumes:
        - text/plain
      responses:
        '200':
          description: OK
          schema:
           type: string
        '400':
          description: Bad Request
          schema:
            type: string
      produces:
        - "application/json"
        - "text/json"
      parameters:
        - in: body
          name: body
          description: The Grand Type
          schema:
            type: string
Steps to reproduce

Api_client.go

func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) {
	if bodyBuf == nil {
		bodyBuf = &bytes.Buffer{}
	}

	if reader, ok := body.(io.Reader); ok {
		_, err = bodyBuf.ReadFrom(reader)
	} else if b, ok := body.([]byte); ok {
		_, err = bodyBuf.Write(b)
	} else if s, ok := body.(string); ok {  // <<--
		_, err = bodyBuf.WriteString(s)
	} else if jsonCheck.MatchString(contentType) {
		err = json.NewEncoder(bodyBuf).Encode(body)
	} else if xmlCheck.MatchString(contentType) {
		xml.NewEncoder(bodyBuf).Encode(body)
	}

	if err != nil {
		return nil, err
	}

	if bodyBuf.Len() == 0 {
		err = fmt.Errorf("Invalid body type %s\n", contentType)
		return nil, err
	}
	return bodyBuf, nil
}
Suggest a fix/enhancement

The check should be changed from `body.(string); ok
The body is an interface containing the pointer to the string.

	} else if s, ok := body.(*string); ok {
		_, err = bodyBuf.WriteString(*s)
Vad1mo added a commit to Vad1mo/swagger-codegen that referenced this issue Mar 2, 2018
@HugoMario
Copy link
Contributor

fixed by #7987

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants