Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #8745
Closed
Open
Issue created Feb 18, 2021 by Administrator@rootContributor2 of 4 checklist items completed2/4 checklist items

[BUG][GO] Request object parameter accessor methods don't work

Created by: karaatanassov

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 : c7fcb39a 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

https://github.com/OpenAPITools/openapi-generator/issues/8744

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 https://github.com/OpenAPITools/openapi-generator/blob/df5050f3b0a2dde6aff9d5d1bd8a9601fc26ae18/modules/openapi-generator/src/main/resources/go/api.mustache#L59

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.

Assignee
Assign to
Time tracking