OpenAPITools / OpenAPITools/openapi-generator

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

Open
#8,745 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Go Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

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

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The generator template is modules/openapi-generator/src/main/resources/go/api.mustache at line 59; start by reading the request accessor declaration and generating the provided OpenAPI schema with the listed CLI command. Done means generated ApiTestRequest accessors mutate the original request and return a pointer, matching the linked playground behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.