Description
Bug Report Checklist
- [ X] 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?
- [X ] 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
ApiRequest methods for setting parameters do not work.
ApiRequest accessor methods MUST use pointer receivers as described in the Go tutorial to modify values in the Request object
https://tour.golang.org/methods/8
ApiRequest accessor methods are declared with value receiver and cannot modify the original request object.
For example with my test spec the api_default.go file has method like this
func (r ApiTestRequest) Test(test Test) ApiTestRequest {
r.test = &test
return r
}
Instead we need method like
func (r *ApiTestRequest) Test(test Test) *ApiTestRequest {
r.test = &test
return r
}
See go playground tests below
openapi-generator version
openapi-generator-cli 5.0.1
commit : c7fcb39
built : 2021-02-06T09:14:19Z
This line is changed/introduced when go-experimental was promoted to go in v5.0
OpenAPI declaration file content or url
---
openapi: 3.0.0
info:
description: test
version: 1.0.0
title: test
paths:
/test:
summary: test
post:
operationId: "test"
requestBody:
content:
"application/json":
schema:
$ref: '#/components/schemas/test'
responses:
'204':
description: "success"
components:
schemas:
test:
type: object
properties:
check:
type: string
Generation Details
java -jar tools/openapi-generator-cli-5.0.1.jar generate -g go \
-i openapi/schema.yaml \
-o oneoftest
Steps to reproduce
Try to set the test
field using the Test
request method.
Here is demo how it is not working in go play ground
https://play.golang.org/p/FvDgBM5UKQd
Here is fixed variant
https://play.golang.org/p/aWi7_hxAQdn
Related issues/PRs
If this bug is fixed I suppose the need to use reflection to access request fields will disappear.
Suggest a fix
The following line has to be changed
Current:
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request {
Fixed:
func (r *{{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) *{{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request {
*
has to be added before the Request type name to use pointer receiver and return pointer.