OpenAPITools / OpenAPITools/openapi-generator
[BUG][GO] Patterned fields cause compile 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
When using patterned fields https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#patterned-fields-1 for HTTP Status Codes such as 4XX we get a compile time error because the codegen spits out lines like localVarHTTPResponse.StatusCode == 4XX. This requires manually editing the codegen file in order to get things to compile.
openapi-generator version
5.0.0-beta2, although this is the case in 5.0.0-beta and 4.3.1 as well
OpenAPI declaration file content or url
(or alternatively, https://raw.githubusercontent.com/ipfs/pinning-services-api-spec/46f23fdea009244a49c12b586c06ef30c2ca982f/ipfs-pinning-service.yaml)
Generation Details
java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~20.04-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)
java -jar openapi-generator-cli.jar generate -g go -i https://raw.githubusercontent.com/ipfs/pinning-services-api-spec/master/ipfs-pinning-service.yaml -o openapi
Steps to reproduce
go build
# github.com/GIT_USER_ID/GIT_REPO_ID
./api_pins.go:211:42: syntax error: unexpected XX, expecting {
./api_pins.go:221:42: syntax error: unexpected XX, expecting {
./api_pins.go:381:42: syntax error: unexpected XX, expecting {
./api_pins.go:391:42: syntax error: unexpected XX, expecting {
./api_pins.go:543:42: syntax error: unexpected XX, expecting {
./api_pins.go:553:42: syntax error: unexpected XX, expecting {
./api_pins.go:698:42: syntax error: unexpected XX, expecting {
./api_pins.go:708:42: syntax error: unexpected XX, expecting {
./api_pins.go:872:42: syntax error: unexpected XX, expecting {
./api_pins.go:882:42: syntax error: unexpected XX, expecting {
./api_pins.go:882:42: too many errors
Related issues/PRs
Suggest a fix
- Emit the same if statements we're already doing but put all the pattern matched ones at the end for if we haven't matched anything directly. Then order the patterns e.g.
if statusCode >= 400 && statusCode < 410 .... if statusCode >= 410... - Perhaps it's just my particular use case but I don't see any of the generated statusCode > 300 if statements doing anything particularly unique, perhaps just having all of the errors do the same thing would be sufficient here, but my understanding of the generator tools and OpenAPI spec creation is limited.
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
Reproduce the issue with the linked ipfs-pinning-service.yaml and the Go generator command, then inspect the generated api_pins.go around the reported lines. The fix is done when patterned HTTP status codes such as 4XX no longer produce invalid Go and the generated client passes go build.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100