OpenAPITools / OpenAPITools/openapi-generator
[BUG][GO] Do not silently ignore EncodeJSONResponse errors
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
In the generated Go code, there are EncodeJSONResponse calls with error response always ignored. In case a bad input is given to json encoder to encode, the API returns 200 OK with empty body and there is no way to know that an error happened.
openapi-generator version
6.2.1 - 6.6.0
Steps to reproduce
Put invalid map as an i parameter to the EncodeJSONResponse call. For example, map[interface{}]interface{}.
For example, I am creating an endpoint providing an openapi.json file, but the source is fetched from a YAML file, which can have integers as keys, while JSON does not. This creates an error, which is ignored in the generated code.
Related issues/PRs
Didn't find any.
Suggest a fix
For me, the best fix would be one that would return 500 in the response and logged the error from JSON encoding. But at the very least, I would suggest adding an empty assignment to make it obvious that the generated code is silently ignoring an error and leaving it up to the user to modify the handling.
_ = EncodeJSONResponse(result.Body, &result.Code, w)
I can do that, in case we won't find any better solution.
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
Locate the generated Go EncodeJSONResponse call sites and the generator template that emits them; begin by reproducing the issue with map[interface{}]interface{} as the i parameter. Done means the generated endpoint no longer silently loses JSON encoding errors, with the chosen error response or explicit handling verified against the example in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100