OpenAPITools / OpenAPITools/openapi-generator
[BUG] Generated Go (golang for the search algorithm) code does not compile.
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
I am trying to generate a Go client for an OpenAPI 3.1 document that represents a contract for a JSON:API endpoint.
My understanding is that union types, especially oneOf and anyOf, can be challenging to generate. Our OpenAPI document leverages all three union types (oneOf, anyOf, and allOf).
I'm not sure whether the compilation errors are due to a pathological OpenAPI document, the fact that the OpenAPI document is 3.1, or if it represents a bug in the Go code generator.
openapi-generator version
7.5, latest
Labels from the :latest image tag, repo sha: openapitools/openapi-generator-cli@sha256:7b4b9591a282b9cfe8ff66fc00719069133e263b855bc80e18432dbde3d39fe6:
"Labels": {
"org.opencontainers.image.created": "2024-04-25T08:30:11Z",
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.revision": "a5d463d90fb579e828070ab2fd74104bb3dd9bc8",
"org.opencontainers.image.title": "openapi-generator-cli",
"org.opencontainers.image.version": ""
}
Labels from the :v7.5.0 image tag, repo sha: openapitools/openapi-generator-cli@sha256:cdf11948948de9c21c6035de47dd5fc73c1651c8ba2ea0a4b86a527608ef52a9`:
"Labels": {
"org.opencontainers.image.created": "2024-04-17T10:58:29Z",
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.revision": "01bea91481bb3176c87384e21ae4c105e17137f5",
"org.opencontainers.image.title": "openapi-generator-cli",
"org.opencontainers.image.version": ""
}
OpenAPI declaration file content or url
Gist link
Validates with the online swagger-parser and redocly:
npx -y @redocly/cli lint \
--skip-rule=no-unused-components \
--skip-rule=info-license \
--skip-rule=security-defined \
--skip-rule=no-server-example.com \
--skip-rule=operation-summary \
openapi.yaml
Generation Details
Generating Go client code with the Docker image using:
docker run --rm -v "$PWD:/local" openapitools/openapi-generator-cli:v7.5.0 generate \
-i /local/openapi.yaml \
-g go \
-o /local
Steps to reproduce
Install Go if you don't have it; the latest 1.22.2 is fine. Execute go version to make sure it is on your command path.
- Create and cd into a new directory to isolate the test case OpenAPI and the generated code.
- Download the problematic spec file from the gist as
openapi.yaml. - Run the docker command above to generate the code.
- Attempt to build the generated Go code:
go build ./..., and you should receive a number of compilation errors
Example output:
$ go build ./...
# github.com/GIT_USER_ID/GIT_REPO_ID
./model_applications_create_applications_request_data.go:100:21: syntax error: unexpected { at end of statement
./model_applications_create_applications_request_data.go:102:2: syntax error: non-declaration statement outside function body
./model_applications_create_applications_request_data.go:175:21: syntax error: unexpected { at end of statement
./model_applications_create_applications_request_data.go:177:2: syntax error: non-declaration statement outside function body
./model_applications_list_application_200_response.go:123:21: syntax error: unexpected { at end of statement
./model_applications_list_application_200_response.go:125:2: syntax error: non-declaration statement outside function body
./model_applications_list_applications_200_response.go:123:21: syntax error: unexpected { at end of statement
./model_applications_list_applications_200_response.go:125:2: syntax error: non-declaration statement outside function body
./model_applications_list_applications_200_response_all_of_data.go:24:2: syntax error: unexpected [, expected field name or embedded type
./model_applications_list_applications_200_response_all_of_data.go:25:2: syntax error: unexpected [, expected field name or embedded type
./model_applications_list_applications_200_response_all_of_data.go:25:2: too many errors
I reproduced this with openapitools/openapi-generator-cli:v7.5.0 and openapitools/openapi-generator-cli:latest.
Related issues/PRs
Suggest a fix
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
Download the linked gist as openapi.yaml and reproduce generation with the documented Docker command. Run go build ./... and inspect the reported generated files, including model_applications_create_applications_request_data.go and model_applications_list_applications_200_response_all_of_data.go. Done means the generated Go client builds successfully for this specification.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, go, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100