OpenAPITools / OpenAPITools/openapi-generator
[BUG][Go] AbstractGoCodegen: nil is not a valid type in Go
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 JSON Schema type null is mapped to nil when Go generators (which extend AbstractGoCodegen) generate model files.
https://github.com/OpenAPITools/openapi-generator/blob/9312ed831f2b8ffc3ee03bb2717f29f572735047/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java#L123
nil is not a type in Go. The closest alternative would be struct{}: an empty struct which takes up no memory and has only one possible value.
This is, though, a rather contrived example and I haven't found any use case for it so far.
openapi-generator version
Built locally from commit 9312ed8
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: title
description: description
version: 1.0.0
servers:
- url: http://localhost
paths:
/:
get:
summary: summary
responses:
200:
description: successful
content:
application/json:
schema:
$ref: "#/components/schemas/Example"
components:
schemas:
Example:
type: object
properties:
empty:
type: 'null'
Generation Details
~/openapi-generator (master)> java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ~/codegen-example/openapi.yaml -g go-echo-server -o /var/tmp/codegen
[main] INFO o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO o.o.c.ignore.CodegenIgnoreProcessor - Output directory (/var/tmp/codegen) does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] INFO o.o.codegen.DefaultGenerator - OpenAPI Generator: go-echo-server (server)
[main] INFO o.o.codegen.DefaultGenerator - Generator 'go-echo-server' is considered beta.
[main] INFO o.o.c.languages.AbstractGoCodegen - Environment variable GO_POST_PROCESS_FILE not defined so Go code may not be properly formatted. To define it, try `export GO_POST_PROCESS_FILE="/usr/local/bin/gofmt -w"` (Linux/Mac)
[main] INFO o.o.c.languages.AbstractGoCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/models/model_example.go
[main] WARN o.o.codegen.DefaultCodegen - Empty operationId found for path: get /. Renamed to auto-generated operationId: rootGet
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/handlers/api_default.go
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/.docs/api/openapi.yaml
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/models/hello-world.go
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/go.mod
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/handlers/container.go
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/main.go
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/Dockerfile
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/README.md
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/.openapi-generator-ignore
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/.openapi-generator/VERSION
[main] INFO o.o.codegen.TemplateManager - writing file /var/tmp/codegen/.openapi-generator/FILES
################################################################################
# Thanks for using OpenAPI Generator. #
# Please consider donation to help us maintain this project 🙏 #
# https://opencollective.com/openapi_generator/donate #
################################################################################
Content of /var/tmp/codegen/models/model_example.go
package models
type Example struct {
Empty nil `json:"empty,omitempty"`
}
The above code won't build and I expect the output to be:
package models
type Example struct {
Empty struct{} `json:"empty,omitempty"`
}
Steps to reproduce
- Save the aforementioned OpenAPI file as
~/codegen-example/openapi.yaml - Go to the root directory of
openapi-generatorand build it. - Run
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ~/codegen-example/openapi.yaml -g go-echo-server -o /var/tmp/codegen. - Check
/var/tmp/codegen/models/model_example.go.
Related issues/PRs
Couldn't find one.
Suggest a fix
Change the following code
https://github.com/OpenAPITools/openapi-generator/blob/9312ed831f2b8ffc3ee03bb2717f29f572735047/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java#L123
to
typeMapping.put("null", "struct{}");
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
Open modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java at the type mapping referenced in the issue. Reproduce with the provided OpenAPI YAML and go-echo-server generation command, then verify that the generated models/model_example.go uses a valid Go type for the null property and builds successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, java, openapi
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100