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
OpenAPI specs with the body marked as not required for a post request, results in code that when called will trigger a segmentation fault because it is treated as always required.
The workaround is to provide an empty body, however it's both a surpri
openapi-generator version
Version: 5.2.0
OpenAPI declaration file content or url
openapi: 3.0.2
info:
version: v1
title: Simple Example of Bug
paths:
"/v1/application/{id}/lifecycle/{action}":
post:
parameters:
- name: id
in: path
description: ID of application
required: true
schema:
type: string
- name: action
in: path
description: action to take on the application
required: true
schema:
type: string
enum:
- activate
- deactivate
requestBody:
content:
application/json:
schema:
type: string
description: "No request body"
responses:
"204":
description: "Deactivated successfully"
"403":
description: Forbidden
Generation Details
docker run --user $(id -u):$(id -g) --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:v5.2.0 generate -i /local/openapi.yaml -g go -o /local/client --package-name client
output
[main] INFO o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO o.o.c.ignore.CodegenIgnoreProcessor - Output directory (/local/client) does not exist, or is inaccessible. No file (.openapi-generator-ignore) will be evaluated.
[main] INFO o.o.codegen.DefaultGenerator - OpenAPI Generator: go (client)
[main] INFO o.o.codegen.DefaultGenerator - Generator 'go' is considered stable.
[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.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] WARN o.o.codegen.DefaultCodegen - Empty operationId found for path: post /v1/application/{id}/lifecycle/{action}. Renamed to auto-generated operationId: v1ApplicationIdLifecycleActionPost
[main] INFO o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/api_default.go
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/docs/DefaultApi.md
[main] INFO o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/api/openapi.yaml
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/README.md
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/git_push.sh
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/.gitignore
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/configuration.go
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/client.go
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/response.go
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/go.mod
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/go.sum
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/.travis.yml
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/utils.go
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/.openapi-generator-ignore
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/.openapi-generator/VERSION
[main] INFO o.o.codegen.TemplateManager - writing file /local/client/.openapi-generator/FILES
################################################################################
# Thanks for using OpenAPI Generator. #
# Please consider donation to help us maintain this project 🙏 #
# https://opencollective.com/openapi_generator/donate #
################################################################################
Steps to reproduce
- create directory to run under (test)
- create file openapi.yaml containing the above yaml
- run the generator as described:
docker run --user $(id -u):$(id -g) --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:v5.2.0 generate -i /local/openapi.yaml -g go -o /local/client --package-name client
- adjust the generated go.mod in the client directory
sed -i -e 's:module .*:module github.com/test/test/client:' client/go.mod
- initialize a go mod in the current directory containing the following:
module github.com/test/test go 1.16 require github.com/test/test/client v0.1.0 replace github.com/test/test/client => ./client
- Run
go mod download && go mod vendor
- add the following as main.go in the current directory:
package main import ( "context" "fmt" "os" "github.com/test/test/client" ) func main() { fmt.Println("vim-go") cfg := client.NewConfiguration() app := client.NewAPIClient(cfg).DefaultApi _, err := app.V1ApplicationIdLifecycleActionPost(context.Background(), "some-id", "deactivate").Execute() if err != nil { os.Exit(1) } }
- Attempt to execute using
go run
:go run ./main.go vim-go panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x67df26] goroutine 1 [running]: github.com/test/test/client.setBody(0x69cda0, 0x0, 0x701fb6, 0x10, 0xc00011db40, 0x2800000014, 0x7f8500000028) /home/baileybd/git/github.com/test/test/vendor/github.com/test/test/client/client.go:432 +0x126 github.com/test/test/client.(*APIClient).prepareRequest(0xc00000e0f0, 0x75e1b0, 0xc0000180a8, 0xc00001e420, 0x2c, 0x6fe8f0, 0x4, 0x69cda0, 0x0, 0xc00011da18, ...) /home/baileybd/git/github.com/test/test/vendor/github.com/test/test/client/client.go:214 +0x19ad github.com/test/test/client.(*DefaultApiService).V1ApplicationIdLifecycleActionPostExecute(0xc00000e0f8, 0x75e1b0, 0xc0000180a8, 0xc00000e0f8, 0x6ff72e, 0x7, 0x7004d9, 0xa, 0x0, 0xc00004e710, ...) /home/baileybd/git/github.com/test/test/vendor/github.com/test/test/client/api_default.go:107 +0x4f6 github.com/test/test/client.ApiV1ApplicationIdLifecycleActionPostRequest.Execute(...) /home/baileybd/git/github.com/test/test/vendor/github.com/test/test/client/api_default.go:44 main.main() /home/baileybd/git/github.com/test/test/main.go:15 +0x29e exit status 2
Related issues/PRs
Appears #4701 is attempting to fix the same issue for Java
Suggest a fix
I believe the fix is to modify the following code:
To look something like:
{{#bodyParams}}
{{#required}}
localVarPostBody = r.{{paramName}}
{{/required}}
{{^required}}
if r.{{paramName}} != nil {
localVarPostBody = r.{{paramName}}
}
{{/required}}
{{/bodyParams}}
Applying the following diff to master prevents the segmentation fault:
diff --git modules/openapi-generator/src/main/resources/go/api.mustache modules/openapi-generator/src/main/resources/go/api.mustache
index 4a94d6aaf59..b4c7b90afb6 100644
--- modules/openapi-generator/src/main/resources/go/api.mustache
+++ modules/openapi-generator/src/main/resources/go/api.mustache
@@ -296,7 +296,14 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{/formParams}}
{{#bodyParams}}
// body params
+{{#required}}
localVarPostBody = r.{{paramName}}
+{{/required}}
+{{^required}}
+ if r.{{paramName}} != nil {
+ localVarPostBody = r.{{paramName}}
+ }
+{{/required}}
{{/bodyParams}}
{{#authMethods}}
{{#isApiKey}}
However I suspect this is incomplete and there is likely other files that would have to be updated based on the corresponding Java PR #4701