OpenAPITools / OpenAPITools/openapi-generator
[BUG] generated go client adds new line at the end of the json string and causes issue with some api servers
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
the client generator for golang uses the golang json encoder when set the request body. The golang json encoder adds a new line at the end of the json string. this causes the generated client to set content-length to the length of json string plus one. The api server (HPE iLO) errors out because the mismtach of the content-length header and the actual json string length (iLO apparently doesn't care about the ending white space.
Another case is if the request body is null, instead of setting an empty body, the generated client sets "null" and content-length header to 5.
To work with applications that treat the ending new lines or other white spaces inconsistently, the generated client should use more defensive programming, e.g., trim the trailing new lines, white spaces in the generated json body and handle null request body as {} (with no new line at the end).
openapi-generator version
v4.2.2
OpenAPI declaration file content or url
https://review.opendev.org/plugins/gitiles/airship/go-redfish/+/refs/heads/master/spec/openapi.yaml
Generation Details
generated client: https://review.opendev.org/plugins/gitiles/airship/go-redfish/+/refs/heads/master/client/
Steps to reproduce
run the client in debug and examine the generated request.
Related issues/PRs
Suggest a fix
Add a check if body is null, then set it as "{}"
Add a call to check and trim the new line at the end of the json body created by the json encoder.
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 at modules/openapi-generator/src/main/resources/go/client.mustache around line 484, then inspect the generated client linked in the issue. Run that client in debug mode and examine the generated request; done means the request body and content length no longer include an unwanted trailing newline and null bodies follow the issue's stated expectation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100