OpenAPITools / OpenAPITools/openapi-generator
[BUG] golang client mishandles return type of string with content-type = application/json
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
API responses of JSON-encoded simple string value with content-type = application/json are mishandled by the golang client and are returned to the caller without JSON-decoding.
openapi-generator version
latest (5.1.1)
OpenAPI declaration file content or url
https://gist.github.com/jpriebe/3bfc34a63b586616bc22190b84a5440e
Generation Details
docker run \
--rm \
--volume "$PWD:/workdir" \
openapitools/openapi-generator-cli:latest \
generate \
--input-spec /workdir/openapi.yaml \
--generator-name go \
--package-name "client" \
--output /workdir
Steps to reproduce
- build an openapi spec for an API that produces application/json and has an endpoint that returns a string
- implement a server based on the spec
- generate a golang client with the spec
- use the client to make a call to the endpoint in question
- assuming the server returned a JSON-encoded string (as it should), the client will return JSON-encoded string instead of the raw string of the response
Suggest a fix
I think the problem is here:
When the caller expects a string, it will pass in a pointer to a string to the decode() function. The login on line 418 will pop the HTTP response body directly into the string and return.
I think that the decode() function should be modified in one of the following ways:
- add a check for content-type = text/plain to the logic block at line 418
- move the logic block at line 418 to line 442
Bottom line: if the server's content-type is application/json, the decode() function should be decoding JSON no matter what the expected go type is.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/go/client.mustache at the referenced line around 418, then review the surrounding decode() logic. Reproduce the generated Go client's handling of an application/json response containing a string, and confirm that JSON content is decoded to the raw string rather than returned encoded.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 50/100